All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
simpleState.h
Go to the documentation of this file.
1 /* simpleState.h
2  */
3 #ifndef OSL_SIMPLE_STATE_H
4 #define OSL_SIMPLE_STATE_H
5 
6 #include "osl/misc/loki.h"
7 #include "osl/direction.h"
8 #include "osl/boardTable.h"
9 #include "osl/ptype.h"
10 #include "osl/ptypeTraits.h"
11 #include "osl/piece.h"
13 #include "osl/container/bitXmask.h"
14 #include "osl/effectContent.h"
15 #include "osl/move.h"
16 #include "osl/player.h"
17 #include "osl/handicap.h"
18 #include "osl/misc/carray.h"
20 #include "osl/ptypeTable.h"
21 
22 #include <iosfwd>
23 
24 namespace osl
25 {
26  namespace state
27  {
28  class SimpleState;
29  std::ostream& operator<<(std::ostream& os,const SimpleState& state);
35  bool operator==(const SimpleState& st1,const SimpleState& st2);
36 
38  {
39  private:
40  friend std::ostream& operator<<(std::ostream& os,const SimpleState& state);
41  friend bool operator==(const SimpleState& st1,const SimpleState& st2);
43  public:
44  static const bool hasPawnMask=true;
45  protected:
46  CArray<Piece,Square::SIZE> board
47 #ifdef __GNUC__
48  __attribute__((aligned(16)))
49 #endif
50  ;
54  CArray<Piece,Piece::SIZE> pieces
55 #ifdef __GNUC__
56  __attribute__((aligned(16)))
57 #endif
58  ;
59  CArray<PieceMask,2> stand_mask;
60  CArray<BitXmask,2> pawnMask;
61  CArray<CArray<char,PTYPE_SIZE-PTYPE_BASIC_MIN>,2> stand_count;
62 
65  PieceMask used_mask;
66  public:
67  // 生成に関するもの
68  explicit SimpleState();
69  explicit SimpleState(Handicap h);
70  // public継承させるには,virtual destructorを定義する.
71  virtual ~SimpleState();
73  void init();
75  void init(Handicap h);
76  // private:
77  void initPawnMask();
78  public:
79  const Piece pieceOf(int num) const{
80  return pieces[num];
81  }
82  void setPieceOf(int num,Piece p) {
83  pieces[num]=p;
84  }
85  template<Player P>
86  const Piece kingPiece() const{
88  }
89  const Piece kingPiece(Player P) const{
90  assert(isValid(P));
91  if (P==BLACK)
92  return kingPiece<BLACK>();
93  else
94  return kingPiece<WHITE>();
95  }
96  template<Player P>
97  Square kingSquare() const{
98  return kingPiece<P>().square();
99  }
100  Square kingSquare(Player player) const{
101  assert(isValid(player));
102  if (player==BLACK)
103  return kingSquare<BLACK>();
104  else
105  return kingSquare<WHITE>();
106  }
107  template <Ptype PTYPE>
108  static int nthLimit() {
110  }
116  template <Ptype PTYPE>
117  const Piece nth(int n) const {
118  assert(0 <= n && n < nthLimit<PTYPE>());
120  }
121 
122  void setBoard(Square sq,Piece piece)
123  {
124  board[sq.index()]=piece;
125  }
126  protected:
127  PieceMask& standMask(Player p) {
128  return stand_mask[p];
129  }
130  public:
131  const PieceMask& standMask(Player p) const {
132  return stand_mask[p];
133  }
134  const PieceMask& usedMask() const {return used_mask;}
135  bool isOffBoard(int num) const{
136  return standMask(BLACK).test(num)
137  || standMask(WHITE).test(num);
138  }
139  // protected:
141  void clearPawn(Player pl,Square sq){
142  pawnMask[pl].clear(sq);
143  }
145  void setPawn(Player pl,Square sq){
146  pawnMask[pl].set(sq);
147  }
148  public:
149  bool isPawnMaskSet(Player player, int x) const
150  {
151  return pawnMask[player].isSet(x);
152  }
153 
154  template<Player P>
155  bool isPawnMaskSet(int x)const {return isPawnMaskSet(P,x); }
156 
158  bool canDropPawnTo(Player player, int x) const
159  {
160  return hasPieceOnStand<PAWN>(player) && ! isPawnMaskSet(player, x);
161  }
162 
163  void setPiece(Player player,Square sq,Ptype ptype);
164  void setPieceAll(Player player);
165 
170  const Piece pieceAt(Square sq) const { return board[sq.index()];}
171  const Piece operator[](Square sq) const { return pieceAt(sq);}
172  const Piece* getPiecePtr(Square sq) const { return &board[sq.index()];}
173  const Piece pieceOnBoard(Square sq) const
174  {
175  assert(sq.isOnBoard());
176  return pieceAt(sq);
177  }
178 
179  bool isOnBoard(int num) const {
180  return pieceOf(num).isOnBoard();
181  }
185  int countPiecesOnStand(Player pl,Ptype ptype) const {
186  assert(isBasic(ptype));
187  return stand_count[pl][ptype-PTYPE_BASIC_MIN];
188  }
190  template <Ptype Type>
191  int countPiecesOnStand(Player pl) const {
192  return countPiecesOnStand(pl, Type);
193  }
194  bool hasPieceOnStand(Player player,Ptype ptype) const{
195  return countPiecesOnStand(player, ptype)!=0;
196  }
197  template<Ptype T>
198  bool hasPieceOnStand(Player P) const {
199  return countPiecesOnStand(P, T);
200  }
201  private:
202  int countPiecesOnStandBit(Player pl,Ptype ptype) const {
203  return (standMask(pl).getMask(Ptype_Table.getIndex(ptype))
204  & Ptype_Table.getMaskLow(ptype)).countBit();
205  }
206  public:
211  Piece nextPiece(Square cur, Offset diff) const
212  {
213  assert(! diff.zero());
214  cur += diff;
215  while (pieceAt(cur) == Piece::EMPTY())
216  cur += diff;
217  return pieceAt(cur);
218  }
219 
220  void setTurn(Player player) {
221  player_to_move=player;
222  }
223  Player turn() const{
224  return player_to_move;
225  }
229  void changeTurn() {
231  }
232  // check
233  bool isConsistent(bool show_error=true) const;
235  template <bool show_error>
236  bool isAlmostValidMove(Move move) const;
244  bool isAlmostValidMove(Move move,bool show_error=true) const;
251  bool isValidMove(Move move,bool show_error=true) const;
252  protected:
253  template <bool show_error> bool isAlmostValidDrop(Move move) const;
254  template <bool show_error> bool testValidityOtherThanEffect(Move move) const;
255  public:
260  static bool isValidMoveByRule(Move move,bool show_error);
261 
270  bool isEmptyBetween(Square from, Square to,Offset offset,bool pieceExistsAtTo=false) const
271 #ifdef __GNUC__
272  __attribute__ ((pure))
273 #endif
274  {
275  assert(from.isOnBoard());
276  assert(! offset.zero());
277  assert(offset==Board_Table.getShortOffset(Offset32(to,from)));
278  Square sq=from+offset;
279  for (; pieceAt(sq).isEmpty(); sq+=offset) {
280  if (!pieceExistsAtTo && sq==to)
281  return true;
282  }
283  return sq==to;
284 
285  }
292  bool
293 #ifdef __GNUC__
294  __attribute__ ((pure))
295 #endif
296  isEmptyBetween(Square from, Square to,bool noSpaceAtTo=false) const{
297  assert(from.isOnBoard());
298  Offset offset=Board_Table.getShortOffset(Offset32(to,from));
299  assert(! offset.zero());
300  return isEmptyBetween(from,to,offset,noSpaceAtTo);
301  }
302 
304  bool dump() const;
308  const SimpleState emulateCapture(Piece from, Player new_owner) const;
309 
313  const SimpleState emulateHandPiece(Player from, Player to, Ptype ptype) const;
314  const SimpleState rotate180() const;
315  const SimpleState flipHorizontal() const;
316  };
317  } // namespace state
318  using state::SimpleState;
319 
320 } // namespace osl
321 
322 #endif /* OSL_SIMPLE_STATE_H */
323 // ;;; Local Variables:
324 // ;;; mode:c++
325 // ;;; c-basic-offset:2
326 // ;;; End: