All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
proofNumberTable.h
Go to the documentation of this file.
1 /* proofNumberTable.h
2  */
3 #ifndef OSL_CHECKMATE_PROOF_NUMBER_TABLE_H
4 #define OSL_CHECKMATE_PROOF_NUMBER_TABLE_H
9 #include "osl/boardTable.h"
10 #include "osl/neighboring8.h"
11 #include "osl/misc/carray.h"
12 #include "osl/misc/carray2d.h"
13 #include "osl/misc/bitOp.h"
14 #include "osl/misc/cstdint.h"
15 #include <boost/scoped_ptr.hpp>
16 
17 namespace osl
18 {
19  namespace checkmate
20  {
22  {
23  public:
24  struct Liberty
25  {
27  uint8_t liberty;
29  bool has_effect;
30  explicit Liberty(uint8_t l=0, bool e=false) : liberty(l), has_effect(e)
31  {
32  }
33  };
34  private:
35  struct Table
36  {
39  CArray2d<CArray<Liberty,DIRECTION_SIZE>,0x100u,PTYPE_SIZE> liberties;
43  CArray2d<uint8_t,0x10000u,8> drop_liberty;
45  CArray2d<uint8_t,0x100u,0x100u> pmajor_liberty;
49  CArray2d<uint8_t,0x100u,0x100u> promote_liberty;
51  CArray2d<uint8_t,0x100u,0x100u> other_move_liberty;
52 
53  Table();
54  };
55  boost::scoped_ptr<Table> table;
56  public:
58 
62  const Liberty countLiberty(Ptype ptype, Direction d, unsigned int liberty_mask) const
63  {
64  assert((d != UUL) && (d != UUR));
65  assert(liberty_mask <= 0xff);
66  return (table->liberties)[liberty_mask][ptype][d];
67  }
75  Square king, King8Info info) const
76  {
77  assert(Neighboring8::isNeighboring8(to, king));
78  assert(ptype != KNIGHT);
79  const unsigned int liberty_mask = info.liberty();
80  const Direction d =
81  (player == BLACK)
82  ? Board_Table.getShort8<BLACK>(to, king)
83  : Board_Table.getShort8<WHITE>(to, king);
84  return countLiberty(ptype, d, liberty_mask);
85  }
86  const Liberty countLibertyLong(Player player, Square to, Ptype ptype,
87  Square king, King8Info info) const
88  {
89  assert(! Neighboring8::isNeighboring8(to, king));
90  const unsigned int liberty_mask = info.liberty();
91  const Offset32 offset32(king,to);
92  const Offset offset = Board_Table.getShortOffsetNotKnight(offset32);
93  if (offset.zero())
94  return Liberty(0, false);
95  if (to + offset + offset != king) // 2マス以上遠く
96  {
97  if (isMajor(ptype))
98  ptype = unpromote(ptype);
99  else if (ptype != LANCE)
100  return Liberty(0, false);
101  }
102  const Direction d =
103  (player == BLACK)
104  ? Board_Table.getLongDirection<BLACK>(offset32)
105  : Board_Table.getLongDirection<WHITE>(offset32);
106  assert(isLong(d));
107  return countLiberty(ptype, d, liberty_mask);
108  }
112  int countLiberty(const NumEffectState& state, int liberty_count,
113  Move move, Square king, King8Info info) const
114  {
115  assert(liberty_count == misc::BitOp::countBit(info.liberty()));
116  const Player attack = move.player();
117  const Player defense = alt(attack);
118  const Square to = move.to();
119  const Ptype ptype = move.ptype();
120  if (ptype == KNIGHT)
121  return std::max(1,liberty_count + state.countEffect(defense, to));
122 
123  const bool neighboring = Neighboring8::isNeighboring8(to, king);
124  Liberty liberty = neighboring
125  ? countLibertyShortNotKnight(attack, to, ptype, king, info)
126  : countLibertyLong(attack, to, ptype, king, info);
127  if (liberty.liberty == 0)
128  return std::max(liberty_count-1,1);
129  if (! neighboring && liberty.has_effect)
130  {
131  // TODO: 詰将棋と協調できるなら liberty.liberty <=
132  // liberty_count を保つように調整したい,が.
133  ++liberty.liberty; // 合駒の分,もし両王手の場合は本来は不要
134  }
135 
136  liberty.liberty += state.countEffect(defense, to);
137  if (move.isDrop())
138  {
139  if (neighboring)
140  {
141  if (state.countEffect(attack, to))
142  --liberty.liberty; // adjust king capture
143  }
144  assert(liberty.liberty);
145  return liberty.liberty;
146  }
147  // 移動: 銀のただすてなどは本当は利きをはずしたい
148  if (neighboring)
149  {
150  if (state.countEffect(attack, to) >= 2
151  || effect_util::AdditionalEffect::hasEffect(state, to, attack))
152  --liberty.liberty; // adjust king capture
153  }
154  assert(liberty.liberty);
155  return liberty.liberty;
156  }
158  int countLiberty(const NumEffectState& state, Move move) const;
159 
161  int
162 #ifdef __GNUC__
163  __attribute__ ((pure))
164 #endif
165  libertyAfterAllDrop(const NumEffectState& state) const;
166  int
167 #ifdef __GNUC__
168  __attribute__ ((pure))
169 #endif
170  libertyAfterAllDrop(const NumEffectState& state, Player attack,
171  King8Info info) const;
173  int
174 #ifdef __GNUC__
175  __attribute__ ((pure))
176 #endif
177  libertyAfterAllMove(const NumEffectState& state) const;
178  int
179 #ifdef __GNUC__
180  __attribute__ ((pure))
181 #endif
182  libertyAfterAllMove(const NumEffectState& state, Player attack,
183  King8Info info, Square king) const;
185  int
186 #ifdef __GNUC__
187  __attribute__ ((pure))
188 #endif
189  libertyAfterAllCheck(const NumEffectState& state) const;
190 
191  int
192 #ifdef __GNUC__
193  __attribute__ ((pure))
194 #endif
195  disproofAfterAllCheck(const NumEffectState&, Player, King8Info) const;
197  const ProofDisproof
198 #ifdef __GNUC__
199  __attribute__ ((pure))
200 #endif
201  attackEstimation(const NumEffectState& state) const;
202  const ProofDisproof
203 #ifdef __GNUC__
204  __attribute__ ((pure))
205 #endif
206  attackEstimation(const NumEffectState& state,
207  Player attack,
208  King8Info info, Square king) const;
209  };
211  }
212 }
213 
214 #endif /* _CHECKMATE_IMMEDIATE_CHECKMATE_TABLE_H */
215 // ;;; Local Variables:
216 // ;;; mode:c++
217 // ;;; c-basic-offset:2
218 // ;;; End:
219