File: attackDefense.cc

package info (click to toggle)
libosl 0.6.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 148,792 kB
  • ctags: 151,985
  • sloc: cpp: 131,133; ansic: 7,228; ruby: 1,290; makefile: 569; perl: 309; sh: 35
file content (180 lines) | stat: -rw-r--r-- 5,535 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* attackDefense.cc
 */
#include "osl/eval/endgame/attackDefense.h"
#include "osl/container/pieceValues.h"

void osl::eval::endgame::
AttackDefense::setValues(const SimpleState& state, PieceValues& values)
{
  values.fill(0);
  // 速度は無視
  const Piece king_black = state.kingPiece(BLACK);
  const Piece king_white = state.kingPiece(WHITE);
  
  for (int i=0; i<Piece::SIZE; i++) {
    const Piece target = state.pieceOf(i);
    values[i] = valueOf(king_black, king_white, target);
  }
}

osl::eval::endgame::
AttackDefense::AttackDefense(const SimpleState& state)
{
  values.fill(0);
  const Piece king_black = state.kingPiece(BLACK);
  const Piece king_white = state.kingPiece(WHITE);
  for (int i=0; i<Piece::SIZE; i++) {
    const Piece target = state.pieceOf(i);
    addValue(king_black, king_white, target);
  }
}

void osl::eval::endgame::
AttackDefense::update(const SimpleState& new_state, Move last_move)
{
  if (last_move.isPass())
    return;

  const Piece black_king = new_state.kingPiece<BLACK>();
  const Piece white_king = new_state.kingPiece<WHITE>();
  const Square to = last_move.to();
  const Player player = new_state.turn();

  if (last_move.isDrop()) {
    assert(last_move.ptype() != KING);
    const int inc = valueOf(black_king, white_king, last_move.ptypeO(), to);
    const int dec = valueOf(black_king, white_king, last_move.ptypeO(), 
			    Square::STAND());
    addValue(player, inc - dec);
    return;
  }
  const Square from = last_move.from();

  if (last_move.ptype() != KING) {
    const int inc = valueOf(black_king, white_king, 
			    last_move.ptypeO(), to);
    const int dec = valueOf(black_king, white_king, 
			    last_move.oldPtypeO(), from);
    addValue(player, inc - dec);

    if (last_move.capturePtype() != PTYPE_EMPTY) {
      const int inc_capture
	= valueOf(black_king, white_king, captured(last_move.capturePtypeO()), 
		  Square::STAND());
      const int dec_capture
	= valueOf(black_king, white_king, last_move.capturePtypeO(), to);
      addValue(player, inc_capture);
      addValue(alt(player), -dec_capture);
    }
    return;
  }
  // KING
  reset();

  for (int i=0; i<Piece::SIZE; i++) {
    const Piece target = new_state.pieceOf(i);
    addValue(black_king, white_king, target);
  }
}

void osl::eval::endgame::
AttackDefense::updateKingMove(const SimpleState& state, 
			      Square from, Square to)
{
  reset();
  
  const Piece old_king = state.pieceOnBoard(from);
  const Player player = old_king.owner();
  assert(old_king.ptype() == KING);
  const Piece new_king = Piece::makeKing(player, to);
  
  const Piece king_black
    = (player == BLACK) ? new_king : state.kingPiece(BLACK);
  const Piece king_white
    = (player == WHITE) ? new_king : state.kingPiece(WHITE);

  for (int i=0; i<Piece::SIZE; i++) {
    const Piece target = state.pieceOf(i);
    if (target == old_king)
      addValue(king_black, king_white, new_king);
    else
      addValue(king_black, king_white, target);
  }
}

void osl::eval::endgame::
AttackDefense::updateKingMove(const SimpleState& state, 
			      Square from, Square to, Piece captured)
{
  reset();

  const Piece old_king = state.pieceOnBoard(from);
  const Player player = old_king.owner();
  assert(old_king.ptype() == KING);
  const Piece new_king = Piece::makeKing(player, to);
  
  const Piece king_black
    = (player == BLACK) ? new_king : state.kingPiece(BLACK);
  const Piece king_white
    = (player == WHITE) ? new_king : state.kingPiece(WHITE);

  for (int i=0; i<Piece::SIZE; i++) {
    const Piece target = state.pieceOf(i);
    if (target == old_king)
      addValue(king_black, king_white, new_king);
    else if (target == captured)
      addValue(king_black, king_white, captured.captured());
    else 
      addValue(king_black, king_white, target);
  }
}

int osl::eval::endgame::
AttackDefense::expect(const SimpleState& state, Move move) const 
{
  const Piece black_king = state.kingPiece<BLACK>();
  const Piece white_king = state.kingPiece<WHITE>();
  const Square to = move.to();
  if (move.isDrop()) {
    const PtypeO ptypeO = move.ptypeO();
    assert(getPtype(ptypeO) != KING);
    const int inc = valueOf(black_king, white_king, ptypeO, to);
    const int dec = valueOf(black_king, white_king, ptypeO, 
			    Square::STAND());
    return value() + inc - dec;
  }
  const Square from = move.from();
  const Piece old_piece = state.pieceOnBoard(from);
  const PtypeO new_ptypeo = move.ptypeO();
  if (old_piece.ptype() == KING) {
    AttackDefense new_eval = *this;
    if (move.capturePtype() == PTYPE_EMPTY)
      new_eval.updateKingMove(state, from, to);
    else
      new_eval.updateKingMove(state, from, to, state.pieceOnBoard(to));
    return new_eval.value();
  }
  const int inc = valueOf(black_king, white_king, new_ptypeo, to);
  const int dec = valueOf(black_king, white_king, old_piece.ptypeO(), from);
  if (move.capturePtype() == PTYPE_EMPTY)
    return value() + inc - dec;
  const int inc_capture
    = valueOf(black_king, white_king, captured(move.capturePtypeO()), 
	      Square::STAND());
  const int dec_capture
    = valueOf(black_king, white_king, move.capturePtypeO(), to);
  return value() + inc - dec + inc_capture - dec_capture;
}

void osl::eval::endgame::
AttackDefense::resetWeights(const int *w)
{
  AttackKing::resetWeights(w);
  DefenseKing::resetWeights(w+KingPieceTable::dimension());
}

/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: