attackDefense.cc
Go to the documentation of this file.
00001 /* attackDefense.cc
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.kingPiece(BLACK);
00012   const Piece king_white = state.kingPiece(WHITE);
00013   
00014   for (int i=0; i<Piece::SIZE; i++) {
00015     const Piece target = state.pieceOf(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.kingPiece(BLACK);
00025   const Piece king_white = state.kingPiece(WHITE);
00026   for (int i=0; i<Piece::SIZE; i++) {
00027     const Piece target = state.pieceOf(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.kingPiece<BLACK>();
00039   const Piece white_king = new_state.kingPiece<WHITE>();
00040   const Square to = last_move.to();
00041   const Player player = new_state.turn();
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                             Square::STAND());
00048     addValue(player, inc - dec);
00049     return;
00050   }
00051   const Square 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                   Square::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   // KING
00072   reset();
00073 
00074   for (int i=0; i<Piece::SIZE; i++) {
00075     const Piece target = new_state.pieceOf(i);
00076     addValue(black_king, white_king, target);
00077   }
00078 }
00079 
00080 void osl::eval::endgame::
00081 AttackDefense::updateKingMove(const SimpleState& state, 
00082                               Square from, Square to)
00083 {
00084   reset();
00085   
00086   const Piece old_king = state.pieceOnBoard(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.kingPiece(BLACK);
00093   const Piece king_white
00094     = (player == WHITE) ? new_king : state.kingPiece(WHITE);
00095 
00096   for (int i=0; i<Piece::SIZE; i++) {
00097     const Piece target = state.pieceOf(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                               Square from, Square to, Piece captured)
00108 {
00109   reset();
00110 
00111   const Piece old_king = state.pieceOnBoard(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.kingPiece(BLACK);
00118   const Piece king_white
00119     = (player == WHITE) ? new_king : state.kingPiece(WHITE);
00120 
00121   for (int i=0; i<Piece::SIZE; i++) {
00122     const Piece target = state.pieceOf(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.kingPiece<BLACK>();
00136   const Piece white_king = state.kingPiece<WHITE>();
00137   const Square 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                             Square::STAND());
00144     return value() + inc - dec;
00145   }
00146   const Square from = move.from();
00147   const Piece old_piece = state.pieceOnBoard(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.pieceOnBoard(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               Square::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 void osl::eval::endgame::
00170 AttackDefense::resetWeights(const int *w)
00171 {
00172   AttackKing::resetWeights(w);
00173   DefenseKing::resetWeights(w+KingPieceTable::dimension());
00174 }
00175 
00176 /* ------------------------------------------------------------------------- */
00177 // ;;; Local Variables:
00178 // ;;; mode:c++
00179 // ;;; c-basic-offset:2
00180 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines