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/move_generator/legalMoves.h"
00008 #include "osl/effect_util/effectUtil.h"
00009 #include "osl/state/historyState.h"
00010 #include "osl/hash/hashKeyStack.h"
00011 #include "osl/container/moveStack.h"
00012 #include "osl/misc/align16New.h"
00013 #include "osl/repetitionCounter.h"
00014 #include "osl/sennichite.h"
00015 #include <boost/foreach.hpp>
00016
00017 struct osl::game_playing::GameState::State
00018 #if OSL_WORDSIZE == 32
00019 : public osl::misc::Align16New
00020 #endif
00021 {
00022 HistoryState state;
00023 RepetitionCounter counter;
00024 MoveStack move_history;
00025 vector<int> eval_stack;
00026
00027 State(const SimpleState& initial_state)
00028 : state(initial_state), counter(state.state())
00029 {
00030 move_history.reserve(1024);
00031 }
00032 };
00033
00034 osl::game_playing::
00035 GameState::GameState(const SimpleState& initial_state)
00036 : stack(new State(initial_state))
00037 {
00038 }
00039
00040 osl::game_playing::
00041 GameState::GameState(const State& src)
00042 : stack(new State(src))
00043 {
00044 }
00045
00046 osl::game_playing::
00047 GameState::~GameState()
00048 {
00049 }
00050
00051 const osl::Sennichite osl::game_playing::
00052 GameState::pushMove(Move m, int eval)
00053 {
00054 stack->move_history.push(m);
00055 const Sennichite result
00056 = stack->counter.isSennichite(state(), m);
00057 stack->counter.push(state(), m);
00058 stack->state.makeMove(m);
00059 stack->eval_stack.push_back(eval);
00060 return result;
00061 }
00062
00063 osl::game_playing::GameState::MoveType osl::game_playing::
00064 GameState::isIllegal(Move m) const
00065 {
00066 if (! state().isValidMove(m, false))
00067 return OTHER_INVALID;
00068 typedef move_classifier::PlayerMoveAdaptor<move_classifier::PawnDropCheckmate>
00069 PawnDropCheckmate_t;
00070 if (PawnDropCheckmate_t::isMember(state(), m))
00071 return PAWN_DROP_FOUL;
00072
00073 stack->state.makeMove(m);
00074 const bool unsafe_king = state().inCheck(alt(state().getTurn()));
00075 stack->state.unmakeMove();
00076
00077 if (unsafe_king)
00078 return UNSAFE_KING;
00079
00080 return VALID;
00081 }
00082
00083 const osl::Move osl::game_playing::
00084 GameState::popMove()
00085 {
00086 const Move result = stack->move_history.lastMove();
00087 assert(canPopMove());
00088 stack->move_history.pop();
00089 stack->counter.pop();
00090 stack->state.unmakeMove();
00091 stack->eval_stack.pop_back();
00092 return result;
00093 }
00094
00095 const osl::NumEffectState& osl::game_playing::
00096 GameState::state() const
00097 {
00098 return stack->state.state();
00099 }
00100
00101 int osl::game_playing::
00102 GameState::moves() const
00103 {
00104 return stack->move_history.size();
00105 }
00106
00107 const osl::MoveStack& osl::game_playing::
00108 GameState::moveHistory() const
00109 {
00110 return stack->move_history;
00111 }
00112
00113 const osl::hash::HashKeyStack& osl::game_playing::
00114 GameState::hashHistory() const
00115 {
00116 return stack->counter.history();
00117 }
00118
00119 const osl::RepetitionCounter& osl::game_playing::
00120 GameState::counter() const
00121 {
00122 return stack->counter;
00123 }
00124
00125 bool osl::game_playing::
00126 GameState::canPopMove() const
00127 {
00128 return ! stack->state.empty();
00129 }
00130
00131 const boost::shared_ptr<osl::game_playing::GameState> osl::game_playing::
00132 GameState::clone() const
00133 {
00134 boost::shared_ptr<GameState> result(new GameState(*stack));
00135 return result;
00136 }
00137
00138 const osl::state::SimpleState& osl::game_playing::
00139 GameState::getInitialState() const
00140 {
00141 return stack->state.initialState();
00142 }
00143
00144 const osl::vector<int>& osl::game_playing::
00145 GameState::evalStack() const
00146 {
00147 return stack->eval_stack;
00148 }
00149
00150 void osl::game_playing::
00151 GameState::generateNotLosingMoves(MoveVector& normal_or_win_or_draw,
00152 MoveVector& loss) const
00153 {
00154 MoveVector all;
00155 LegalMoves::generate(state(), all);
00156 BOOST_FOREACH(Move m, all) {
00157 if (isIllegal(m) != VALID) {
00158 loss.push_back(m);
00159 continue;
00160 }
00161 const Sennichite result
00162 = counter().isSennichite(state(), m);
00163 if (result.hasWinner()
00164 && result.winner() == alt(state().getTurn())) {
00165 loss.push_back(m);
00166 continue;
00167 }
00168 normal_or_win_or_draw.push_back(m);
00169 }
00170 }
00171
00172
00173
00174
00175
00176