//****************************************************************// // // // 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_Board.h // Description: abstraction for twixt board, includes peg places (including // empties) and links // 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_Board ADT // conditional compilation to avoid trying to include same thing twice #ifndef TW_BOARD_H #define TW_BOARD_H #include #include "point.h" #include "tw-color.h" #include "tw-peg.h" #include "tw-link.h" // #include "exceptions.h" const int MaxPrintArrayCols = 256; const int MaxPrintArrayRows = 128; const int MaxBoardArrayCols = 32; // SUN C++ const int max_board_size = 30; // Tw_Board ADT: class description // Defines class and its interface to the world // implementation is in tw-link.cpp class Tw_Board { // private (by default) - put up here so compiler knows // GNU g++ // static const int max_board_size = 30; // public interface can be used by anybody public: // member functions // constructors - // first - 0 or 1 args - produces an empty board of default or given size Tw_Board(int size = 20); // second - 4 args - game in progress - board known w/ // pegs, black links and white links, and board size // (cant be inferred from board_arr since that is the // size of the maximum possible board) Tw_Board(Tw_Peg board_arr[][MaxBoardArrayCols], Tw_Link black_links[], Tw_Link white_links[], int num_black, int num_white, int size = 20); // inspectors // report if there is any winner Tw_Color WhoWon() const; // report all links of a given color in board int GetAllLinksForColor(Tw_Link approp_links[], const Tw_Color& color) const; // report all links in board int GetAllLinks(Tw_Link approp_links[]) const; // report all black links in board int GetBlackLinks(Tw_Link black_links[]) const; // report all black pegs on board int GetBlackPegs(Tw_Peg black_pegs[]) const; // report all white links in board int GetWhiteLinks(Tw_Link white_links[]) const; // report all white pegs on board int GetWhitePegs(Tw_Peg white_pegs[]) const; // report all empty places on the board int GetEmptyPlaces(Tw_Peg empty_pegs[]) const; // report all links on board which involve a given position int GetLinksForPosition(const Tw_Peg& peg, Tw_Link return_links[]) const; int GetLinksForPosition(const Point& pos, Tw_Link return_links[]) const; // determine if this is a legal position to put the specified color peg // position must be empty and not be in opponents home row bool LegalPositionToPlace(const Tw_Peg& potential_pos, const Tw_Color& color) const; bool LegalPositionToPlace(const Point& potential_pos, const Tw_Color& color) const; bool LegalPositionToPlace(const int row, const int col, const Tw_Color& color) const; // determine if this is a legal link to add to the board // link must not be blocked by own or opponents links bool LegalLink(const Tw_Link& potential_link) const; bool LegalLink(const Tw_Peg& startpos, const Tw_Peg& endpos) const; bool LegalLink(const Point& startpos, const Point& endpeg, const Tw_Color& color) const; bool LegalLink(const int start_row, const int start_col, const int end_row, const int end_col, const Tw_Color& color) const; int FindLegalPotentialLinksForPosition(const Tw_Peg& peg, Tw_Link return_links[]) 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 PlacePeg(const int row, const int colm, const Tw_Color& col); bool PlacePeg(const Point& pos, const Tw_Color& col); bool PlacePeg(const Tw_Peg& pos, const Tw_Color& col); bool PlacePeg(const Tw_Peg& pos); // if the link is legal then add link to the board // and return true otherwise return false bool PlaceLink(const Tw_Link& link); bool PlaceLink(const Tw_Peg& peg1, const Tw_Peg& peg2); bool PlaceLink(const Point& peg1, const Point& peg2, const Tw_Color& col); bool PlaceLink(const int startrow, const int startcol, const int endrow, const int endcol, const Tw_Color& col); // facilitators // find chain of linked pegs from a given peg int FindAllLinkedPegs(const Tw_Peg& startpeg, Tw_Peg return_pegs[]) const; // place peg on a board should go under board // some stream facilitators // draw board // this may just be our insert method void DrawBoard(ostream &sout) const; void DrawSimpleBoardWLinks(ostream &sout) const; // 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) void GetBoardArr(Tw_Peg board_arr[][MaxBoardArrayCols]) const; // get specific position Tw_Peg GetSpecPosit(int row, int col) const; // check that a peg is as advertised on the board bool PegThere(const Tw_Peg& testpeg) const; // find out board size int GetBoardSize() const; // find out the number of links for each color int GetNumBlackLinks() const; int GetNumWhiteLinks() const; Point GetPosition() const; // mutators // establish new values of attributes (data members) // facilitators bool BlackWon() const ; bool WhiteWon() const ; bool Linkable (const Tw_Peg first_home_pegs[],const int num_first_home_pegs, const Tw_Peg last_home_pegs[],const int num_last_home_pegs) const; void DrawScaledPrettyBoard(ostream &sout, const int magnif) const; void CreatePrintArray(char print_arr[][MaxPrintArrayCols], const int magnif) const; void AddLinkToPrintArray(char print_arr[][MaxPrintArrayCols], const Tw_Color curr_color, const Tw_Link& curr_link, const int magnif) const; void PrintPrintArray(ostream &sout,char print_arr[][MaxPrintArrayCols], const int full_size) const; void PrintBoardLinks(ostream &sout) const; // 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 const int board_size; // board array and arrays of black and white links // could do this as dynamic storage // avoiding allocating arrays as big as may be possible // the cost is that access is then not as convenient Tw_Peg board_arr[max_board_size+2][max_board_size+2]; // list of links may need to be bigger Tw_Link list_of_black_links[max_board_size * max_board_size]; Tw_Link list_of_white_links[max_board_size * max_board_size]; // current counts of links necessary so know how far // to look in the array int num_black_links; int num_white_links; }; // 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_Board ADT: auxiliary operator description ostream& operator<<(ostream &sout, const Tw_Board &s); istream& operator>>(istream &sin, Tw_Board &r); #endif