Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GoMoveExecutor.h

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


Sun Mar 13 2011 Doxygen 1.7.1