00001 //---------------------------------------------------------------------------- 00002 /** @file Game.hpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef GAME_HPP 00007 #define GAME_HPP 00008 00009 #include "Hex.hpp" 00010 #include "Move.hpp" 00011 #include "StoneBoard.hpp" 00012 00013 _BEGIN_BENZENE_NAMESPACE_ 00014 00015 //---------------------------------------------------------------------------- 00016 00017 /** A Game of Hex. 00018 00019 @todo Store time-info in history, so that undoing moves fixes the 00020 time remaining. 00021 */ 00022 class Game 00023 { 00024 public: 00025 00026 /** Creates a new game on the given board. Calls NewGame(). */ 00027 explicit Game(StoneBoard& brd); 00028 00029 //---------------------------------------------------------------------- 00030 00031 /** Starts a new game. The board and move history are cleared. */ 00032 void NewGame(); 00033 00034 //---------------------------------------------------------------------- 00035 00036 typedef enum { INVALID_MOVE, OCCUPIED_CELL, VALID_MOVE } ReturnType; 00037 00038 /** If move is invalid (color is not BLACK or WHITE, point is an 00039 invalid point, point is swap when swap not available) then 00040 INVALID_MOVE is returned and game/board is not changed. If 00041 point is occupied, returns CELL_OCCUPIED. Otherwise, returns 00042 VALID_MOVE and plays the move to the board and adds it to the 00043 game's history. */ 00044 ReturnType PlayMove(HexColor color, HexPoint point); 00045 00046 /** The last move is cleared from the board and removed from the 00047 game history. Does nothing if history is empty. */ 00048 void UndoMove(); 00049 00050 //---------------------------------------------------------------------- 00051 00052 /** Returns the time remaining for color. */ 00053 double TimeRemaining(HexColor color) const; 00054 00055 /** Sets the time remaining for the given player. */ 00056 void SetTimeRemaining(HexColor color, double time); 00057 00058 //---------------------------------------------------------------------- 00059 00060 /** Returns the game board. */ 00061 StoneBoard& Board(); 00062 00063 /** Returns a constant reference to the game board. */ 00064 const StoneBoard& Board() const; 00065 00066 /** Change board game is played on. */ 00067 void SetBoard(StoneBoard& brd); 00068 00069 /** Returns the history of moves. */ 00070 const MoveSequence& History() const; 00071 00072 /** Whether swap move is allowed to be played or not. */ 00073 bool AllowSwap() const; 00074 00075 /** See SwapAllowed() */ 00076 void SetAllowSwap(bool enable); 00077 00078 /** Amount of time given to each player at start of game. */ 00079 double GameTime() const; 00080 00081 /** See GameTime() 00082 Can only be called if no moves have been played yet. 00083 */ 00084 void SetGameTime(double time); 00085 00086 //---------------------------------------------------------------------- 00087 00088 private: 00089 00090 StoneBoard* m_board; 00091 00092 MoveSequence m_history; 00093 00094 double m_time_remaining[BLACK_AND_WHITE]; 00095 00096 /** See SwapAllowed() */ 00097 bool m_allow_swap; 00098 00099 /** See GameTime() */ 00100 double m_game_time; 00101 }; 00102 00103 inline StoneBoard& Game::Board() 00104 { 00105 return *m_board; 00106 } 00107 00108 inline const StoneBoard& Game::Board() const 00109 { 00110 return *m_board; 00111 } 00112 00113 inline void Game::SetBoard(StoneBoard& brd) 00114 { 00115 m_board = &brd; 00116 } 00117 00118 inline const MoveSequence& Game::History() const 00119 { 00120 return m_history; 00121 } 00122 00123 inline double Game::TimeRemaining(HexColor color) const 00124 { 00125 return m_time_remaining[color]; 00126 } 00127 00128 inline void Game::SetTimeRemaining(HexColor color, double time) 00129 { 00130 m_time_remaining[color] = time; 00131 } 00132 00133 inline bool Game::AllowSwap() const 00134 { 00135 return m_allow_swap; 00136 } 00137 00138 inline void Game::SetAllowSwap(bool enable) 00139 { 00140 m_allow_swap = enable; 00141 } 00142 00143 inline double Game::GameTime() const 00144 { 00145 return m_game_time; 00146 } 00147 00148 //---------------------------------------------------------------------------- 00149 00150 /** Utilities on Games. */ 00151 namespace GameUtil 00152 { 00153 /** Returns true if the game is over, that is, if the current 00154 position contains a solid connection for one player. */ 00155 bool IsGameOver(const Game& game); 00156 00157 /** If game contains the given position, returns the move history 00158 from that position to the current end of the game. */ 00159 bool SequenceFromPosition(const Game& game, const StoneBoard& pos, 00160 MoveSequence& seq); 00161 00162 /** Converts a game history into a sequence of points played. */ 00163 void HistoryToSequence(const MoveSequence& history, 00164 PointSequence& sequence); 00165 } 00166 00167 //---------------------------------------------------------------------------- 00168 00169 _END_BENZENE_NAMESPACE_ 00170 00171 #endif // GAME_HPP