00001
00002
00003 #include "osl/eval/endgame/attackDefense.h"
00004 #include "osl/container/pieceValues.h"
00005
00006 void osl::eval::endgame::
00007 AttackDefense::setValues(const SimpleState& state, PieceValues& values)
00008 {
00009 values.fill(0);
00010
00011 const Piece king_black = state.getKingPiece(BLACK);
00012 const Piece king_white = state.getKingPiece(WHITE);
00013
00014 for (int i=0; i<Piece::SIZE; i++) {
00015 const Piece target = state.getPieceOf(i);
00016 values[i] = valueOf(king_black, king_white, target);
00017 }
00018 }
00019
00020 osl::eval::endgame::
00021 AttackDefense::AttackDefense(const SimpleState& state)
00022 {
00023 values.fill(0);
00024 const Piece king_black = state.getKingPiece(BLACK);
00025 const Piece king_white = state.getKingPiece(WHITE);
00026 for (int i=0; i<Piece::SIZE; i++) {
00027 const Piece target = state.getPieceOf(i);
00028 addValue(king_black, king_white, target);
00029 }
00030 }
00031
00032 void osl::eval::endgame::
00033 AttackDefense::update(const SimpleState& new_state, Move last_move)
00034 {
00035 if (last_move.isPass())
00036 return;
00037
00038 const Piece black_king = new_state.getKingPiece<BLACK>();
00039 const Piece white_king = new_state.getKingPiece<WHITE>();
00040 const Position to = last_move.to();
00041 const Player player = new_state.getTurn();
00042
00043 if (last_move.isDrop()) {
00044 assert(last_move.ptype() != KING);
00045 const int inc = valueOf(black_king, white_king, last_move.ptypeO(), to);
00046 const int dec = valueOf(black_king, white_king, last_move.ptypeO(),
00047 Position::STAND());
00048 addValue(player, inc - dec);
00049 return;
00050 }
00051 const Position from = last_move.from();
00052
00053 if (last_move.ptype() != KING) {
00054 const int inc = valueOf(black_king, white_king,
00055 last_move.ptypeO(), to);
00056 const int dec = valueOf(black_king, white_king,
00057 last_move.oldPtypeO(), from);
00058 addValue(player, inc - dec);
00059
00060 if (last_move.capturePtype() != PTYPE_EMPTY) {
00061 const int inc_capture
00062 = valueOf(black_king, white_king, captured(last_move.capturePtypeO()),
00063 Position::STAND());
00064 const int dec_capture
00065 = valueOf(black_king, white_king, last_move.capturePtypeO(), to);
00066 addValue(player, inc_capture);
00067 addValue(alt(player), -dec_capture);
00068 }
00069 return;
00070 }
00071
00072 reset();
00073
00074 for (int i=0; i<Piece::SIZE; i++) {
00075 const Piece target = new_state.getPieceOf(i);
00076 addValue(black_king, white_king, target);
00077 }
00078 }
00079
00080 void osl::eval::endgame::
00081 AttackDefense::updateKingMove(const SimpleState& state,
00082 Position from, Position to)
00083 {
00084 reset();
00085
00086 const Piece old_king = state.getPieceOnBoard(from);
00087 const Player player = old_king.owner();
00088 assert(old_king.ptype() == KING);
00089 const Piece new_king = Piece::makeKing(player, to);
00090
00091 const Piece king_black
00092 = (player == BLACK) ? new_king : state.getKingPiece(BLACK);
00093 const Piece king_white
00094 = (player == WHITE) ? new_king : state.getKingPiece(WHITE);
00095
00096 for (int i=0; i<Piece::SIZE; i++) {
00097 const Piece target = state.getPieceOf(i);
00098 if (target == old_king)
00099 addValue(king_black, king_white, new_king);
00100 else
00101 addValue(king_black, king_white, target);
00102 }
00103 }
00104
00105 void osl::eval::endgame::
00106 AttackDefense::updateKingMove(const SimpleState& state,
00107 Position from, Position to, Piece captured)
00108 {
00109 reset();
00110
00111 const Piece old_king = state.getPieceOnBoard(from);
00112 const Player player = old_king.owner();
00113 assert(old_king.ptype() == KING);
00114 const Piece new_king = Piece::makeKing(player, to);
00115
00116 const Piece king_black
00117 = (player == BLACK) ? new_king : state.getKingPiece(BLACK);
00118 const Piece king_white
00119 = (player == WHITE) ? new_king : state.getKingPiece(WHITE);
00120
00121 for (int i=0; i<Piece::SIZE; i++) {
00122 const Piece target = state.getPieceOf(i);
00123 if (target == old_king)
00124 addValue(king_black, king_white, new_king);
00125 else if (target == captured)
00126 addValue(king_black, king_white, captured.captured());
00127 else
00128 addValue(king_black, king_white, target);
00129 }
00130 }
00131
00132 int osl::eval::endgame::
00133 AttackDefense::expect(const SimpleState& state, Move move) const
00134 {
00135 const Piece black_king = state.getKingPiece<BLACK>();
00136 const Piece white_king = state.getKingPiece<WHITE>();
00137 const Position to = move.to();
00138 if (move.isDrop()) {
00139 const PtypeO ptypeO = move.ptypeO();
00140 assert(getPtype(ptypeO) != KING);
00141 const int inc = valueOf(black_king, white_king, ptypeO, to);
00142 const int dec = valueOf(black_king, white_king, ptypeO,
00143 Position::STAND());
00144 return value() + inc - dec;
00145 }
00146 const Position from = move.from();
00147 const Piece old_piece = state.getPieceOnBoard(from);
00148 const PtypeO new_ptypeo = move.ptypeO();
00149 if (old_piece.ptype() == KING) {
00150 AttackDefense new_eval = *this;
00151 if (move.capturePtype() == PTYPE_EMPTY)
00152 new_eval.updateKingMove(state, from, to);
00153 else
00154 new_eval.updateKingMove(state, from, to, state.getPieceOnBoard(to));
00155 return new_eval.value();
00156 }
00157 const int inc = valueOf(black_king, white_king, new_ptypeo, to);
00158 const int dec = valueOf(black_king, white_king, old_piece.ptypeO(), from);
00159 if (move.capturePtype() == PTYPE_EMPTY)
00160 return value() + inc - dec;
00161 const int inc_capture
00162 = valueOf(black_king, white_king, captured(move.capturePtypeO()),
00163 Position::STAND());
00164 const int dec_capture
00165 = valueOf(black_king, white_king, move.capturePtypeO(), to);
00166 return value() + inc - dec + inc_capture - dec_capture;
00167 }
00168
00169
00170
00171
00172
00173