File: proofNumberTable.cc

package info (click to toggle)
libosl 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 273,976 kB
  • sloc: cpp: 129,625; ansic: 7,145; ruby: 1,290; makefile: 558; perl: 413; sh: 35
file content (368 lines) | stat: -rw-r--r-- 10,941 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* proofNumberTable.cc
 */
#include "osl/checkmate/proofNumberTable.h"
#include "osl/checkmate/immediateCheckmate.h"
#include "osl/effect_util/neighboring8Direct.h"
#include "osl/boardTable.h"
#include "osl/ptypeTable.h"

namespace
{
  using namespace osl;
  using namespace osl::checkmate;
  /**
   * @return 0 は王手でない場合.それ以外は逃げ方の見積
   */
  const osl::checkmate::ProofNumberTable::Liberty
  effectiveCheckShort(Ptype ptype,Direction dir,unsigned int mask)
  {
    assert(isShort(dir));
    // 王は8近傍に移動できない
    if (ptype==KING) 
      return ProofNumberTable::Liberty(0,false);
    // ptypeがdir方向に利きを持たない場合も空王手の場合があるので作成
    const bool has_effect 
      = (Ptype_Table.getMoveMask(ptype)
	 & (dirToMask(dir) | dirToMask(shortToLong(dir))));
    int dx=Board_Table.getDxForBlack(dir);
    int dy=Board_Table.getDyForBlack(dir);
    int count = 0;
    for (int l=0;l<8;l++) {
      if ((mask&(1<<l))==0) 
	continue;
      Direction dir1=static_cast<Direction>(l);
      int dx1=Board_Table.getDxForBlack(dir1);
      int dy1=Board_Table.getDyForBlack(dir1);
      Offset32 o32(dx-dx1,dy-dy1);
      if ((dx != dx1 || dy != dy1)
	  && !Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect())
	++count;
    }
    return ProofNumberTable::Liberty(std::max(count,1), has_effect);
  }
  const osl::checkmate::ProofNumberTable::Liberty
  effectiveCheckLong(Ptype ptype,Direction dir,unsigned int mask)
  {
    assert(isLong(dir));
    // ptypeがdir方向に利きを持たなくても2マス離れた空王手で8近傍に
    // 利きが増える場合があるので作成
    const bool has_effect 
      = (Ptype_Table.getMoveMask(ptype) & dirToMask(dir));
    int dx=Board_Table.getDxForBlack(dir)*2; // 1マス遠くから
    int dy=Board_Table.getDyForBlack(dir)*2;
    int count = 0;
    for (int l=0;l<8;l++) {
      if ((mask&(1<<l))==0) 
	continue;
      Direction dir1=static_cast<Direction>(l);
      int dx1=Board_Table.getDxForBlack(dir1);
      int dy1=Board_Table.getDyForBlack(dir1);
      Offset32 o32(dx-dx1,dy-dy1);
      if (!Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect())
	++count;
    }
    return ProofNumberTable::Liberty(std::max(count,1), has_effect);
  }
}

osl::checkmate::
ProofNumberTable::Table::Table()
{
}

osl::checkmate::
ProofNumberTable::ProofNumberTable()
  : table(new Table())
{
  // liberties
  for (int i=0; i<0x100; i++) {
    for (int k=PTYPE_PIECE_MIN; k<=PTYPE_MAX; k++) {
      const Ptype ptype=static_cast<Ptype>(k);
      assert(isPiece(ptype));
      for (int j=0; j<8; j++) {
	Direction dir=static_cast<Direction>(j);
	const Liberty e = effectiveCheckShort(ptype,dir,i);
	table->liberties[i][k][j] = e;
      }
      int longs = 0;
      for (int j=LONG_DIRECTION_MIN; j<=LONG_DIRECTION_MAX; ++j,++longs) {
	Direction dir=static_cast<Direction>(j);
	const Liberty e = effectiveCheckLong(ptype,dir,i);
	table->liberties[i][k][j] = e;
      }
      assert(longs == 8);
    }
  }
  // drop_liberty
  table->drop_liberty.fill(0);
  for(int i=0;i<0x10000;i++){
    const unsigned int liberty = (i>>8)&0xff;
    const int liberty_count = misc::BitOp::countBit(liberty);
    if (liberty_count <= 2)
      continue;			// low enough
    
    for (int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++) {
      int minimum_liberty = liberty_count;
      Ptype ptype=static_cast<Ptype>(k);
      if (ptype == KING)
	continue;
      for (int j=0;j<8;j++) {
	// 有効王手でない
	if ((i&(0x1<<j))==0) 
	  continue;
	if ((i&(0x100<<j))!=0)
	  continue;
	const Direction dir=static_cast<Direction>(j);
	// ptypeがdir方向に利きを持つか
	const bool has_effect 
	  = (Ptype_Table.getMoveMask(ptype)
	     & (dirToMask(dir) | dirToMask(shortToLong(dir))));
	if (! has_effect)
	  continue;
	const int e = table->liberties[liberty][k][j].liberty;
	assert(e);
	minimum_liberty = std::min(minimum_liberty, e);
      }
      for (int l=minimum_liberty; l<liberty_count; ++l)
      {
	table->drop_liberty[i][l] |= (1<<(ptype-GOLD));
      }
    }
  }
  // pmajor_liberty
  table->pmajor_liberty.fill(8);
  for (int l=0; l<0x100; l++) {	// liberty
    for (int m=0; m<0x100; m++) {	// move_mask
      if (l & m)
	continue;
      int min_liberty = std::max(2,misc::BitOp::countBit(l))-1;
      if (min_liberty > 1)
      {
	for (int j=0; j<8; j++) {
	  if ((m&(0x1<<j))==0)
	    continue;
	  const int pr = table->liberties[l][PROOK][j].liberty;
	  const int pb = table->liberties[l][PBISHOP][j].liberty;
	  min_liberty = std::min(min_liberty, std::min(pr,pb));
	  assert(min_liberty);
	}
      }
      table->pmajor_liberty[l][m] = min_liberty;
    }
  }
  // promote_liberty
  table->promote_liberty.fill(8);
  for (int l=0; l<0x100; l++) {	// liberty
    for (int m=0; m<0x100; m++) {	// move_mask
      if (l & m)
	continue;
      int min_liberty = std::max(2,misc::BitOp::countBit(l))-1;
      if (min_liberty > 1)
      {
	for (int j=0; j<8; j++) {
	  if ((m&(0x1<<j))==0)
	    continue;
	  for (int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++) {
	    Ptype ptype=static_cast<Ptype>(k);
	    if (ptype == KING || ptype == PROOK || ptype == PBISHOP)
	      continue;
	    Liberty e = table->liberties[l][k][j];
	    if (! e.has_effect)
	      continue;
	    assert(e.liberty); 
	    min_liberty = std::min(min_liberty, (int)e.liberty);
	    assert(min_liberty);
	  }
	}
      }
      table->promote_liberty[l][m] = min_liberty;
    }
  }
  // other_move_liberty
  table->other_move_liberty.fill(8);
  for (int l=0; l<0x100; l++) {	// liberty
    for (int m=0; m<0x100; m++) {	// move_mask
      if (l & m)
	continue;
      int min_liberty = std::max(2,misc::BitOp::countBit(l))-1;
      if (min_liberty > 1)
      {
	for (int j=0; j<8; j++) {
	  if ((m&(0x1<<j))==0)
	    continue;
	  for (int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++) {
	    Ptype ptype=static_cast<Ptype>(k);
	    if (ptype == KING || ptype == PROOK || ptype == PBISHOP)
	      continue;
	    if (j == U 
		&& (ptype == GOLD || ptype == PPAWN || ptype == PLANCE
		    || ptype == PKNIGHT || ptype == PSILVER))
	      continue;
	    Liberty e = table->liberties[l][k][j];
	    if (! e.has_effect)
	      continue;
	    assert(e.liberty); 
	    min_liberty = std::min(min_liberty, (int)e.liberty);
	    assert(min_liberty);
	  }
	}
      }
      table->other_move_liberty[l][m] = min_liberty;
    }
  }
}

int osl::checkmate::
ProofNumberTable::countLiberty(const NumEffectState& state, Move move) const
{
  const Player attack = move.player();
  const Square king = state.kingSquare(alt(attack));
  const King8Info info(state.Iking8Info(alt(attack)));
  return countLiberty(state, info.libertyCount(), move, king, info);
}

int osl::checkmate::
ProofNumberTable::libertyAfterAllDrop(const NumEffectState& state, Player attack,
				      King8Info info) const
{
  assert(state.turn() == attack);
  int result = info.libertyCount()-1;
  if (result < 2)
    return 1;
  const unsigned int ld_mask = info.libertyDropMask();
  uint8_t ptype_mask = 0;
  for (int p=GOLD; p<=ROOK; ++p)
    ptype_mask |= state.hasPieceOnStand(attack, static_cast<Ptype>(p)) << (p-GOLD);
  for (; 
       result > 1
	 && (ptype_mask & table->drop_liberty[ld_mask][result-1]);
       --result)
  {
  }
  return result;	
}

int osl::checkmate::
ProofNumberTable::libertyAfterAllDrop(const NumEffectState& state) const
{
  const Player attack = state.turn();
  const King8Info info(state.Iking8Info(alt(attack)));
  return libertyAfterAllDrop(state, attack, info);
}

int osl::checkmate::
ProofNumberTable::libertyAfterAllMove(const NumEffectState& state,
				      Player attack,
				      King8Info info, Square king) const
{
  bool has_pmajor = false;
  {
    for (int i = PtypeTraits<BISHOP>::indexMin;
	 i < PtypeTraits<ROOK>::indexLimit; i++)
    {
      // move maskを見て8マス調べる方が良いか?
      const Piece p = state.pieceOf(i);
      assert(isMajor(p.ptype()));
      if (! p.isOnBoardByOwner(attack))
	continue;
      if (king.squareForBlack(attack).y() > 3 // 1段おまけ
	  && ! isPromoted(p.ptype()))
      {
	if (! p.square().canPromote(attack))
	  continue;
      }
      if (Neighboring8Direct::hasEffect(state, p.ptypeO(), p.square(),
					king))
      {
	// 本当はそこにbitが立っているかを判定したい.
	has_pmajor = true;
	break;
      }
    }
  }
  int moveCandidate;
  if(attack==BLACK)
    moveCandidate = info.moveCandidateMask<BLACK>(state);
  else
    moveCandidate = info.moveCandidateMask<WHITE>(state);
  if (has_pmajor)
  {
    int result = table->pmajor_liberty[info.liberty()][moveCandidate];
    assert(result);
    return result;
  }
  bool promoted_area = king.squareForBlack(attack).y() < 3;
  if (! promoted_area)
  {
    const Square u = king + Board_Table.getOffset(alt(attack), U);
    promoted_area = state.hasEffectByPtype<GOLD>(attack, u);
  }
  if (promoted_area)
  {
    int result = table->promote_liberty[info.liberty()][moveCandidate];
    assert(result);
    return result;
  }
  int result = table->other_move_liberty[info.liberty()][moveCandidate];
  assert(result);
  return result;
}

int osl::checkmate::
ProofNumberTable::libertyAfterAllMove(const NumEffectState& state) const
{
  const Player attack = state.turn();
  const Square king = state.kingSquare(alt(attack));
  const King8Info info(state.Iking8Info(alt(attack)));
  return libertyAfterAllMove(state, attack, info, king);
}

int osl::checkmate::
ProofNumberTable::disproofAfterAllCheck(const NumEffectState& state, 
					Player attack,
					King8Info info) const
{
  int num_checks;
  num_checks=info.countMoveCandidate(attack,state);
  int drop_scale = state.hasPieceOnStand<GOLD>(attack) 
    + state.hasPieceOnStand<SILVER>(attack);  
  if (drop_scale)
    num_checks += misc::BitOp::countBit(info.dropCandidate()) * drop_scale;
  return std::max(1, num_checks);
}

const osl::checkmate::ProofDisproof osl::checkmate::
ProofNumberTable::attackEstimation(const NumEffectState& state,
				   Player attack,
				   King8Info info, Square king) const
{
  int p = libertyAfterAllDrop(state, attack, info);
  if (p >= 2)
  {
    p = std::min(p, libertyAfterAllMove(state, attack, info, king));
  }
  return ProofDisproof(p, disproofAfterAllCheck(state, attack, info));
}

const osl::checkmate::ProofDisproof osl::checkmate::
ProofNumberTable::attackEstimation(const NumEffectState& state) const
{
  const Player attack = state.turn();
  const Square king = state.kingSquare(alt(attack));
  const King8Info info(state.Iking8Info(alt(attack)));
  return attackEstimation(state, attack, info, king);
}

int osl::checkmate::
 ProofNumberTable::libertyAfterAllCheck(const NumEffectState& state) const
{
  return attackEstimation(state).proof();
}

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