//////////////////////////////////////////////////////// // // tttboard.h // // Started 1/14/98 by Jeff Ondich // Last modified: 1/14/98 // // This is the interface for the class TTTBoard. // TTTBoard objects store the current state of // a game of Tic-Tac-Toe, and provide operations for // manipulating the game. // //////////////////////////////////////////////////////// #ifndef TTTBOARD_H #define TTTBOARD_H typedef char PlayerID; const PlayerID kPlayerX = 'X'; const PlayerID kPlayerO = 'O'; const PlayerID kNoPlayer = ' '; enum { kTopLeft = 0, kTopMiddle, kTopRight, kMiddleLeft, kMiddleMiddle, kMiddleRight, kBottomLeft, kBottomMiddle, kBottomRight }; class TTTBoard { private: char mBoard[9]; PlayerID mWhoseTurn; public: // Constructor TTTBoard( void ); // Getting information about data members bool IsOpen( int theSquare ) const; PlayerID WhoIsThere( int theSquare ) const; PlayerID WhoseTurn( void ) const; bool CanWin( PlayerID thePlayer, int & theSquare ) const; bool CanBlock( PlayerID thePlayer, int & theSquare ) const; // Making moves bool MakeMove( PlayerID thePlayer, int theSquare ); // Displaying the state of the game void DrawBoard( void ); }; //////////////////////////////////////////////////////// // // Descriptions of the member functions // //////////////////////////////////////////////////////// // The constructor TTTBoard() initializes the board // to empty--neither X nor O has moved--and sets // mWhoseTurn to kPlayerX. // IsOpen() returns false if theSquare is // occupied by an X or an O, and true otherwise. // WhoIsThere() returns kPlayerX, kPlayerO, or kNoPlayer, // depending on what is in theSquare. // WhoseTurn() returns kPlayerX or kPlayerO, depending on // whose turn it is. As indicated in the description of // the constructor, X always goes first. // CanWin() determines whether there is a square in which // thePlayer can move to win the game. If it is // thePlayer's move and there is a winning move available, // CanWin() sets theSquare equal to the winning move // and returns true. Otherwise, CanWin() returns false. // CanBlock() determines whether there is a square in which // thePlayer can move to prevent the other player from // getting three in a row on the next move. If it is // thePlayer's move and there is a blocking move available, // CanBlock() sets theSquare equal to the blocking move // and returns true. Otherwise, CanBlock() returns false. // If it is thePlayer's move and theSquare is open, MakeMove() // makes a move for thePlayer in theSquare and returns true. // Otherwise, MakeMove() returns false. // DrawBoard() prints some kind of representation of the // Tic-Tac-Toe board to standard output (cout). For // example, the display might look like this: // // | | // X | O | // | | // ----------- // | | // X | | // | | // ----------- // | | // O | | // | | // // It is X's turn. // #endif