-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
458 lines (448 loc) · 10.2 KB
/
parse.go
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package ghess
import (
"errors"
"regexp"
"strconv"
"strings"
)
// ParseStand does pas grande chose mnt.
// TODO: include piece value? Or use different method
// Pgn map takes standar notation and returns coordinate.
// This method is useful for chessboardjs gui
func (b *Board) ParseStand(orig, dest string) error {
e := b.Move(PgnToCoordMap[orig], PgnToCoordMap[dest])
if e != nil {
return e
}
return nil
}
// ParseMove infers origin and destination
// coordinates from a pgn notation move. Check
// and Check Mate notations will be added automatically.
// TODO: disambiguiation
func (b *Board) ParseMove(move string) error {
move = strings.TrimRight(move, "\r\n") // prepare for input
// Variables
var piece string // find move piece
var orig int // find origin coord of move
var square string // find PgnMap key of move
var attacker string // left of x
//var precise string // for multiple possibilities
var target byte // the piece to move, in proper case
var column int
var row [8]int
columns := make(map[string]int)
columns["a"] = 8
columns["b"] = 7
columns["c"] = 6
columns["d"] = 5
columns["e"] = 4
columns["f"] = 3
columns["g"] = 2
columns["h"] = 1
// Status
isCastle := false
isWhite := b.toMove == "w"
isCapture, _ := regexp.MatchString(`x`, move)
res := PgnPattern.FindStringSubmatch(move)
if res == nil && move != "O-O" && move != "O-O-O" {
return errors.New("invalid input")
} else if move == "O-O" || move == "O-O-O" {
// be nice
isCastle = true
}
// Either is capture or not
if isCapture {
attacker = res[1]
if attacker == strings.ToLower(attacker) {
piece = "P"
column = columns[attacker]
} else { // if upper case, forcement a piece
piece = string(attacker[0])
if len(res[1]) > 1 {
i, err := strconv.Atoi(string(res[1][1]))
if err == nil && i != 0 {
row = PgnRowMap[i]
} else {
c := string(res[1][1])
column = columns[c]
}
}
}
square = res[2]
} else if isCastle {
if move == "O-O" {
piece = "K"
if isWhite {
square = "h1"
} else {
square = "h8"
}
} else if move == "O-O-O" {
piece = "K"
if isWhite {
square = "a1"
} else {
square = "a8"
}
}
} else { // No x
chars := len(move)
if chars == 2 {
piece = "P"
square = res[2]
} else if chars == 3 && move != "0-0" {
// Breaks when e44 is entered...
piece = res[1]
square = res[2] //move[0]
} else if chars == 4 {
if len(res[1]) > 1 {
i, err := strconv.Atoi(string(res[1][1]))
if err == nil && i != 0 {
row = PgnRowMap[i]
} else {
c := string(res[1][1])
column = columns[c]
}
}
piece = string(res[1][0]) // remove second char
square = res[2]
} else {
return errors.New("Not enough input")
}
}
// the presumed destination
dest := PgnToCoordMap[square]
// The piece will be saved as case sensitive byte
if b.toMove == "b" {
if piece != "" {
target = []byte(strings.ToLower(piece))[0]
} else {
return errors.New("Invalid Input")
}
} else {
if piece != "" {
target = []byte(piece)[0]
} else {
return errors.New("Invalid Input")
}
}
switch {
case piece == "P": // Pawn Parse
var possibilities [2]int // two potentional origins
// TODO: Allow for empassant take
if b.toMove == "w" {
if isCapture {
possibilities[0],
possibilities[1] = dest-9,
dest-11
} else {
possibilities[0],
possibilities[1] = dest-10,
dest-20
}
} else { // is black to move
if isCapture {
possibilities[0],
possibilities[1] = dest+9,
dest+11
} else {
possibilities[0],
possibilities[1] = dest+10,
dest+20
}
}
// Disambiguate
firstDisambig := column != 0 && possibilities[0]%10 == column
secondDisambig := column != 0 && possibilities[1]%10 == column
firstPoss := b.board[possibilities[0]] == target
secondPoss := b.board[possibilities[1]] == target
if firstPoss && firstDisambig && isCapture {
orig = possibilities[0]
} else if secondPoss && secondDisambig && isCapture {
orig = possibilities[1]
} else if firstPoss {
orig = possibilities[0]
} else if secondPoss {
orig = possibilities[1]
}
case piece == "N": // Knight Parse
var possibilities [8]int
// TODO: assume no precision
// Change to possibilities[]
possibilities[0], possibilities[1],
possibilities[2], possibilities[3],
possibilities[4], possibilities[5],
possibilities[6], possibilities[7] = dest+21,
dest+19, dest+12, dest+8, dest-8,
dest-12, dest-19, dest-21
for _, possibility := range possibilities {
if column != 0 { // Disambiguate
disambig := possibility%10 == column
if b.board[possibility] == target && disambig {
orig = possibility
break
}
} else if row[0] != 0 { // Disambiguate
for _, r := range row {
disambig := b.board[possibility] == target &&
b.board[possibility] == byte(r)
if disambig {
orig = possibility
break
}
}
} else {
if b.board[possibility] == target {
orig = possibility
break
}
}
}
case piece == "B": // Bishop Parse
var possibilities [14]int
ticker := 0
// a8 - h1
for i := dest + 9; i < 90; i += 9 {
if (i+1)%10 == 0 { // hits boarder
break
}
possibilities[ticker] = i
ticker++
}
for i := dest - 9; i > 10; i -= 9 {
if (i+1)%10 == 0 { // hits boarder
break
}
possibilities[ticker] = i
ticker++
}
// a1 - h8 Vector
for i := dest + 11; i < 90; i += 11 {
if (i+1)%10 == 0 { // hits boarder
break
}
possibilities[ticker] = i
ticker++
}
for i := dest - 11; i > 10; i -= 11 {
if i%10 == 0 {
break
}
possibilities[ticker] = i
ticker++
}
// Find piece origin
for _, possibility := range possibilities {
if b.board[possibility] == target {
orig = possibility
break
}
}
case piece == "R": // Rook Parse
var possibilities [14]int
ticker := 0
// Horizontal Vector
for i := dest + 10; i < 90; i += 10 {
possibilities[ticker] = i
ticker++
}
for i := dest - 10; i > 10; i -= 10 {
possibilities[ticker] = i
ticker++
}
// Vertical Vector
for i := dest + 1; i < 90; i++ {
if (i+1)%10 == 0 { // hits boarder
break
}
possibilities[ticker] = i
ticker++
}
for i := dest - 1; i > 10; i-- {
if i%10 == 0 {
break
}
possibilities[ticker] = i
ticker++
}
LoopRook:
for _, possibility := range possibilities {
if column != 0 { // Disambiguate
disambig := possibility%10 == column
if b.board[possibility] == target && disambig {
orig = possibility
err := b.validRook(orig, dest)
if err != nil {
continue
}
break LoopRook
}
} else if row[0] != 0 { // Disambiguate
for _, r := range row {
disambig := b.board[possibility] == target &&
possibility == r
if disambig {
orig = possibility
err := b.validRook(orig, dest)
if err != nil {
continue
}
break LoopRook
}
}
} else {
if b.board[possibility] == target {
orig = possibility
err := b.validRook(orig, dest)
if err != nil {
continue
}
break LoopRook
}
}
}
case piece == "Q": // Queen Parse
for idx, possibility := range b.board {
if possibility == target {
orig = idx
break
}
}
case piece == "K": // King Parse
var possibilities [8]int
if isCastle {
if isWhite {
orig = 14
} else {
orig = 84
}
break
}
possibilities[0], possibilities[1],
possibilities[2], possibilities[3],
possibilities[4], possibilities[5],
possibilities[6], possibilities[7] = dest+10,
dest+11, dest+1, dest+9, dest-10,
dest-11, dest-1, dest-9
for _, possibility := range possibilities {
if b.board[possibility] == target {
orig = possibility
break
}
}
}
// Move the Piece
// - Validate Move in Board.Move()
if b.board[dest] != '.' && !isCapture && !isCastle {
return errors.New("Not the proper capture syntax")
}
if orig != 0 && dest != 0 {
err := b.Move(orig, dest)
if err == nil {
matchCheck, _ := regexp.MatchString(`+`, move)
matchMate, _ := regexp.MatchString(`#`, move)
// Update pgn History
if b.toMove == "b" {
b.pgn += strconv.Itoa(b.moves) + ". "
}
b.pgn += (move)
if b.Checkmate && matchMate {
b.pgn += "# "
} else if b.Check && matchCheck {
b.pgn += "+ "
} else {
b.pgn += " " // add space
}
}
return err
}
return errors.New("No such move")
}
// LoadPgn reads a pgn match.
// TODO: ignore header strings eg [White].
func (b *Board) LoadPgn(match string) error {
// Does this already ignore?
result := PgnPattern.FindAllString(match, -1)
for _, val := range result {
err := b.ParseMove(val)
if err != nil {
//fmt.Println(b.StringWhite())
//fmt.Println(val)
//fmt.Println(err)
return err
}
}
return nil
}
// LoadFen parses FEN string and update Board.board.
func (b *Board) LoadFen(fen string) error {
// Treat fen input
fen = strings.TrimRight(fen, "\r\n")
matches := FenPattern.MatchString(fen)
if !matches {
return errors.New("Invalid FEN")
}
res := FenPattern.FindStringSubmatch(fen)
posCount := 88 // First position to fill
//
for _, val := range res[1] { // res[1] {
// First, make sure it's a relevant position
if (posCount % 10) == 0 { // || (posCount+1)%10 == 0 {
posCount -= 2
}
// Check if there are Empty Squares
num, e := strconv.Atoi(string(val))
if e == nil {
for j := 0; j < num; j++ {
b.board[posCount] = '.'
posCount--
}
continue
}
if val == '/' {
continue
}
// Is regular Piece
b.board[posCount] = byte(val)
posCount--
}
// Update Board value
// Castle
b.castle = [4]byte{'-', '-', '-', '-'}
for _, v := range res[3] {
switch {
case v == 'K':
b.castle[0] = 'K'
break
case v == 'Q':
b.castle[1] = 'Q'
break
case v == 'k':
b.castle[2] = 'k'
break
case v == 'q':
b.castle[3] = 'q'
break
}
}
// Empassant
if res[4] != "-" {
emp := PgnToCoordMap[res[4]]
if emp < 40 {
// white pawn
} else if emp > 60 {
} else {
return errors.New("Invalid FEN empassant")
}
} else {
b.empassant = 0
}
// Turn
turns, _ := strconv.Atoi(res[5])
b.moves = turns
b.fen = fen
b.toMove = res[2]
b.Check = b.isPlayerInCheck()
return nil
}