All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
king8Info.h
Go to the documentation of this file.
1 /* king8Info.h
2  */
3 #ifndef OSL_CHECKMATE_KING8INFO_H
4 #define OSL_CHECKMATE_KING8INFO_H
5 
7 #include "osl/misc/bitOp.h"
9 #include <boost/cstdint.hpp>
10 #include <iosfwd>
11 namespace osl
12 {
13  namespace checkmate
14  {
30  class King8Info
31  {
32  uint64_t value;
33  public:
34  explicit King8Info(uint64_t v) : value(v)
35  {
36  }
37 
38  template<Player P>
39  static const King8Info make(NumEffectState const& state,Square king, PieceMask pinned);
40  template<Player P>
41  static const King8Info make(NumEffectState const& state,Square king);
42 
44  static const King8Info make(Player attack, NumEffectState const& state);
46  static const King8Info makeWithPin(Player attack, NumEffectState const& state,
47  const PieceMask& pinned);
48  uint64_t uint64Value() const { return value; }
49 
51  unsigned int dropCandidate() const
52  {
53  return (unsigned int)(value&0xffull);
54  }
56  unsigned int liberty() const
57  {
58  return (unsigned int)((value>>8)&0xffull);
59  }
61  unsigned int libertyDropMask() const
62  {
63  return (unsigned int)(value&0xffffull);
64  }
66  unsigned int libertyCandidate() const
67  {
68  return (unsigned int)((value>>16)&0xffull);
69  }
71  unsigned int moveCandidate2() const
72  {
73  return (unsigned int)((value>>24)&0xffull);
74  }
75  unsigned int spaces() const
76  {
77  return (unsigned int)((value>>32)&0xffull);
78  }
79  unsigned int moves() const
80  {
81  return (unsigned int)((value>>40)&0xffull);
82  }
84  unsigned int libertyCount() const
85  {
86  return (unsigned int)((value>>48)&0xfull);
87  }
88  template<Player P,Direction Dir>
89  unsigned int moveCandidateDir(NumEffectState const& state,Square target) const{
90  if((value & (1ull<<(24+Dir)))==0) return 0;
92  if(state.countEffect(P,pos)<2 &&
93  !effect_util::AdditionalEffect::hasEffect(state,pos,P)) return 0;
94  return 1;
95  }
96  template<Player P>
97  unsigned int countMoveCandidate(NumEffectState const& state) const
98  {
100  Square king=state.kingSquare<altP>();
101  return moveCandidateDir<P,UL>(state,king)+
102  moveCandidateDir<P,U>(state,king)+
103  moveCandidateDir<P,UR>(state,king)+
104  moveCandidateDir<P,L>(state,king)+
105  moveCandidateDir<P,R>(state,king)+
106  moveCandidateDir<P,DL>(state,king)+
107  moveCandidateDir<P,D>(state,king)+
108  moveCandidateDir<P,DR>(state,king);
109  }
110  unsigned int countMoveCandidate(Player player, NumEffectState const& state) const
111  {
112  if(player==BLACK) return countMoveCandidate<BLACK>(state);
113  else return countMoveCandidate<WHITE>(state);
114  }
115  template<Player P>
116  unsigned int moveCandidateMask(NumEffectState const& state) const
117  {
119  Square king=state.kingSquare<altP>();
120  return (moveCandidateDir<P,UL>(state,king)<<UL)+
121  (moveCandidateDir<P,U>(state,king)<<U)+
122  (moveCandidateDir<P,UR>(state,king)<<UR)+
123  (moveCandidateDir<P,L>(state,king)<<L)+
124  (moveCandidateDir<P,R>(state,king)<<R)+
125  (moveCandidateDir<P,DL>(state,king)<<DL)+
126  (moveCandidateDir<P,D>(state,king)<<D)+
127  (moveCandidateDir<P,DR>(state,king)<<DR);
128  }
129  template<Player P>
130  bool hasMoveCandidate(NumEffectState const& state) const
131  {
133  Square king=state.kingSquare<altP>();
134  if(moveCandidateDir<P,U>(state,king)!=0) return true;
135  if(moveCandidateDir<P,UL>(state,king)!=0) return true;
136  if(moveCandidateDir<P,UR>(state,king)!=0) return true;
137  if(moveCandidateDir<P,L>(state,king)!=0) return true;
138  if(moveCandidateDir<P,R>(state,king)!=0) return true;
139  if(moveCandidateDir<P,D>(state,king)!=0) return true;
140  if(moveCandidateDir<P,DL>(state,king)!=0) return true;
141  if(moveCandidateDir<P,DR>(state,king)!=0) return true;
142  return false;
143  }
144  private:
152  template<Player P,Direction Dir>
153  static uint64_t
154 #ifdef __GNUC__
155  __attribute__ ((pure))
156 #endif
157  hasEffectMask(NumEffectState const& state,Square target, PieceMask pinned,
158  PieceMask on_board_defense);
159  };
160 
161  class EdgeTable
162  {
163  CArray2d<uint64_t, 2, Square::SIZE> edge_mask;
164  public:
165  EdgeTable();
167  const King8Info
168 #ifdef __GNUC__
169  __attribute__ ((pure))
170 #endif
171  resetEdgeFromLiberty(Player king_player, Square king, King8Info info) const
172  {
173  uint64_t ret = info.uint64Value();
174  ret &= edge_mask[king_player][king.index()];
175  const uint64_t count = misc::BitOp::countBit((ret>>8)&0xffull);
176  ret |= count << 48;
177  return King8Info(ret);
178  }
179  };
180  extern const EdgeTable Edge_Table;
181 
182  std::ostream& operator<<(std::ostream&, King8Info);
183  } // namespace checkmate
184  using checkmate::King8Info;
185 } // namespace osl
186 
187 #endif /* OSL_CHECKMATE_KING8INFO_H */
188 // ;;; Local Variables:
189 // ;;; mode:c++
190 // ;;; c-basic-offset:2
191 // ;;; End:
192