00001
00002
00003 #include "osl/game_playing/gameState.h"
00004 #include "osl/game_playing/openingBookTracer.h"
00005 #include "osl/move_classifier/pawnDropCheckmate.h"
00006 #include "osl/move_classifier/moveAdaptor.h"
00007 #include "osl/effect_util/effectUtil.h"
00008 #include "osl/apply_move/doUndoMoveStack.h"
00009 #include "osl/hash/hashKeyStack.h"
00010 #include "osl/container/moveStack.h"
00011 #include "osl/repetitionCounter.h"
00012 #include "osl/sennichite.h"
00013
00014 struct osl::game_playing::GameState::State
00015 {
00016 typedef DoUndoMoveStack<HashEffectState> lock_stack_t;
00017 HashEffectState state;
00018 RepetitionCounter counter;
00019 lock_stack_t move_stack;
00020 MoveStack move_history;
00021 const SimpleState initial_state;
00022
00023 State(const SimpleState& initial_state)
00024 : state(initial_state), counter(state), initial_state(initial_state)
00025 {
00026 move_history.reserve(1024);
00027 }
00028 enum CopyType { FORK };
00029 State(CopyType, const State& src)
00030 : state(src.state), counter(src.counter), move_history(src.move_history),
00031 initial_state(src.initial_state)
00032 {
00033 }
00034 };
00035
00036 osl::game_playing::
00037 GameState::GameState(const SimpleState& initial_state)
00038 : stack(new State(initial_state))
00039 {
00040 }
00041
00042 osl::game_playing::
00043 GameState::GameState(const State& src)
00044 : stack(new State(State::FORK, src))
00045 {
00046 }
00047
00048 osl::game_playing::
00049 GameState::~GameState()
00050 {
00051 }
00052
00053 const osl::Sennichite osl::game_playing::
00054 GameState::pushMove(Move m)
00055 {
00056 stack->move_history.push(m);
00057 const Sennichite result
00058 = stack->counter.isSennichite(stack->state, m);
00059 stack->counter.push(stack->state, m);
00060 stack->move_stack.push(stack->state, m);
00061 return result;
00062 }
00063
00064 osl::game_playing::GameState::MoveType osl::game_playing::
00065 GameState::isIllegal(Move m) const
00066 {
00067 if (! state().isValidMove(m, false))
00068 return OTHER_INVALID;
00069 typedef move_classifier::PlayerMoveAdaptor<move_classifier::PawnDropCheckmate>
00070 PawnDropCheckmate_t;
00071 if (PawnDropCheckmate_t::isMember(state(), m))
00072 return PAWN_DROP_FOUL;
00073
00074 stack->move_stack.push(stack->state, m);
00075 const bool unsafe_king
00076 = EffectUtil::isKingInCheck(alt(state().getTurn()), state());
00077 stack->move_stack.pop();
00078
00079 if (unsafe_king)
00080 return UNSAFE_KING;
00081
00082 return VALID;
00083 }
00084
00085 const osl::Move osl::game_playing::
00086 GameState::popMove()
00087 {
00088 const Move result = stack->move_history.lastMove();
00089 assert(canPopMove());
00090 stack->move_history.pop();
00091 stack->counter.pop();
00092 stack->move_stack.pop();
00093 return result;
00094 }
00095
00096 const osl::HashEffectState& osl::game_playing::
00097 GameState::state() const
00098 {
00099 return stack->state;
00100 }
00101
00102 int osl::game_playing::
00103 GameState::moves() const
00104 {
00105 return stack->move_history.size();
00106 }
00107
00108 const osl::MoveStack& osl::game_playing::
00109 GameState::moveHistory() const
00110 {
00111 return stack->move_history;
00112 }
00113
00114 const osl::hash::HashKeyStack& osl::game_playing::
00115 GameState::hashHistory() const
00116 {
00117 return stack->counter.history();
00118 }
00119
00120 const osl::RepetitionCounter& osl::game_playing::
00121 GameState::counter() const
00122 {
00123 return stack->counter;
00124 }
00125
00126 bool osl::game_playing::
00127 GameState::canPopMove() const
00128 {
00129 return ! stack->move_stack.empty();
00130 }
00131
00132 const boost::shared_ptr<osl::game_playing::GameState> osl::game_playing::
00133 GameState::clone() const
00134 {
00135 boost::shared_ptr<GameState> result(new GameState(*stack));
00136 return result;
00137 }
00138
00139 const osl::state::SimpleState& osl::game_playing::
00140 GameState::getInitialState() const
00141 {
00142 return stack->initial_state;
00143 }
00144
00145
00146
00147
00148
00149