-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositionUtils.nim
264 lines (223 loc) · 9.59 KB
/
positionUtils.nim
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
import
types,
position,
move,
movegen,
utils,
bitboard,
castling
export move, position
import std/[
strutils,
options,
bitops
]
func fen*(position: Position): string =
result = ""
var emptySquareCounter = 0
for rank in countdown(7, 0):
for file in 0..7:
let square = (rank*8 + file).Square
let coloredPiece = position.coloredPiece(square)
if coloredPiece.piece != noPiece and coloredPiece.color != noColor:
if emptySquareCounter > 0:
result &= $emptySquareCounter
emptySquareCounter = 0
result &= coloredPiece.notation
else:
emptySquareCounter += 1
if emptySquareCounter > 0:
result &= $emptySquareCounter
emptySquareCounter = 0
if rank != 0:
result &= "/"
result &= (if position.us == white: " w " else: " b ")
for color in [white, black]:
for castlingSide in queenside..kingside:
let rookSource = position.rookSource[color][castlingSide]
if rookSource != noSquare and (rookSource.toBitboard and homeRank[color]) != 0:
result &= ($rookSource)[0]
if result[^1] == 'h':
result[^1] = 'k'
if result[^1] == 'a':
result[^1] = 'q'
if color == white:
result[^1] = result[^1].toUpperAscii
if result.endsWith(' '):
result &= "-"
result &= " "
if position.enPassantTarget != 0:
result &= $(position.enPassantTarget.toSquare)
else:
result &= "-"
result &= " " & $position.halfmoveClock & " " & $(position.halfmovesPlayed div 2)
func `$`*(position: Position): string =
result = boardString(proc (square: Square): Option[string] =
if (square.toBitboard and position.occupancy) != 0:
return some($position.coloredPiece(square))
none(string)
) & "\n"
let fenWords = position.fen.splitWhitespace
for i in 1..<fenWords.len:
result &= fenWords[i] & " "
func debugString*(position: Position): string =
for piece in pawn..king:
result &= $piece & ":\n"
result &= position[piece].bitboardString & "\n"
for color in white..black:
result &= $color & ":\n"
result &= position[color].bitboardString & "\n"
result &= "enPassantTarget:\n"
result &= position.enPassantTarget.bitboardString & "\n"
result &= "us: " & $position.us & ", enemy: " & $position.enemy & "\n"
result &= "halfmovesPlayed: " & $position.halfmovesPlayed & ", halfmoveClock: " & $position.halfmoveClock & "\n"
result &= "zobristKey: " & $position.zobristKey & "\n"
result &= "rookSource: " & $position.rookSource
func legalMoves*(position: Position): seq[Move] =
var pseudoLegalMoves = newSeq[Move](64)
while true:
# 'generateMoves' silently stops generating moves if the given array is not big enough
let numMoves = position.generateMoves(pseudoLegalMoves)
if pseudoLegalMoves.len <= numMoves:
pseudoLegalMoves.setLen(numMoves * 2)
else:
pseudoLegalMoves.setLen(numMoves)
for move in pseudoLegalMoves:
let newPosition = position.doMove(move)
if newPosition.inCheck(position.us):
continue
result.add move
break
func isChess960*(position: Position): bool =
for color in white..black:
if position.rookSource[color] != [noSquare, noSquare] and position.kingSquare(color) != classicalKingSource[color]:
return true
for side in queenside..kingside:
if position.rookSource[color][side] notin [noSquare, classicalRookSource[color][side]]:
return true
false
func toMove*(s: string, position: Position): Move =
if s.len != 4 and s.len != 5:
raise newException(ValueError, "Move string is wrong length: " & s)
let
source = parseEnum[Square](s[0..1])
target = parseEnum[Square](s[2..3])
promoted = if s.len == 5: s[4].toColoredPiece.piece else: noPiece
for move in position.legalMoves:
if move.source == source and move.promoted == promoted:
if move.target == target:
return move
if move.castled and target == kingTarget[position.us][position.castlingSide(move)] and not position.isChess960:
return move
raise newException(ValueError, "Move is illegal: " & s)
proc toPosition*(fen: string, suppressWarnings = false): Position =
var fenWords = fen.splitWhitespace()
if fenWords.len < 4:
raise newException(ValueError, "FEN must have at least 4 words")
if fenWords.len > 6 and not suppressWarnings:
echo "WARNING: FEN shouldn't have more than 6 words"
while fenWords.len < 6:
fenWords.add("0")
for i in 2..8:
fenWords[0] = fenWords[0].replace($i, repeat("1", i))
let piecePlacement = fenWords[0]
let activeColor = fenWords[1]
let castlingRights = fenWords[2]
let enPassant = fenWords[3]
let halfmoveClock = fenWords[4]
let fullmoveNumber = fenWords[5]
var squareList = block:
var squareList: seq[Square]
for y in 0..7:
for x in countdown(7, 0):
squareList.add Square(y*8 + x)
squareList
for pieceChar in piecePlacement:
if squareList.len == 0:
raise newException(ValueError, "FEN is not correctly formatted (too many squares)")
case pieceChar
of '/':
# we don't need to do anything, except check if the / is at the right place
if not squareList[^1].isLeftEdge:
raise newException(ValueError, "FEN is not correctly formatted (misplaced '/')")
of '1':
discard squareList.pop
of '0':
if not suppressWarnings:
echo "WARNING: '0' in FEN piece placement data is not official notation"
else:
doAssert pieceChar notin ['2', '3', '4', '5', '6', '7', '8']
try:
let sq = squareList.pop
result.addColoredPiece(pieceChar.toColoredPiece, sq)
except ValueError:
raise newException(ValueError, "FEN piece placement is not correctly formatted: " &
getCurrentExceptionMsg())
if squareList.len != 0:
raise newException(ValueError, "FEN is not correctly formatted (too few squares)")
# active color
case activeColor
of "w", "W":
result.us = white
of "b", "B":
result.us = black
else:
raise newException(ValueError, "FEN active color notation does not exist: " & activeColor)
# castling rights
result.rookSource = [[noSquare, noSquare], [noSquare, noSquare]]
for castlingChar in castlingRights:
if castlingChar == '-':
continue
let
us = if castlingChar.isUpperAscii: white else: black
kingSquare = (result[us] and result[king]).toSquare
let rookSource = case castlingChar:
of 'K', 'k':
var rookSource = kingSquare
while rookSource.goRight:
if (result[rook, us] and rookSource.toBitboard) != 0:
break
rookSource
of 'Q', 'q':
var rookSource = kingSquare
while rookSource.goLeft:
if (result[rook, us] and rookSource.toBitboard) != 0:
break
rookSource
else:
let rookSourceBit = files[parseEnum[Square](castlingChar.toLowerAscii & "1")] and homeRank[us]
if rookSourceBit.countSetBits != 1:
raise newException(ValueError, "FEN castling ambiguous or erroneous: " & activeColor)
(files[parseEnum[Square](castlingChar.toLowerAscii & "1")] and homeRank[us]).toSquare
let castlingSide = if rookSource < kingSquare: queenside else: kingside
result.rookSource[us][castlingSide] = rookSource
# en passant square
result.enPassantTarget = 0
if enPassant != "-":
try:
result.enPassantTarget = parseEnum[Square](enPassant.toLowerAscii).toBitboard
except ValueError:
raise newException(ValueError, "FEN en passant target square is not correctly formatted: " &
getCurrentExceptionMsg())
# halfmove clock and fullmove number
try:
result.halfmoveClock = parseUInt(halfmoveClock).int16
except ValueError:
raise newException(ValueError, "FEN halfmove clock is not correctly formatted: " & getCurrentExceptionMsg())
try:
result.halfmovesPlayed = parseUInt(fullmoveNumber).int16 * 2
except ValueError:
raise newException(ValueError, "FEN fullmove number is not correctly formatted: " & getCurrentExceptionMsg())
result.zobristKey = result.calculateZobristKey
func notation*(move: Move, position: Position): string =
if move.castled and not position.isChess960:
return $move.source & $kingTarget[position.us][position.castlingSide(move)]
$move
func notation*(pv: seq[Move], position: Position): string =
var currentPosition = position
for move in pv:
result &= move.notation(currentPosition) & " "
currentPosition = currentPosition.doMove(move)
func insufficientMaterial*(position: Position): bool =
(position[pawn] or position[rook] or position[queen]) == 0 and (position[bishop] or position[knight]).countSetBits <= 1
const startpos* = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".toPosition