My Project
immediateCheckmateTable.cc
Go to the documentation of this file.
1 /* immediateCheckmateTable.cc
2  */
4 #include "osl/bits/boardTable.h"
5 #include "osl/bits/ptypeTable.h"
6 namespace
7 {
8  bool canCheckmate(osl::Ptype ptype,osl::Direction dir,unsigned int mask)
9  {
10  // 王はdropできない, 打ち歩詰め
11  if(ptype==osl::KING || ptype==osl::PAWN) return false;
12  // ptypeがdir方向に利きを持たない == 王手をかけられない
13  if(!(osl::Ptype_Table.getMoveMask(ptype)&
14  (osl::dirToMask(dir) | osl::dirToMask(osl::shortToLong(dir))))) return false;
15  int dx=osl::Board_Table.getDxForBlack(dir);
16  int dy=osl::Board_Table.getDyForBlack(dir);
17  for(int l=0;l<8;l++){
18  if((mask&(1<<l))==0) continue;
19  osl::Direction dir1=static_cast<osl::Direction>(l);
20  int dx1=osl::Board_Table.getDxForBlack(dir1);
21  int dy1=osl::Board_Table.getDyForBlack(dir1);
22  osl::Offset32 o32(dx-dx1,dy-dy1);
23  if(!osl::Ptype_Table.getEffect(osl::newPtypeO(osl::BLACK,ptype),o32).hasEffect())
24  return false;
25  }
26  return true;
27  }
28 }
29 
31 {
32  // ptypeDropMaskの初期化
33  for(int i=0;i<0x100;i++){
34  for(int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++){
35  unsigned char mask=0;
36  Ptype ptype=static_cast<Ptype>(k);
37  for(int j=0;j<8;j++){
38  // 玉の逃げ道がある
39  if((i&(0x1<<j))!=0)continue;
40  Direction dir=static_cast<Direction>(j);
41  if(canCheckmate(ptype,dir,i))
42  mask|=(1<<j);
43  }
44  ptypeDropMasks[i][ptype]=mask;
45  }
46  }
47  // dropPtypeMaskの初期化
48  for(int i=0;i<0x10000;i++){
49  unsigned char ptypeMask=0;
50  for(int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++){
51  Ptype ptype=static_cast<Ptype>(k);
52  for(int j=0;j<8;j++){
53  // 空白でない
54  if((i&(0x1<<j))==0) continue;
55  // 玉の逃げ道がある
56  if((i&(0x100<<j))!=0)continue;
57  Direction dir=static_cast<Direction>(j);
58  if(canCheckmate(ptype,dir,(i>>8)&0xff)){
59  ptypeMask|=1u<<(k-PTYPE_BASIC_MIN);
60  goto nextPtype;
61  }
62  }
63  nextPtype:;
64  }
65  dropPtypeMasks[i]=ptypeMask;
66  }
67  // blockingMaskの初期化
68  for(int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++){
69  Ptype ptype=static_cast<Ptype>(k);
70  for(int j=0;j<8;j++){
71  unsigned int mask=0;
72  Direction dir=static_cast<Direction>(j);
73  if(Ptype_Table.getMoveMask(ptype)&
74  (dirToMask(dir) | dirToMask(shortToLong(dir)))){
75  int dx=Board_Table.getDxForBlack(dir);
76  int dy=Board_Table.getDyForBlack(dir);
77  for(int l=0;l<8;l++){
78  Direction dir1=static_cast<Direction>(l);
79  int dx1=Board_Table.getDxForBlack(dir1);
80  int dy1=Board_Table.getDyForBlack(dir1);
81  Offset32 o32(dx-dx1,dy-dy1);
82  if(!Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect()){
84  !(dx==-dx1 && dy==-dy1)
85  ){
86  mask|=1<<l;
87  }
88  }
89  }
90  }
91  blockingMasks[ptype][dir]=mask;
92  }
93  }
94  // effectMaskの初期化
95  for(int k=PTYPE_PIECE_MIN;k<=PTYPE_MAX;k++){
96  Ptype ptype=static_cast<Ptype>(k);
97  for(int j=0;j<8;j++){
98  unsigned int mask=0x1ff;
99  Direction dir=static_cast<Direction>(j);
100  if(Ptype_Table.getMoveMask(ptype)&
101  (dirToMask(dir) | dirToMask(shortToLong(dir)))){ // 王手をかけられる
102  mask=0;
103  int dx=Board_Table.getDxForBlack(dir);
104  int dy=Board_Table.getDyForBlack(dir);
105  for(int l=0;l<8;l++){
106  Direction dir1=static_cast<Direction>(l);
107  int dx1=Board_Table.getDxForBlack(dir1);
108  int dy1=Board_Table.getDyForBlack(dir1);
109  Offset32 o32(dx-dx1,dy-dy1);
110  if(dir!= dir1 &&
111  !Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect()){
112  mask|=1<<l;
113  }
114  }
115  }
116  noEffectMasks[ptype][dir]=mask;
117  }
118  }
119 }
120 
121 
122 
123 /* ------------------------------------------------------------------------- */
124 // ;;; Local Variables:
125 // ;;; mode:c++
126 // ;;; c-basic-offset:2
127 // ;;; End:
128 
129 
int getDxForBlack(Direction dir) const
Definition: boardTable.h:40
const Offset getShortOffsetNotKnight(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Knight以外のShortの利きのoffsetの場合はそれ自身を返す.
Definition: boardTable.h:119
int getDyForBlack(Direction dir) const
Definition: boardTable.h:43
差が uniqになるような座標の差分.
Definition: offset32.h:17
bool zero() const
Definition: basic_type.h:502
const EffectContent getEffect(PtypeO ptypeo, Square from, Square to) const
fromにいるptypeoがtoに利きを持つか?
Definition: ptypeTable.h:112
int getMoveMask(Ptype ptype) const
Definition: ptypeTable.h:84
CArray2d< unsigned short, PTYPE_SIZE, 8 > noEffectMasks
CArray< unsigned char, 0x10000u > dropPtypeMasks
CArray2d< unsigned char, 0x100u, PTYPE_SIZE > ptypeDropMasks
CArray2d< unsigned char, PTYPE_SIZE, 8 > blockingMasks
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ PTYPE_PIECE_MIN
Definition: basic_type.h:104
@ PTYPE_MAX
Definition: basic_type.h:105
@ PAWN
Definition: basic_type.h:95
@ KING
Definition: basic_type.h:93
@ PTYPE_BASIC_MIN
Definition: basic_type.h:103
const PtypeTable Ptype_Table
Definition: tables.cc:97
constexpr int dirToMask(Direction dir)
Definition: basic_type.h:393
const BoardTable Board_Table
Definition: tables.cc:95
Direction
Definition: basic_type.h:310
@ BLACK
Definition: basic_type.h:9
constexpr Direction shortToLong(Direction d)
引数に longDirを与えてはいけない
Definition: basic_type.h:388
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211