//****************************************************************// // // // Copyright (c) 1997. // // Dr. Michael A. Redmond // // // // This software may not be distributed further without permission from // // Dr Michael A. Redmond. // // // // This software is distributed WITHOUT ANY WARRANTY. No claims are made // // as to its functionality or purpose. // // // // Title: Tw_Link.h // Description: abstraction for twixt link, includes two pegs and color // Author: Dr Michael A. Redmond // // Date: 3/07/97 // // // // Overcommented because also explaining the language as well // as the program // //****************************************************************// // tw-peg.h: declaration of Tw_Link ADT // conditional compilation to avoid trying to include same thing twice #ifndef TW_LINK_H #define TW_LINK_H #include #include #include "point.h" #include "tw-color.h" #include "tw-peg.h" // Tw_Link ADT: class description // Defines class and its interface to the world // implementation is in tw-peg.cpp class Tw_Link { friend class Tw_Board; // Tw_Board needs access to GetStartPeg etc // public interface can be used by anybody public: #if EXCEPT_HAND_ON // nested class definition for illegal_link excpetions // stdlib.h must be included so that we CAN BASE OUR EXCEPTIONS ON PARENT CLASSES DEFINED FOR C++ // class for illegal link exceptions // holds info about the circumstances of the error // 1) the reason for the exception - bad distance, incompatible colors, // // 2) the pegs that were attempted to be linked // 3) the color of the attemped link // class illegal_link : public range_error { class illegal_link { public: // constructors illegal_link(const char* msg, const Tw_Peg& startpeg, const Tw_Peg& endpeg); illegal_link(const char* msg, const Tw_Peg& startpeg, const Tw_Peg& endpeg, const Tw_Color& color); void terminate(); // probably want a display method private: char message[256]; Tw_Peg startpeg; Tw_Peg endpeg; Tw_Color color; }; #endif // member functions // constructors - // each makes sure that the coordinates are the right // distance apart. And versions using pegs checks // that colors are compatible // however, does not check if linkable on board // (board class's problem) // first - given two pegs, create the link // (color is in the peg object) #if EXCEPT_HAND_ON Tw_Link(const Tw_Peg& peg1, const Tw_Peg& peg2) throw(illegal_link); #endif #if EXCEPT_HAND_OFF #endif Tw_Link(const Tw_Peg& peg1, const Tw_Peg& peg2) ; // second - given two pegs and the color, create the // link #if EXCEPT_HAND_ON Tw_Link(const Tw_Peg& peg1, const Tw_Peg& peg2, const Tw_Color& col) throw(illegal_link); #endif #if EXCEPT_HAND_OFF #endif Tw_Link(const Tw_Peg& peg1, const Tw_Peg& peg2, const Tw_Color& col) ; // third - given two locations and the color, create the // link #if EXCEPT_HAND_ON Tw_Link(const Point& loc1, const Point& loc2, const Tw_Color& col) throw(illegal_link); #endif #if EXCEPT_HAND_OFF #endif Tw_Link(const Point& loc1, const Point& loc2, const Tw_Color& col) ; // fourth - given start and end coordinates and the // color, create the link #if EXCEPT_HAND_ON Tw_Link(const int startrow, const int startcolumn, const int endrow, const int endcol, const Tw_Color& col) throw(illegal_link); #endif #if EXCEPT_HAND_OFF #endif Tw_Link(const int startrow, const int startcolumn, const int endrow, const int endcol, const Tw_Color& col) ; // fifth - default - has no useful values but exists #if EXCEPT_HAND_ON Tw_Link() throw(); #endif #if EXCEPT_HAND_OFF #endif Tw_Link() ; // inspectors // report back what color is the link Tw_Color WhatColor() const; // returns a one char abreviation for the color char GetCharRep() const; // mutators - establish new values of attributes (data members) // facilitators // determine if two links are in conflict with each other // they are in conflict with each other if they would cross bool Conflict(const Tw_Link& link2) const; // rest are in support of Conflict so probably should be // protected // determine slope of a link float CalcSlope() const; // determine y intercept of a link float CalcIntercept(const float slope) const; // determine if two links are anywhere near each other bool Nearby(const Tw_Link& link2) const; // determine if two links intersect bool Intersect(const float slope1, const float interc1, const Tw_Link& link2, const float slope2, const float interc2) const; // determine if link shares a common end point with another link bool common_end_pt(const Tw_Link& link2) const; // determine if a given peg is part of the link bool peg_part_of_link(const Tw_Peg& testpeg) const; // return the other peg in a link given one peg in a link Tw_Peg other_part_of_link(const Tw_Peg& testpeg) const; // determine if two links are equal bool LinksEqual(const Tw_Link& link2) const; // place link on a board should go under board // some stream facilitators // write to stream void Insert(ostream &sout) const; // read from stream void Extract(istream &sin); // protected interface can be used by objects that are in class, // and objects that are in a subtype class (derived class) protected: // inspectors // return values of attributes (data members) Tw_Peg GetStartPeg() const; Tw_Peg GetEndPeg() const; Tw_Color GetLinkColor() const; // mutators // establish new values of attributes (data members) void SetStartPeg(const Tw_Peg& pos); void SetEndPeg(const Tw_Peg& pos); void SetLinkColor(const Tw_Color& col); // facilitators // private members can only be used by other member functions and // operators of this class // typically all attributes (data members) go here private: // data members Tw_Peg startpeg; Tw_Peg endpeg; Tw_Color linkcolorv; }; // below overloads standard c++ operators so that they can be used // with tw_peg class objects, so syntax is like with ints floats etc // // Tw_Link ADT: auxiliary operator description ostream& operator<<(ostream &sout, const Tw_Link &s); istream& operator>>(istream &sin, Tw_Link &r); bool operator==(const Tw_Link& link1, const Tw_Link& link2); #endif