My Project
basic_type.cc
Go to the documentation of this file.
1#include "osl/basic_type.h"
4#include "osl/simpleState.h"
6// #include "move-phash.c"
7#include <iostream>
8
9bool osl::isValid(Player player)
10{
11 return player==BLACK || player==WHITE;
12}
13
14std::ostream& osl::operator<<(std::ostream& os,Player player)
15{
16 if(player==BLACK)
17 return os << "+";
18 else
19 return os << "-";
20}
21
22
23
25{
26 return static_cast<int>(ptype)>=PTYPE_MIN
27 && static_cast<int>(ptype)<=PTYPE_MAX;
28}
29
30bool osl::isValidPtypeO(int ptypeO)
31{
32 return (ptypeO >= PTYPEO_MIN) && (ptypeO <= PTYPEO_MAX);
33}
34
35std::istream& osl::operator>>(std::istream& is, osl::Ptype& ptype)
36{
37 std::string s;
38 is >> s;
39 if (s == "PTYPE_EMPTY")
40 ptype = PTYPE_EMPTY;
41 else if (s == "PTYPE_EDGE")
42 ptype = PTYPE_EDGE;
43 else if (s == "PPAWN")
44 ptype = PPAWN;
45 else if (s == "PLANCE")
46 ptype = PLANCE;
47 else if (s == "PKNIGHT")
48 ptype = PKNIGHT;
49 else if (s == "PSILVER")
50 ptype = PSILVER;
51 else if (s == "PBISHOP")
52 ptype = PBISHOP;
53 else if (s == "PROOK")
54 ptype = PROOK;
55 else if (s == "KING")
56 ptype = KING;
57 else if (s == "GOLD")
58 ptype = GOLD;
59 else if (s == "PAWN")
60 ptype = PAWN;
61 else if (s == "LANCE")
62 ptype = LANCE;
63 else if (s == "KNIGHT")
64 ptype = KNIGHT;
65 else if (s == "SILVER")
66 ptype = SILVER;
67 else if (s == "BISHOP")
68 ptype = BISHOP;
69 else if (s == "ROOK")
70 ptype = ROOK;
71 else{
72 std::cerr << "Incorrect input : " << s << std::endl;
73 ptype = PTYPE_EMPTY;
74 }
75 return is;
76}
77
78std::ostream& osl::operator<<(std::ostream& os,const osl::Ptype ptype)
79{
80 return os << Ptype_Table.getName(ptype);
81}
82
83std::ostream& osl::operator<<(std::ostream& os,const osl::PtypeO ptypeO)
84{
85 if (isPiece(ptypeO))
86 return os << "PtypeO(" << getOwner(ptypeO) << ","
87 << getPtype(ptypeO) << ")";
88 return os << "PtypeO(" << (int)ptypeO << "," << getPtype(ptypeO) << ")";
89}
90
91
92
94 return DIRECTION_MIN<=d && d<=DIRECTION_MAX;
95}
96
97std::ostream& osl::operator<<(std::ostream& os,const Direction d){
98 static const char* names[]={
99 "UL","U","UR","L",
100 "R","DL","D","DR",
101 "UUL","UUR","LONG_UL",
102 "LONG_U","LONG_UR","LONG_L",
103 "LONG_R","LONG_DL","LONG_D","LONG_DR"
104 };
105 return os << names[static_cast<int>(d)];
106}
107
108#define OFFSET_INDEX(dx,dy) ((dx*BOARD_HEIGHT + dy) - OFFSET_MIN)
109
111{
112 *this = Board_Table.getOffset(player, direction);
113}
114
120{
121 switch (index())
122 {
123 case OFFSET_INDEX(-1,-2): return -1;
124 case OFFSET_INDEX(1,-2): return 1;
125 case OFFSET_INDEX(-1,-1): return -1;
126 case OFFSET_INDEX(0,-1): return 0;
127 case OFFSET_INDEX(1,-1): return 1;
128 case OFFSET_INDEX(-1,0): return -1;
129 case OFFSET_INDEX(1,0): return 1;
130 case OFFSET_INDEX(-1,1): return -1;
131 case OFFSET_INDEX(0,1): return 0;
132 case OFFSET_INDEX(1,1): return 1;
133 case OFFSET_INDEX(-1,2): return -1;
134 case OFFSET_INDEX(1,2): return 1;
135 default:
136 std::cerr << index() << " " << ZERO().index() << "\n";
137 assert(0);
138 }
139 return 0;
140}
141
147{
148 switch (index())
149 {
150 case OFFSET_INDEX(-1,-2): return -2;
151 case OFFSET_INDEX(1,-2): return -2;
152 case OFFSET_INDEX(-1,-1): return -1;
153 case OFFSET_INDEX(0,-1): return -1;
154 case OFFSET_INDEX(1,-1): return -1;
155 case OFFSET_INDEX(-1,0): return 0;
156 case OFFSET_INDEX(1,0): return 0;
157 case OFFSET_INDEX(-1,1): return 1;
158 case OFFSET_INDEX(0,1): return 1;
159 case OFFSET_INDEX(1,1): return 1;
160 case OFFSET_INDEX(-1,2): return 2;
161 case OFFSET_INDEX(1,2): return 2;
162 default: assert(0);
163 }
164 return 0;
165}
166
167#ifndef MINIMAL
168std::ostream& osl::operator<<(std::ostream& os, Offset offset)
169{
170 return os << "offset(" << offset.intValue() << ')';
171}
172#endif
173
174
175
176static_assert(sizeof(osl::Square) == 4, "square size");
177
179{
180 return (1<=x() && x() <=9
181 && 1<=y() && y() <=9);
182}
183
185{
186 return isPieceStand() || isOnBoard();
187}
188
189
192{
193 return Board_Table.nextSquare(P, *this, D);
194}
195
198{
199 return Board_Table.nextSquare(alt(P), *this, D);
200}
201
203 return (*this != to)
204 && (to == *this+Board_Table.getShortOffsetNotKnight(Offset32(to,*this)));
205}
206
207std::ostream& osl::operator<<(std::ostream& os, Square square)
208{
209 if (square.isPieceStand())
210 return os << "OFF";
211 return os << "Square(" << square.x() << square.y() << ")";
212}
213
214static_assert(sizeof(osl::Piece) == 4, "piece size");
215
216std::ostream& osl::operator<<(std::ostream& os,const Piece piece)
217{
218 if (piece.isPiece())
219 os << "Piece(" << piece.owner() << "," << piece.ptype()
220 << ",num=" << piece.number()
221 << "," << piece.square() << ')';
222 else if (piece == Piece::EMPTY())
223 os << "PIECE_EMPTY";
224 else if (piece == Piece::EDGE())
225 os << "PIECE_EDGE";
226 else
227 os << "unknown piece?!";
228 return os;
229}
230
232{
233 const int number = ((owner == BLACK)
236 return Piece(owner, KING, number, position);
237}
238
239
240
241namespace osl
242{
243 static_assert(sizeof(Move) == 4, "move size");
244} //namespace osl
245
247{
248 if (! isNormal())
249 return false;
250 const Square from = this->from();
251 if (! from.isValid())
252 return false;
253 const Square to = this->to();
254 if (! to.isOnBoard())
255 return false;
256 return osl::isValid(ptype())
257 && osl::isValid(capturePtype())
258 && capturePtype()!=KING
259 && osl::isValid(player());
260}
261
263{
264 if (isPass())
265 return Move::PASS(alt(player()));
266 if (! isNormal())
267 return *this;
268 return Move(from().rotate180Safe(), to().rotate180(), ptype(),
269 capturePtype(), isPromotion(), alt(player()));
270}
271
272std::ostream& osl::operator<<(std::ostream& os,const Move move)
273{
274 if (move == Move::DeclareWin())
275 return os << "MOVE_DECLARE_WIN";
276 if (move.isInvalid())
277 return os << "MOVE_INVALID";
278 if (move.isPass())
279 return os << "MOVE_PASS";
280 const Player turn = move.player();
281 if (move.isValid())
282 {
283 if (move.from().isPieceStand())
284 {
285 os << "Drop(" << turn << "," << move.ptype() << "," << move.to() << ")";
286 }
287 else
288 {
289 const Ptype capture_ptype=move.capturePtype();
290 os << "Move(" << turn << "," << move.ptype() << ","
291 << move.from() << "->" << move.to() ;
292 if (move.promoteMask())
293 os << ",promote";
294 if (capture_ptype != PTYPE_EMPTY)
295 os << ",capture=" << capture_ptype;
296 os << ")";
297 }
298 }
299 else
300 {
301 os << "InvalidMove " << move.from() << " " << move.to()
302 << " " << move.ptypeO() << " " << move.oldPtypeO()
303 << " " << move.promoteMask()
304 << " " << move.capturePtype() << "\n";
305 }
306 return os;
307}
308
309unsigned int osl::Move::hash() const
310{
311 assert(capturePtype() == PTYPE_EMPTY);
312 // return move_phash(intValue());
313 return intValue();
314}
315
317fromMove16(Move16 move16, const SimpleState& state)
318{
319 if (move16==MOVE16_NONE)
320 return Move();
321 Player turn=state.turn();
322 Square to=SquareCompressor::melt((move16>>8)&0x7f);
323 if((move16&0x80)!=0){
324 Ptype ptype=(Ptype)(move16-0x80);
325 return Move(to,ptype,turn);
326 }
327 Square from=SquareCompressor::melt(move16&0x7f);
328 Ptype ptype=state[from].ptype();
329 Ptype capture_ptype=state[to].ptype();
330 bool is_promote=(move16&0x8000)!=0;
331 if(is_promote)
332 return Move(from,to,::osl::promote(ptype),capture_ptype,true,turn);
333 else
334 return Move(from,to,ptype,capture_ptype,false,turn);
335}
337{
338 if (isInvalid())
339 return MOVE16_NONE;
340 if (isDrop())
341 return Move16(0x80+(uint16_t)ptype()+((SquareCompressor::compress(to()))<<8));
342 if (isPromotion())
343 return Move16(SquareCompressor::compress(from())+(SquareCompressor::compress(to())<<8)+0x8000);
345}
346
347
348// ;;; Local Variables:
349// ;;; mode:c++
350// ;;; c-basic-offset:2
351// ;;; End:
#define OFFSET_INDEX(dx, dy)
const Offset getOffset(Direction dir) const
Definition boardTable.h:47
const Offset getShortOffsetNotKnight(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Knight以外のShortの利きのoffsetの場合はそれ自身を返す.
Definition boardTable.h:119
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition boardTable.h:61
圧縮していない moveの表現 .
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
static const Move PASS(Player P)
bool isValid() const
bool isInvalid() const
state に apply 可能でない場合にtrue
Ptype ptype() const
Player player() const
PtypeO oldPtypeO() const
移動前のPtypeO, i.e., 成る手だった場合成る前
Move16 toMove16() const
static const Move DeclareWin()
bool isPass() const
Ptype capturePtype() const
static const Move fromMove16(Move16, const SimpleState &)
int promoteMask() const
pieceに使うためのmaskなので
const Move rotate180() const
unsigned int hash() const
駒を取らない手を [0, 16305] にmap
const Square to() const
const Square from() const
座標の差分
Definition basic_type.h:430
int intValue() const
Definition basic_type.h:448
int dx() const
Offsetから一般に dxは求まらないので, ここでの入力は12近傍のみとする
int dy() const
Offsetから一般に dyは求まらないので, ここでの入力は12近傍のみとする
Ptype ptype() const
Definition basic_type.h:821
const Square square() const
Definition basic_type.h:832
Player owner() const
Definition basic_type.h:963
bool isPiece() const
Definition basic_type.h:953
int number() const
Definition basic_type.h:828
static const Piece EMPTY()
Definition basic_type.h:797
static const Piece makeKing(Player owner, Square square)
玉を作る
static const Piece EDGE()
Definition basic_type.h:798
const char * getName(Ptype ptype) const
Definition ptypeTable.h:76
Player turn() const
bool isPieceStand() const
Definition basic_type.h:576
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
bool isNeighboring8(Square to) const
bool isOnBoardSlow() const
const Square neighbor() const
Definition basic_type.h:746
const Square back() const
Definition basic_type.h:750
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する.
Definition basic_type.h:583
bool isValid() const
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
@ PTYPE_MAX
Definition basic_type.h:105
@ ROOK
Definition basic_type.h:100
@ PPAWN
Definition basic_type.h:87
@ BISHOP
Definition basic_type.h:99
@ PROOK
Definition basic_type.h:92
@ PAWN
Definition basic_type.h:95
@ PTYPE_EDGE
Definition basic_type.h:86
@ KING
Definition basic_type.h:93
@ KNIGHT
Definition basic_type.h:97
@ PTYPE_EMPTY
Definition basic_type.h:85
@ SILVER
Definition basic_type.h:98
@ PKNIGHT
Definition basic_type.h:89
@ GOLD
Definition basic_type.h:94
@ PLANCE
Definition basic_type.h:88
@ PBISHOP
Definition basic_type.h:91
@ LANCE
Definition basic_type.h:96
@ PTYPE_MIN
Definition basic_type.h:102
@ PSILVER
Definition basic_type.h:90
const PtypeTable Ptype_Table
Definition tables.cc:97
Move16
16bit 表現
@ MOVE16_NONE
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
bool isValidPtypeO(int ptypeO)
Definition basic_type.cc:30
const BoardTable Board_Table
Definition tables.cc:95
Player getOwner(PtypeO ptypeO)
Definition basic_type.h:256
bool isValid(Player player)
cast等で作られたplayerが正しいかどうかを返す
Definition basic_type.cc:9
Direction
Definition basic_type.h:310
@ DIRECTION_MAX
Definition basic_type.h:337
@ DIRECTION_MIN
Definition basic_type.h:334
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
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
@ PTYPEO_MAX
Definition basic_type.h:201
@ PTYPEO_MIN
Definition basic_type.h:200
std::istream & operator>>(std::istream &is, Ptype &ptype)
Definition basic_type.cc:35
constexpr Player alt(Player player)
Definition basic_type.h:13
Ptype promote(Ptype ptype)
promote可能なptypeに対して,promote後の型を返す promote不可のptypeを与えてはいけない.
Definition basic_type.h:173
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
static int compress(Square pos)
static Square melt(int index)