attackDefense.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
attackDefense.cc
Go to the documentation of this file.
1 /* attackDefense.cc
2 */
3 #include "osl/eval/endgame/attackDefense.h"
4 #include "osl/container/pieceValues.h"
5
6 void osl::eval::endgame::
7 AttackDefense::setValues(const SimpleState& state, PieceValues& values)
8 {
9 values.fill(0);
10 // 速度は無視
11 const Piece king_black = state.kingPiece(BLACK);
12 const Piece king_white = state.kingPiece(WHITE);
13
14 for (int i=0; i<Piece::SIZE; i++) {
15 const Piece target = state.pieceOf(i);
16 values[i] = valueOf(king_black, king_white, target);
17 }
18 }
19
20 osl::eval::endgame::
21 AttackDefense::AttackDefense(const SimpleState& state)
22 {
23 values.fill(0);
24 const Piece king_black = state.kingPiece(BLACK);
25 const Piece king_white = state.kingPiece(WHITE);
26 for (int i=0; i<Piece::SIZE; i++) {
27 const Piece target = state.pieceOf(i);
28 addValue(king_black, king_white, target);
29 }
30 }
31
32 void osl::eval::endgame::
33 AttackDefense::update(const SimpleState& new_state, Move last_move)
34 {
35 if (last_move.isPass())
36 return;
37
38 const Piece black_king = new_state.kingPiece<BLACK>();
39 const Piece white_king = new_state.kingPiece<WHITE>();
40 const Square to = last_move.to();
41 const Player player = new_state.turn();
42
43 if (last_move.isDrop()) {
44 assert(last_move.ptype() != KING);
45 const int inc = valueOf(black_king, white_king, last_move.ptypeO(), to);
46 const int dec = valueOf(black_king, white_king, last_move.ptypeO(),
47 Square::STAND());
48 addValue(player, inc - dec);
49 return;
50 }
51 const Square from = last_move.from();
52
53 if (last_move.ptype() != KING) {
54 const int inc = valueOf(black_king, white_king,
55 last_move.ptypeO(), to);
56 const int dec = valueOf(black_king, white_king,
57 last_move.oldPtypeO(), from);
58 addValue(player, inc - dec);
59
60 if (last_move.capturePtype() != PTYPE_EMPTY) {
61 const int inc_capture
62 = valueOf(black_king, white_king, captured(last_move.capturePtypeO()),
63 Square::STAND());
64 const int dec_capture
65 = valueOf(black_king, white_king, last_move.capturePtypeO(), to);
66 addValue(player, inc_capture);
67 addValue(alt(player), -dec_capture);
68 }
69 return;
70 }
71 // KING
72 reset();
73
74 for (int i=0; i<Piece::SIZE; i++) {
75 const Piece target = new_state.pieceOf(i);
76 addValue(black_king, white_king, target);
77 }
78 }
79
80 void osl::eval::endgame::
81 AttackDefense::updateKingMove(const SimpleState& state,
82 Square from, Square to)
83 {
84 reset();
85
86 const Piece old_king = state.pieceOnBoard(from);
87 const Player player = old_king.owner();
88 assert(old_king.ptype() == KING);
89 const Piece new_king = Piece::makeKing(player, to);
90
91 const Piece king_black
92 = (player == BLACK) ? new_king : state.kingPiece(BLACK);
93 const Piece king_white
94 = (player == WHITE) ? new_king : state.kingPiece(WHITE);
95
96 for (int i=0; i<Piece::SIZE; i++) {
97 const Piece target = state.pieceOf(i);
98 if (target == old_king)
99 addValue(king_black, king_white, new_king);
100 else
101 addValue(king_black, king_white, target);
102 }
103 }
104
105 void osl::eval::endgame::
106 AttackDefense::updateKingMove(const SimpleState& state,
107 Square from, Square to, Piece captured)
108 {
109 reset();
110
111 const Piece old_king = state.pieceOnBoard(from);
112 const Player player = old_king.owner();
113 assert(old_king.ptype() == KING);
114 const Piece new_king = Piece::makeKing(player, to);
115
116 const Piece king_black
117 = (player == BLACK) ? new_king : state.kingPiece(BLACK);
118 const Piece king_white
119 = (player == WHITE) ? new_king : state.kingPiece(WHITE);
120
121 for (int i=0; i<Piece::SIZE; i++) {
122 const Piece target = state.pieceOf(i);
123 if (target == old_king)
124 addValue(king_black, king_white, new_king);
125 else if (target == captured)
126 addValue(king_black, king_white, captured.captured());
127 else
128 addValue(king_black, king_white, target);
129 }
130 }
131
132 int osl::eval::endgame::
133 AttackDefense::expect(const SimpleState& state, Move move) const
134 {
135 const Piece black_king = state.kingPiece<BLACK>();
136 const Piece white_king = state.kingPiece<WHITE>();
137 const Square to = move.to();
138 if (move.isDrop()) {
139 const PtypeO ptypeO = move.ptypeO();
140 assert(getPtype(ptypeO) != KING);
141 const int inc = valueOf(black_king, white_king, ptypeO, to);
142 const int dec = valueOf(black_king, white_king, ptypeO,
143 Square::STAND());
144 return value() + inc - dec;
145 }
146 const Square from = move.from();
147 const Piece old_piece = state.pieceOnBoard(from);
148 const PtypeO new_ptypeo = move.ptypeO();
149 if (old_piece.ptype() == KING) {
150 AttackDefense new_eval = *this;
151 if (move.capturePtype() == PTYPE_EMPTY)
152 new_eval.updateKingMove(state, from, to);
153 else
154 new_eval.updateKingMove(state, from, to, state.pieceOnBoard(to));
155 return new_eval.value();
156 }
157 const int inc = valueOf(black_king, white_king, new_ptypeo, to);
158 const int dec = valueOf(black_king, white_king, old_piece.ptypeO(), from);
159 if (move.capturePtype() == PTYPE_EMPTY)
160 return value() + inc - dec;
161 const int inc_capture
162 = valueOf(black_king, white_king, captured(move.capturePtypeO()),
163 Square::STAND());
164 const int dec_capture
165 = valueOf(black_king, white_king, move.capturePtypeO(), to);
166 return value() + inc - dec + inc_capture - dec_capture;
167 }
168
169 void osl::eval::endgame::
170 AttackDefense::resetWeights(const int *w)
171 {
172 AttackKing::resetWeights(w);
173 DefenseKing::resetWeights(w+KingPieceTable::dimension());
174 }
175
176 /* ------------------------------------------------------------------------- */
177 // ;;; Local Variables:
178 // ;;; mode:c++
179 // ;;; c-basic-offset:2
180 // ;;; End: