-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboard.go
More file actions
294 lines (266 loc) · 6.24 KB
/
board.go
File metadata and controls
294 lines (266 loc) · 6.24 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package chessongo
import (
"math/rand"
"sync"
)
// Castling permissions
const (
CASTLE_WKS = 1 //White king side castling 0001
CASTLE_WQS = 2 //White queen side castling 0010
CASTLE_BKS = 4 //Black king side castling 0100
CASTLE_BQS = 8 //Black queen side castling 1000
)
// castling squares
const (
W_KING_INIT_SQUARE = 60 // e1
B_KING_INIT_SQUARE = 4 // e8
WKS_KING_TO_SQUARE = 62 // g1
WQS_KING_TO_SQUARE = 58 // c1
BKS_KING_TO_SQUARE = 6 // g8
BQS_KING_TO_SQUARE = 2 // c8
WKS_ROOK_ORIGINAL_SQUARE = 63 // h1
WQS_ROOK_ORIGINAL_SQUARE = 56 // a1
BKS_ROOK_ORIGINAL_SQUARE = 7 // h8
BQS_ROOK_ORIGINAL_SQUARE = 0 // a8
)
type Game struct {
Fen string
WhitePieces Bitboard
BlackPieces Bitboard
// _, pawns, knights, bishops, rooks, queens, king
Whites [7]Bitboard
Blacks [7]Bitboard
Occupied Bitboard
Squares [64]Piece
EnPassant Square
Castling int
Ply int
HalfMoves int
FullMoves int
Turn Color
PseudoMoves []Move
LegalMoves []Move
PositionHistory map[uint64]int
ZobristHash uint64
IsCheck bool
IsCheckmate bool
IsStalement bool
IsMaterialDraw bool
IsThreefoldRepetition bool
IsFiftyMoveRule bool
IsSeventyFiveMoveRule bool
IsFinished bool
History []GameState
}
func (g *Game) Reset() {
g.Fen = ""
g.WhitePieces = 0
g.BlackPieces = 0
for _, kind := range [6]Piece{PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING} {
g.Whites[kind] = 0
g.Blacks[kind] = 0
}
g.Occupied = 0
g.Squares = [64]Piece{}
g.EnPassant = 0
g.Castling = 0
g.Ply = 0
g.HalfMoves = 0
g.FullMoves = 0
g.Turn = WHITE
g.PseudoMoves = []Move{}
g.LegalMoves = []Move{}
g.PositionHistory = map[uint64]int{}
g.ZobristHash = 0
g.IsCheck = false
g.IsCheckmate = false
g.IsStalement = false
g.IsMaterialDraw = false
g.IsThreefoldRepetition = false
g.IsFiftyMoveRule = false
g.IsSeventyFiveMoveRule = false
g.IsFinished = false
g.History = []GameState{}
}
func NewGame() *Game {
g := Game{}
g.LoadFen(STARTING_POSITION_FEN)
//g.LoadFen("8/PPPPPPPP/8/8/8/8/8 w - - 0 1")
return &g
}
func CloneGame(g *Game) Game {
clone := Game{
Fen: g.Fen,
WhitePieces: g.WhitePieces,
BlackPieces: g.BlackPieces,
Whites: [7]Bitboard{},
Blacks: [7]Bitboard{},
Squares: [64]Piece{},
Occupied: g.Occupied,
EnPassant: g.EnPassant,
Castling: g.Castling,
Ply: g.Ply,
HalfMoves: g.HalfMoves,
FullMoves: g.FullMoves,
Turn: g.Turn,
PseudoMoves: []Move{},
LegalMoves: []Move{},
PositionHistory: map[uint64]int{},
ZobristHash: g.ZobristHash,
IsCheck: g.IsCheck,
IsCheckmate: g.IsCheckmate,
IsStalement: g.IsStalement,
IsMaterialDraw: g.IsMaterialDraw,
IsFinished: g.IsFinished,
History: make([]GameState, len(g.History)),
}
copy(clone.Whites[:], g.Whites[:])
copy(clone.Blacks[:], g.Blacks[:])
copy(clone.Squares[:], g.Squares[:])
copy(clone.History, g.History)
for k, v := range g.PositionHistory {
clone.PositionHistory[k] = v
}
return clone
}
func (g *Game) addPiece(piece Piece, index int) {
g.Squares[index] = piece
if piece == EMPTY {
return
}
bit := Bitboard(0x1 << uint(index))
kind := piece.Kind()
switch piece.Color() {
case WHITE:
g.Whites[kind] |= bit
g.WhitePieces |= bit
case BLACK:
g.Blacks[kind] |= bit
g.BlackPieces |= bit
}
g.Occupied |= bit
}
// Get our pawns and opponent's
func (g *Game) GetPawns() (Bitboard, Bitboard) {
if g.Turn == WHITE {
return g.Whites[PAWN], g.Blacks[PAWN]
}
return g.Blacks[PAWN], g.Whites[PAWN]
}
// Get our color and opponent's
func (g *Game) GetColors() (Color, Color) {
if g.Turn == WHITE {
return WHITE, BLACK
}
return BLACK, WHITE
}
func (g *Game) hasMoves() bool {
return len(g.LegalMoves) > 0
}
func (g *Game) ShouldIncFullMoves(m Move) bool {
return g.Squares[m.From()].Color() == BLACK
}
func (g *Game) ShouldResetHalfMoves(m Move) bool {
return m.GetCapturedPiece() > 0 || g.Squares[m.From()].Kind() == PAWN
}
var (
zobristOnce sync.Once
zobristPiece [12][64]uint64
zobristCastling [16]uint64
zobristEnPassant [8]uint64
zobristTurnToMove uint64
)
func initZobrist() {
rng := rand.New(rand.NewSource(1))
for i := 0; i < 12; i++ {
for j := 0; j < 64; j++ {
zobristPiece[i][j] = rng.Uint64()
}
}
for i := 0; i < 16; i++ {
zobristCastling[i] = rng.Uint64()
}
for i := 0; i < 8; i++ {
zobristEnPassant[i] = rng.Uint64()
}
zobristTurnToMove = rng.Uint64()
}
func ensureZobrist() {
zobristOnce.Do(initZobrist)
}
func zobristPieceIndex(p Piece) int {
switch p {
case W_PAWN:
return 0
case W_KNIGHT:
return 1
case W_BISHOP:
return 2
case W_ROOK:
return 3
case W_QUEEN:
return 4
case W_KING:
return 5
case B_PAWN:
return 6
case B_KNIGHT:
return 7
case B_BISHOP:
return 8
case B_ROOK:
return 9
case B_QUEEN:
return 10
case B_KING:
return 11
default:
return -1
}
}
func (g *Game) computeZobrist() uint64 {
ensureZobrist()
h := uint64(0)
for sq, piece := range g.Squares {
idx := zobristPieceIndex(piece)
if idx >= 0 {
h ^= zobristPiece[idx][sq]
}
}
h ^= zobristCastling[g.Castling&0xF]
if g.EnPassant != 0 {
file := g.EnPassant.File()
h ^= zobristEnPassant[file]
}
if g.Turn == BLACK {
h ^= zobristTurnToMove
}
return h
}
func (g *Game) recordPosition() {
if g.PositionHistory == nil {
g.PositionHistory = map[uint64]int{}
}
g.ZobristHash = g.computeZobrist()
g.PositionHistory[g.ZobristHash] = g.PositionHistory[g.ZobristHash] + 1
}
func (g *Game) checkThreefoldRepetition() bool {
return g.PositionHistory != nil && g.PositionHistory[g.ZobristHash] >= 3
}
func (g *Game) IsFivefoldRepetition() bool {
return g.PositionHistory != nil && g.PositionHistory[g.ZobristHash] >= 5
}
func (g *Game) checkFiftyMoveRule() bool {
return g.HalfMoves >= 100
}
func (g *Game) checkSeventyFiveMoveRule() bool {
return g.HalfMoves >= 150
}
type GameState struct {
CapturedPiece Piece
Castling int
EnPassant Square
Ply int
HalfMoves int
ZobristHash uint64
}