-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntc.nim
604 lines (536 loc) · 23 KB
/
ntc.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
## The nimTUROCHAMP chess engine,
## an implementation of TUROCHAMP in Nim using the Sunfish move generator
import tables, strutils, times, algorithm, math
from strformat import fmt
from unicode import reversed, swapCase
const
A1 = 91
H1 = 98
A8 = 21
H8 = 28
ini = ( " \n" &
" \n" &
" rnbqkbnr\n" &
" pppppppp\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" PPPPPPPP\n" &
" RNBQKBNR\n" &
" \n" &
" \n"
)
emp = ( " \n" &
" \n" &
" ........\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" ........\n" &
" \n" &
" \n"
)
N = -10
E = 1
S = 10
W = -1
dirs = to_table({'P': [N, N+N, N+W, N+E, 0, 0, 0, 0],
'N': [N+N+E, E+N+E, E+S+E, S+S+E, S+S+W, W+S+W, W+N+W, N+N+W],
'B': [N+E, S+E, S+W, N+W, 0, 0, 0, 0],
'R': [N, E, S, W, 0, 0, 0, 0],
'Q': [N, E, S, W, N+E, S+E, S+W, N+W],
'K': [N, E, S, W, N+E, S+E, S+W, N+W]
})
piece = to_table({'P': 1.0, 'N': 3.0, 'B': 3.5, 'R': 5.0, 'Q': 10.0, 'K': 1000.0})
piece_type = to_table({'P': 1.0, 'N': 2.0, 'B': 3.0, 'R': 4.0, 'Q': 5.0, 'K': 6.0})
var
MAXPLIES* = 2 ## brute-force search depth
QPLIES* = 8 ## selective search depth
NODES = 0
type Position* = object
board*: string
score*: float # material score
wc_w*: bool # castling rights (White/Black West/East)
wc_e*: bool
bc_w*: bool
bc_e*: bool
ep*: int # en-passant square
kp*: int # King's passant square
lastcap*: int # target square of preceding capture move
proc render*(x: int): string =
## convert index to square name
var r = int((x - A8) / 10)
var f = (x - A8) mod 10
result = fmt"{char(f + ord('a'))}{8 - r}"
proc parse*(c: string, inv = false): int =
## convert square name to index
var f = ord(c[0]) - ord('a')
var r = ord(c[1]) - ord('1')
if inv:
f = 7 - f
r = 7 - r
#echo f, " ", r
return A1 + f - 10 * r
proc newgame*(): Position =
## create a new board in the starting position
Position(board: ini, score: 0, wc_w: true, wc_e: true,
bc_w: true, bc_e: true, ep: 0, kp: 0)
proc put(board: string, at: int, piece: char): string =
## put piece at board location
return board[0..at-1] & piece & board[at+1..119]
proc rotate*(s: Position): Position =
## rotate board for other player's turn
var ep = 0
var kp = 0
var lastcap = 0
if s.ep > 0:
ep = 119 - s.ep
if s.kp > 0:
kp = 119 - s.kp
if s.lastcap > 0:
lastcap = 119 - s.lastcap
return Position(board: s.board.reversed.swapCase(),
score: -s.score, wc_w: s.bc_w, wc_e: s.bc_e, bc_w: s.wc_w, bc_e: s.wc_e,
ep: ep, kp: kp, lastcap: lastcap)
proc fromfen*(fen: string): Position =
## accept a FEN and return a board
var b = emp
let f = fen.split(" ")[0]
let cas = fen.split(" ")[2]
let enpas = fen.split(" ")[3]
var i = 0
var j = 0
for x in f:
var a = ord(x)
if (a > 48) and (a < 57):
i = i + (a - 48)
elif a == 47:
i = 0
inc j
else:
b[A8 + 10*j + i] = x
inc i
var ep = 0
if enpas != "-":
ep = parse(enpas)
var pos = Position(board: b, score: 0, wc_w: cas.contains('Q'), wc_e: cas.contains('K'),
bc_w: cas.contains('k'), bc_e: cas.contains('q'), ep: ep, kp: 0)
if fen.split(" ")[1] == "b":
pos = pos.rotate()
return pos
proc gen_moves_no_castle*(s: Position): seq[(int, int)] =
## generate all pseudo-legal moves in a position (except castling)
for i in 0..119:
let p = s.board[i]
if not p.isUpperAscii():
continue
#echo i, " ", render(i)
for d in dirs[p]:
if d == 0:
break
var j = i + d
while true:
#echo render(j)
let q = s.board[j]
if q.isSpaceAscii() or q.isUpperAscii():
break
if p == 'P' and d in [N, N+N] and q != '.':
break
if p == 'P' and d == N+N and (i < A1+N or
s.board[i+N] != '.'):
break
if p == 'P' and d in [N+W, N+E] and q == '.' and
not (j in [s.ep, s.kp, s.kp-1, s.kp+1]):
break
result.add((i, j))
if p == 'P' or p == 'N' or p == 'K' or
q.isLowerAscii():
break
j = j + d
proc attacks*(pos: Position, x: int): seq[int] =
## return attacked empty and enemy squares
let moves = pos.gen_moves_no_castle()
for n in 0..len(moves)-1:
let i = moves[n][0]
let j = moves[n][1]
if i == x:
result.add(j)
proc ischeck(s: Position): (bool, bool, bool) =
## is the King in check? is castling allowed?
var
check = false
wcastle = true
ecastle = true
kpos = -1
for i in 0..119:
if s.board[i] == 'k':
kpos = i
for i in 0..119:
let p = s.board[i]
if not p.isUpperAscii(): continue
let a = s.attacks(i)
for j in a:
if s.board[j] == 'k': check = true
if j == kpos - 2 or j == kpos - 1:
ecastle = false
if j == kpos + 1 or j == kpos + 2:
wcastle = false
return (check, wcastle, ecastle)
proc gen_moves*(s: Position, test_check = false): seq[(int, int)] =
## generate all pseudo-legal moves in a position
var
check = false
wcastle = true
ecastle = true
if test_check:
(check, wcastle, ecastle) = ischeck(rotate(s))
#echo (check, wcastle, ecastle)
for i in 0..119:
let p = s.board[i]
if not p.isUpperAscii():
continue
#echo i, " ", render(i)
for d in dirs[p]:
if d == 0:
break
var j = i + d
while true:
#echo render(j)
let q = s.board[j]
if q.isSpaceAscii() or q.isUpperAscii():
break
if p == 'P' and d in [N, N+N] and q != '.':
break
if p == 'P' and d == N+N and (i < A1+N or
s.board[i+N] != '.'):
break
if p == 'P' and d in [N+W, N+E] and q == '.' and
not (j in [s.ep, s.kp, s.kp-1, s.kp+1]):
break
result.add((i, j))
if p == 'P' or p == 'N' or p == 'K' or
q.isLowerAscii():
break
if i == A1 and s.board[j+E] == 'K' and s.wc_w and wcastle and not check:
result.add((j+E, j+W))
if i == H1 and s.board[j+W] == 'K' and s.wc_e and ecastle and not check:
result.add((j+W, j+E))
j = j + d
proc value(s: Position, fr: int, to: int): float =
## compute score difference due to given move
let p = s.board[fr]
let q = s.board[to]
if q.isLowerAscii():
result += piece[q.toUpperAscii()]
if p == 'P':
if (A8 <= to) and (to <= H8):
result += piece['Q'] - piece['P']
if to == s.ep:
result += piece['P']
proc move*(s: Position, fr: int, to: int): Position =
## carry out a move on the board
let p = s.board[fr]
let q = s.board[to]
var
board = s.board
score = s.score + s.value(fr, to)
wc_w = s.wc_w
wc_e = s.wc_e
bc_w = s.bc_w
bc_e = s.bc_e
ep = 0
kp = 0
lastcap = 0
if q != '.': lastcap = to else: lastcap = 0
board = put(board, to, p)
board = put(board, fr, '.')
if fr == A1: wc_w = false
if fr == H1: wc_e = false
if to == A8: bc_e = false
if to == H8: bc_w = false
if p == 'K':
wc_w = false
wc_e = false
if abs(to - fr) == 2:
kp = int((to + fr) / 2)
if to < fr:
board = put(board, A1, '.')
else:
board = put(board, H1, '.')
board = put(board, kp, 'R')
if p == 'P':
if (A8 <= to) and (to <= H8):
board = put(board, to, 'Q')
if to - fr == 2*N:
ep = fr + N
if to == s.ep:
board = put(board, to+S, '.')
return Position(board: board, score: score,
wc_w: wc_w, wc_e: wc_e, bc_w: bc_w, bc_e: bc_e, ep: ep, kp: kp, lastcap: lastcap)
proc myCmp(x, y: tuple): int =
if x[0] > y[0]: -1 else: 1
proc isblack*(pos: Position): bool =
## is it Black's turn?
if pos.board.startsWith('\n'): true else: false
proc mirror*(x: string): string =
## mirror move for Black
let f1 = char(ord('a') + 7 - (ord(x[0]) - ord('a')))
let f2 = char(ord('a') + 7 - (ord(x[2]) - ord('a')))
let r1 = char(ord('1') + 7 - (ord(x[1]) - ord('1')))
let r2 = char(ord('1') + 7 - (ord(x[3]) - ord('1')))
if x.len == 4:
return f1 & r1 & f2 & r2
else: # piece promotion
return f1 & r1 & f2 & r2 & x[4]
proc mirrmv(pos: Position, x: string): string =
## mirror move if it is Black's turn
if pos.isblack:
return x.mirror
else:
return x
proc defenders*(pos: Position, x: int): seq[int] =
## get list of defenders for a square
var db = Position(board: pos.board, score: pos.score,
wc_w: pos.wc_w, wc_e: pos.wc_e,
bc_w: pos.bc_w, bc_e: pos.bc_e, ep: pos.ep, kp: pos.kp)
db.board[x] = 'p'
let moves = db.gen_moves()
for n in 0..len(moves)-1:
let i = moves[n][0]
let j = moves[n][1]
if j == x:
result.add(i)
proc isdead(s: Position, mm: seq[(int, int)]): bool =
## is the position dead?
var check = false
for i in 0..119:
let p = s.board[i]
if not p.isUpperAscii(): continue
let a = s.attacks(i)
# other player's King
for j in a:
if s.board[j] == 'k': check = true
if check: return false
for m in mm:
#let p = s.board[m[0]]
let q = s.board[m[1]]
if q != '.' and s.lastcap > 0 and m[1] == s.lastcap: return false
return true
proc order(b: Position, ply: int, moves: seq[(int, int)]): seq[(int, int)] =
## order moves by importance
var mlist: seq[(float, int, int)]
for m in moves:
let p = b.board[m[0]].toUpperAscii
let q = b.board[m[1]].toUpperAscii
if q != '.':
mlist.add((10 * piece_type[q] - piece_type[p], m[0], m[1]))
elif m[1] == b.ep:
mlist.add((10 - piece_type[p], m[0], m[1]))
else:
mlist.add((piece_type[p], m[0], m[1]))
mlist.sort(myCmp)
for m in mlist:
result.add((m[1], m[2]))
proc searchmax(b: Position, ply: int, alpha: float, beta: float, pv: string): (float, string) =
## Negamax search function
inc NODES
if ply >= QPLIES:
return (b.score, pv)
if not ('K' in b.board): return (-9999.0, pv)
if not ('k' in b.board): return (9999.0, pv)
var moves = order(b, ply, gen_moves(b))
if ply > MAXPLIES and b.isdead(moves):
return (b.score, pv)
if ply > MAXPLIES:
var mov2: seq[(int, int)]
for i in 0..len(moves)-1:
if b.board[moves[i][1]] != '.':
mov2.add((moves[i][0], moves[i][1]))
moves = mov2
if len(moves) == 0: return (b.score, pv)
var al = alpha
var v = pv
for i in 0..len(moves)-1:
let c = b.move(moves[i][0], moves[i][1])
let d = c.rotate()
let fr = render(moves[i][0])
let to = render(moves[i][1])
var (t, vv) = searchmax(d, ply + 1, -beta, -al, pv & " " & c.mirrmv(fr & to))
t = -t
if t >= beta:
return (beta, vv)
if t > al:
al = t
v = vv
return (al, v)
proc turing(s: Position): float =
## evaluate Turing positional criteria
var bking = false
for i in 0..119:
let p = s.board[i]
var tt: float
if not p.isUpperAscii(): continue
let a = s.attacks(i)
# Black King
for j in a:
if s.board[j] == 'k': bking = true
# piece mobility
if p != 'P':
if len(a) > 0:
for j in a:
if s.board[j] == '.': tt += 1
else: tt += 2
result += sqrt(tt)
# pieces defended
if p == 'R' or p == 'B' or p == 'N':
let ndef = len(s.defenders(i))
if ndef > 0: result += 1
if ndef > 1: result += 0.5
# King safety
if p == 'K':
var ks = Position(board: s.board, score: s.score,
wc_w: s.wc_w, wc_e: s.wc_e,
bc_w: s.bc_w, bc_e: s.bc_e, ep: s.ep, kp: s.kp)
tt = 0
ks.board[i] = 'Q'
let ka = ks.attacks(i)
if len(ka) > 0:
for j in ka:
if s.board[j] == '.': tt += 1
else: tt += 2
result -= sqrt(tt)
# Pawns
if p == 'P':
let rad = int(6 - (i - A8) / 10)
result += 0.2 * float(rad)
let pdef = s.defenders(i)
var pawndef = false
for k in pdef:
if s.board[k] != 'P':
pawndef = true
if pawndef: result += 0.3
# Black King
if bking: result += 0.5
proc getmove*(b: Position, output = false): string =
## get computer move for board position
NODES = 0
let start = epochTime()
let moves = order(b, 0, gen_moves(b, true))
var tbest = -1e6
var ll: seq[(float, string, string, int, int, string)]
for i in 0..len(moves)-1:
let fr = render(moves[i][0])
let to = render(moves[i][1])
var castle: float
if b.board[moves[i][0]] == 'K' and abs(moves[i][0] - moves[i][1]) == 2: castle += 1
let c = b.move(moves[i][0], moves[i][1])
if c.isblack():
if c.bc_w or c.bc_e: castle += 1
else:
if c.wc_w or c.wc_e: castle += 1
let d = c.rotate()
var (t, pv) = searchmax(d, 2, -1e6, -tbest, c.mirrmv(fr & to))
t = -t
if t > tbest: tbest = t - 0.1
if output:
echo fr, to, " ", t, " ", c.turing(), " ", pv
ll.add((t + (c.turing() + castle) / 1000.0, fr, to, moves[i][0], moves[i][1], pv))
ll.sort(myCmp)
let diff = epochTime() - start
let nps = int(float(NODES) / diff)
echo fmt"info depth {MAXPLIES} seldepth {QPLIES} score cp {int(100*ll[0][0])} time {int(1000*diff)} nodes {NODES} nps {nps} pv {ll[0][5]}"
result = ll[0][1] & ll[0][2]
if b.board[ll[0][3]] == 'P' and (A8 <= ll[0][4]) and (ll[0][4] <= H8):
result = result & "q"
when isMainModule:
proc main() =
var b: Position
var side = true # White's turn
proc getgame(x: string): Position =
## construct board from startpos moves command, e.g. from Cute Chess
var b = newgame()
side = true
let mm = x.split(' ')[3..^1]
var inv = false
for i in mm:
var fr = parse(i[0..1], inv = inv)
var to = parse(i[2..3], inv = inv)
var c = b.move(fr, to)
side = not side
var d = c.rotate()
b = d
inv = not inv
return b
proc shredder(l: string): string =
## fix Shredder FENs
let x = l.split(" ")
return x[0..5].join(" ") & " 0 1 " & x[6..^1].join(" ")
proc getgame_fen(x: string): Position =
## construct board from FEN command, e.g. from Picochess
var inv: bool
var l: string
if x.split(" ")[6] == "moves": l = shredder(x) else: l = x
let ff = l.split(" ")[2..7]
let ff2 = ff.join(" ")
var b = fromfen(ff2)
if " w " in ff2:
side = true
inv = false
else:
side = false
inv = true
if len(l.split(" ")) > 8:
let mm = l.split(" ")[9..^1]
for i in mm:
let fr = parse(i[0..1], inv = inv)
let to = parse(i[2..3], inv = inv)
let c = b.move(fr, to)
side = not side
let d = c.rotate()
b = d
inv = not inv
return b
while true:
let l = readLine(stdin)
if l == "quit":
break
if l == "?":
echo b.board
if l == "uci":
echo "id name nimTUROCHAMP"
echo "id author Martin C. Doege"
echo fmt"option name maxplies type spin default {MAXPLIES} min 1 max 1024"
echo fmt"option name qplies type spin default {QPLIES} min 1 max 1024"
echo "uciok"
if l == "isready":
if b.board == "":
b = newgame()
side = true
echo "readyok"
if l == "ucinewgame" or l == "position startpos":
b = newgame()
side = true
if l.startsWith("setoption name maxplies value"):
MAXPLIES = parseInt(l.split()[4])
echo "# maxplies ", MAXPLIES
if l.startsWith("setoption name qplies value"):
QPLIES = parseInt(l.split()[4])
echo "# qplies ", QPLIES
if l.startsWith("position startpos moves"):
b = l.getgame()
if l.startsWith("position fen"):
b = l.getgame_fen()
if l.startsWith("go"):
if b.board == "":
b = newgame()
side = true
var m = b.getmove()
if not side:
m = m.mirror()
side = not side
echo "bestmove ", m
main()