//****************************************************************// // // // 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_Peg.h // Description: abstraction for twixt peg, includes position and color // Author: Dr Michael A. Redmond // // Date: 2/11/97 // // // // Overcommented because also explaining the language as well // as the program // //****************************************************************// // tw-peg.h: declaration of Tw_Peg ADT // conditional compilation to avoid trying to include same thing twice #ifndef TW_PEG_H #define TW_PEG_H #include #include "point.h" #include "tw-color.h" const int badrow = -1; const int badcol = -1; // Tw_Peg ADT: class description // Defines class and its interface to the world // implementation is in tw-peg.cpp class Tw_Peg { // public interface can be used by anybody friend class Tw_Board; // Tw_Board needs access to GetPosition friend class Tw_Link; // Tw_Link needs access to GetPosition public: // member functions // constructors - prob shouldn't provide second - can // be simulated using Tw_Peg(Point(row,column),col); Tw_Peg(); Tw_Peg(Point pos, Tw_Color col = empty); Tw_Peg(int row, int column, Tw_Color col = empty); // inspectors // report back if a position is free bool PositionFree() const; // report back what color is in the position Tw_Color WhatColor() const; // report all offsets from the position // without concern for legality void AllOffsets(Point neighbors[]) const; // mutators - establish new values of attributes (data members) // if the position is free fill with specified color // and return true otherwise return false bool AcceptPeg(Tw_Color col); // facilitators // place peg on a board should go under board // form a link should go under link // check two pegs to see if they are appropriate // distance apart to form a link bool LegalLinkOffset(const Tw_Peg peg2) const; // determines if two pegs are equal bool PegEqual(const Tw_Peg peg2) const; // 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) Point GetPosition() const; Tw_Color GetPegColor() const; // mutators // establish new values of attributes (data members) void SetPosition(Point pos); void SetPegColor(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 Point position; Tw_Color colorv; }; // 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_Peg ADT: auxiliary operator description ostream& operator<<(ostream &sout, const Tw_Peg &s); istream& operator>>(istream &sin, Tw_Peg &r); bool operator==(const Tw_Peg &left, const Tw_Peg &right); #endif