-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmove.h
146 lines (121 loc) · 3.6 KB
/
move.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright (C) 2015 Matthew Lai
Giraffe is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Giraffe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MOVE_H
#define MOVE_H
#include <cassert>
#include "types.h"
#include "containers.h"
// what's the maximum number of possible moves from any position?
// 216 seems to be a popular number
static const size_t MAX_LEGAL_MOVES = 256;
typedef uint32_t Move;
// fields
// [3:0] = PieceType
// [9:4] = From
// [15:10] = To
// [19:16] = Castling flags
// [23:20] = promotion piecetype
namespace MoveConstants
{
const static uint32_t PIECE_TYPE_SHIFT = 0;
const static uint32_t PIECE_TYPE_MASK = 0xF;
const static uint32_t FROM_SHIFT = 4;
const static uint32_t FROM_MASK = 0x3F;
const static uint32_t TO_SHIFT = 10;
const static uint32_t TO_MASK = 0x3F;
const static uint32_t PROMO_SHIFT = 20;
const static uint32_t PROMO_MASK = 0xF;
// castling flags are NOT shifted!
const static uint32_t CASTLE_WHITE_LONG = 1 << 19;
const static uint32_t CASTLE_WHITE_SHORT = 1 << 18;
const static uint32_t CASTLE_BLACK_LONG = 1 << 17;
const static uint32_t CASTLE_BLACK_SHORT = 1 << 16;
const static uint32_t CASTLE_MASK = CASTLE_WHITE_LONG | CASTLE_WHITE_SHORT | CASTLE_BLACK_LONG | CASTLE_BLACK_SHORT;
}
inline PieceType GetPieceType(Move mv)
{
return (mv >> MoveConstants::PIECE_TYPE_SHIFT) & MoveConstants::PIECE_TYPE_MASK;
}
inline void SetPieceType(Move &mv, PieceType pt)
{
#ifdef DEBUG
assert((pt & ~MoveConstants::PIECE_TYPE_MASK) == 0);
assert(GetPieceType(mv) == 0);
#endif
mv |= pt << MoveConstants::PIECE_TYPE_SHIFT;
}
inline Square GetFromSquare(Move mv)
{
return (mv >> MoveConstants::FROM_SHIFT) & MoveConstants::FROM_MASK;
}
inline void SetFromSquare(Move &mv, Square sq)
{
#ifdef DEBUG
assert((sq & ~MoveConstants::FROM_MASK) == 0);
assert(GetFromSquare(mv) == 0);
#endif
mv |= sq << MoveConstants::FROM_SHIFT;
}
inline Square GetToSquare(Move mv)
{
return (mv >> MoveConstants::TO_SHIFT) & MoveConstants::TO_MASK;
}
inline void SetToSquare(Move &mv, Square sq)
{
#ifdef DEBUG
assert((sq & ~MoveConstants::TO_MASK) == 0);
assert(GetToSquare(mv) == 0);
#endif
mv |= sq << MoveConstants::TO_SHIFT;
}
// 0 means no promotion (0 is the piece type for white king)
inline PieceType GetPromoType(Move mv)
{
return (mv >> MoveConstants::PROMO_SHIFT) & MoveConstants::PROMO_MASK;
}
inline bool IsPromotion(Move mv)
{
return GetPromoType(mv) != 0;
}
inline void SetPromoType(Move &mv, PieceType pt)
{
#ifdef DEBUG
assert((pt & ~MoveConstants::PROMO_MASK) == 0);
assert(GetPromoType(mv) == 0);
#endif
mv |= pt << MoveConstants::PROMO_SHIFT;
}
inline bool IsCastling(Move mv)
{
return mv & MoveConstants::CASTLE_MASK;
}
inline uint32_t GetCastlingType(Move mv)
{
#ifdef DEBUG
assert(IsCastling(mv));
#endif
return mv & MoveConstants::CASTLE_MASK;
}
// type should be one of the castling masks
inline void SetCastlingType(Move &mv, uint32_t type)
{
#ifdef DEBUG
assert((type & ~MoveConstants::CASTLE_MASK) == 0);
assert((type & MoveConstants::CASTLE_MASK) != 0);
assert(!IsCastling(mv));
#endif
mv |= type;
}
typedef FixedVector<Move, MAX_LEGAL_MOVES> MoveList;
#endif // MOVE_H