00001 //---------------------------------------------------------------------------- 00002 /** @file GoMoveExecutor.h 00003 Class GoMoveExecutor. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef GO_MOVEEXECUTOR_H 00008 #define GO_MOVEEXECUTOR_H 00009 00010 #include "GoBoardUtil.h" 00011 00012 //---------------------------------------------------------------------------- 00013 00014 /** Used to execute and undo one move without having to worry about undoing 00015 the move. 00016 */ 00017 class GoMoveExecutor 00018 { 00019 public: 00020 GoMoveExecutor(GoBoard& board, SgPoint move) 00021 : m_board(board) 00022 { 00023 m_isLegal = GoBoardUtil::PlayIfLegal(m_board, move); 00024 } 00025 00026 GoMoveExecutor(GoBoard& board, SgPoint move, SgBlackWhite player) 00027 : m_board(board) 00028 { 00029 m_isLegal = GoBoardUtil::PlayIfLegal(m_board, move, player); 00030 } 00031 00032 ~GoMoveExecutor() 00033 { 00034 if (m_isLegal) 00035 m_board.Undo(); 00036 } 00037 00038 bool IsLegal() const 00039 { 00040 return m_isLegal; 00041 } 00042 00043 /** Can be used to undo the move before the MoveExecutor is destructed, to 00044 force the situation back the previous state. 00045 Note that this can only be called for legal moves, and IsLegal returns 00046 false afterwards. 00047 */ 00048 void UndoMove() 00049 { 00050 SG_ASSERT(m_isLegal); 00051 m_board.Undo(); 00052 m_isLegal = false; 00053 } 00054 00055 private: 00056 GoBoard& m_board; 00057 00058 bool m_isLegal; 00059 00060 /** Not implemented. */ 00061 GoMoveExecutor(const GoMoveExecutor&); 00062 00063 /** Not implemented. */ 00064 GoMoveExecutor& operator=(const GoMoveExecutor&); 00065 }; 00066 00067 //---------------------------------------------------------------------------- 00068 00069 #endif // GO_MOVEEXECUTOR_H 00070