My Project
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/basic_type.h"
10#include "osl/bits/pieceMask.h"
11#include "osl/bits/bitXmask.h"
13#include "osl/container.h"
14
15
16#include <iosfwd>
17
18namespace osl
19{
22 // KYOUOCHI,
23 // KAKUOCHI,
24 };
25 class SimpleState;
26 std::ostream& operator<<(std::ostream& os,const SimpleState& state);
32 bool operator==(const SimpleState& st1,const SimpleState& st2);
33
35 {
36 private:
37 friend std::ostream& operator<<(std::ostream& os,const SimpleState& state);
38 friend bool operator==(const SimpleState& st1,const SimpleState& st2);
40 public:
41 static const bool hasPawnMask=true;
42 protected:
44#ifdef __GNUC__
45 __attribute__((aligned(16)))
46#endif
47 ;
52#ifdef __GNUC__
53 __attribute__((aligned(16)))
54#endif
55 ;
59
63 public:
64 // 生成に関するもの
65 explicit SimpleState();
66 explicit SimpleState(Handicap h);
67 // public継承させるには,virtual destructorを定義する.
68 virtual ~SimpleState();
70 void init();
72 void init(Handicap h);
73 // private:
74 void initPawnMask();
75 public:
76 const Piece pieceOf(int num) const{
77 return pieces[num];
78 }
79 void setPieceOf(int num,Piece p) {
80 pieces[num]=p;
81 }
82 template<Player P>
83 const Piece kingPiece() const{
85 }
86 const Piece kingPiece(Player P) const{
87 assert(isValid(P));
88 if (P==BLACK)
89 return kingPiece<BLACK>();
90 else
91 return kingPiece<WHITE>();
92 }
93 template<Player P>
95 return kingPiece<P>().square();
96 }
97 Square kingSquare(Player player) const{
98 assert(isValid(player));
99 if (player==BLACK)
100 return kingSquare<BLACK>();
101 else
102 return kingSquare<WHITE>();
103 }
104 template <Ptype PTYPE>
113 template <Ptype PTYPE>
114 const Piece nth(int n) const {
115 assert(0 <= n && n < nthLimit<PTYPE>());
117 }
118
119 void setBoard(Square sq,Piece piece)
120 {
121 board[sq.index()]=piece;
122 }
123 protected:
125 return stand_mask[p];
126 }
127 public:
128 const PieceMask& standMask(Player p) const {
129 return stand_mask[p];
130 }
131 const PieceMask& usedMask() const {return used_mask;}
132 bool isOffBoard(int num) const{
133 return standMask(BLACK).test(num)
134 || standMask(WHITE).test(num);
135 }
136 // protected:
139 pawnMask[pl].clear(sq);
140 }
142 void setPawn(Player pl,Square sq){
143 pawnMask[pl].set(sq);
144 }
145 public:
146 bool isPawnMaskSet(Player player, int x) const
147 {
148 return pawnMask[player].isSet(x);
149 }
150
151 template<Player P>
152 bool isPawnMaskSet(int x)const {return isPawnMaskSet(P,x); }
153
155 bool canDropPawnTo(Player player, int x) const
156 {
157 return hasPieceOnStand<PAWN>(player) && ! isPawnMaskSet(player, x);
158 }
159
160 void setPiece(Player player,Square sq,Ptype ptype);
161 void setPieceAll(Player player);
162
167 const Piece pieceAt(Square sq) const { return board[sq.index()];}
168 const Piece operator[](Square sq) const { return pieceAt(sq);}
169 const Piece* getPiecePtr(Square sq) const { return &board[sq.index()];}
170 const Piece pieceOnBoard(Square sq) const
171 {
172 assert(sq.isOnBoard());
173 return pieceAt(sq);
174 }
175
176 bool isOnBoard(int num) const {
177 return pieceOf(num).isOnBoard();
178 }
182 int countPiecesOnStand(Player pl,Ptype ptype) const {
183 assert(isBasic(ptype));
184 return stand_count[pl][ptype-PTYPE_BASIC_MIN];
185 }
187 template <Ptype Type>
189 return countPiecesOnStand(pl, Type);
190 }
191 bool hasPieceOnStand(Player player,Ptype ptype) const{
192 return countPiecesOnStand(player, ptype)!=0;
193 }
194 template<Ptype T>
195 bool hasPieceOnStand(Player P) const {
196 return countPiecesOnStand(P, T);
197 }
198 private:
199 int countPiecesOnStandBit(Player pl,Ptype ptype) const {
200 return (standMask(pl).getMask(0)
201 & Ptype_Table.getMaskLow(ptype)).countBit();
202 }
203 public:
208 Piece nextPiece(Square cur, Offset diff) const
209 {
210 assert(! diff.zero());
211 cur += diff;
212 while (pieceAt(cur) == Piece::EMPTY())
213 cur += diff;
214 return pieceAt(cur);
215 }
216
217 void setTurn(Player player) {
218 player_to_move=player;
219 }
220 Player turn() const{
221 return player_to_move;
222 }
229 // check
230 bool isConsistent(bool show_error=true) const;
232 template <bool show_error>
233 bool isAlmostValidMove(Move move) const;
241 bool isAlmostValidMove(Move move,bool show_error=true) const;
248 bool isValidMove(Move move,bool show_error=true) const;
249 protected:
250 template <bool show_error> bool isAlmostValidDrop(Move move) const;
251 template <bool show_error> bool testValidityOtherThanEffect(Move move) const;
252 public:
257 static bool isValidMoveByRule(Move move,bool show_error);
258
267 bool isEmptyBetween(Square from, Square to,Offset offset,bool pieceExistsAtTo=false) const
268#ifdef __GNUC__
269 __attribute__ ((pure))
270#endif
271 {
272 assert(from.isOnBoard());
273 assert(! offset.zero());
274 assert(offset==Board_Table.getShortOffset(Offset32(to,from)));
275 Square sq=from+offset;
276 for (; pieceAt(sq).isEmpty(); sq+=offset) {
277 if (!pieceExistsAtTo && sq==to)
278 return true;
279 }
280 return sq==to;
281
282 }
289 bool
290#ifdef __GNUC__
291 __attribute__ ((pure))
292#endif
293 isEmptyBetween(Square from, Square to,bool noSpaceAtTo=false) const{
294 assert(from.isOnBoard());
295 Offset offset=Board_Table.getShortOffset(Offset32(to,from));
296 assert(! offset.zero());
297 return isEmptyBetween(from,to,offset,noSpaceAtTo);
298 }
299
301 bool dump() const;
305 const SimpleState emulateCapture(Piece from, Player new_owner) const;
306
310 const SimpleState emulateHandPiece(Player from, Player to, Ptype ptype) const;
311 const SimpleState rotate180() const;
312 const SimpleState flipHorizontal() const;
313 };
314
315} // namespace osl
316
317#endif /* OSL_SIMPLE_STATE_H */
318// ;;; Local Variables:
319// ;;; mode:c++
320// ;;; c-basic-offset:2
321// ;;; End:
const Offset getShortOffset(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Shortの利きのoffsetの場合はそれ自身を返す.
Definition boardTable.h:110
圧縮していない moveの表現 .
差が uniqになるような座標の差分.
Definition offset32.h:17
座標の差分
Definition basic_type.h:430
bool zero() const
Definition basic_type.h:502
駒番号のビットセット.
Definition pieceMask.h:21
bool test(int num) const
Definition pieceMask.h:45
bool isEmpty() const
Definition basic_type.h:913
static const Piece EMPTY()
Definition basic_type.h:797
bool isOnBoard() const
Definition basic_type.h:985
mask_t getMaskLow(Ptype ptype) const
Definition ptypeTable.h:46
CArray< BitXmask, 2 > pawnMask
Definition simpleState.h:57
Square kingSquare(Player player) const
Definition simpleState.h:97
Piece nextPiece(Square cur, Offset diff) const
diff方向にあるPiece を求める.
const Piece pieceOnBoard(Square sq) const
bool isValidMove(Move move, bool show_error=true) const
合法手かどうかを検査する. isValidMoveByRule, isAlmostValidMove をおこなう. 玉の素抜きや王手を防いでいるか, 千日手,打歩詰かどうかは検査しない.
bool isAlmostValidDrop(Move move) const
CArray< Piece, Square::SIZE > board
Definition simpleState.h:47
SimpleState state_t
Definition simpleState.h:39
Player player_to_move
手番
Definition simpleState.h:61
bool hasPieceOnStand(Player player, Ptype ptype) const
const SimpleState emulateHandPiece(Player from, Player to, Ptype ptype) const
from からto に ptypeの持駒を一枚渡した局面を作る.
int countPiecesOnStand(Player pl) const
後方互換
void setTurn(Player player)
const Piece nth(int n) const
unpromote(PTYPE)のn番目の駒を帰す.
bool hasPieceOnStand(Player P) const
static bool isValidMoveByRule(Move move, bool show_error)
盤面以外の部分の反則のチェック
CArray< CArray< char, PTYPE_SIZE-PTYPE_BASIC_MIN >, 2 > stand_count
Definition simpleState.h:58
PieceMask used_mask
Definition simpleState.h:62
void setPieceOf(int num, Piece p)
Definition simpleState.h:79
friend std::ostream & operator<<(std::ostream &os, const SimpleState &state)
void init()
盤面が空の状態に初期化
void clearPawn(Player pl, Square sq)
(internal)
PieceMask & standMask(Player p)
bool isOnBoard(int num) const
const Piece operator[](Square sq) const
const Piece kingPiece() const
Definition simpleState.h:83
const Piece * getPiecePtr(Square sq) const
const PieceMask & standMask(Player p) const
bool isPawnMaskSet(int x) const
Player turn() const
const SimpleState emulateCapture(Piece from, Player new_owner) const
from で表現されたPieceをnew_ownerの持駒にした局面を作る.
void setPieceAll(Player player)
virtual ~SimpleState()
void changeTurn()
手番を変更する
bool isEmptyBetween(Square from, Square to, bool noSpaceAtTo=false) const
bool canDropPawnTo(Player player, int x) const
xの筋に歩を打てる
friend bool operator==(const SimpleState &st1, const SimpleState &st2)
盤上の駒のみを比較する(持ち駒は見ない).
CArray< PieceMask, 2 > stand_mask
Definition simpleState.h:56
void setBoard(Square sq, Piece piece)
const Piece kingPiece(Player P) const
Definition simpleState.h:86
CArray< Piece, Piece::SIZE > pieces
全てのpieceが登録されている
Definition simpleState.h:55
const PieceMask & usedMask() const
bool testValidityOtherThanEffect(Move move) const
const Piece pieceOf(int num) const
Definition simpleState.h:76
bool isConsistent(bool show_error=true) const
const SimpleState flipHorizontal() const
bool isOffBoard(int num) const
void setPawn(Player pl, Square sq)
(internal)
int countPiecesOnStandBit(Player pl, Ptype ptype) const
bool isEmptyBetween(Square from, Square to, Offset offset, bool pieceExistsAtTo=false) const
static int nthLimit()
static const bool hasPawnMask
Definition simpleState.h:41
Square kingSquare() const
Definition simpleState.h:94
void setPiece(Player player, Square sq, Ptype ptype)
bool isPawnMaskSet(Player player, int x) const
int countPiecesOnStand(Player pl, Ptype ptype) const
持駒の枚数を数える
const Piece pieceAt(Square sq) const
const SimpleState rotate180() const
bool isAlmostValidMove(Move move) const
エラー表示をするかどうかをtemplateパラメータにした高速化版
bool dump() const
dump: 自分を cerr に表示する。abort 前などにデバッグに使う
unsigned int index() const
Definition basic_type.h:572
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する.
Definition basic_type.h:583
unsigned int square
Definition basic_type.h:533
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
@ PTYPE_BASIC_MIN
Definition basic_type.h:103
const PtypeTable Ptype_Table
Definition tables.cc:97
const int PTYPE_SIZE
Definition basic_type.h:107
const BoardTable Board_Table
Definition tables.cc:95
bool isValid(Player player)
cast等で作られたplayerが正しいかどうかを返す
Definition basic_type.cc:9
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
Player
Definition basic_type.h:8
@ WHITE
Definition basic_type.h:10
@ BLACK
Definition basic_type.h:9
const PtypeO PTYPEO_EDGE __attribute__((unused))
@ HIRATE
Definition simpleState.h:21
bool isBasic(Ptype ptype)
ptypeが基本型(promoteしていない)かのチェック
Definition basic_type.h:128
constexpr Player alt(Player player)
Definition basic_type.h:13
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
bool operator==(Square l, Square r)
Definition basic_type.h:758