-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.py
More file actions
425 lines (353 loc) · 13.2 KB
/
verify.py
File metadata and controls
425 lines (353 loc) · 13.2 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
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
#!/usr/bin/env python3
"""Verification mirror of chess.rpgle — exact same logic in Python.
NOT a generated file. This is a 1:1 translation of the RPG source
to verify the chess engine works before publishing on GitHub.
Same board layout, same piece encoding, same search algorithm.
"""
import sys
import time
# ─── Constants (mirrors RPG dcl-c) ────────────────────────────
EMPTY = 0
wPAWN = 1; wKNIGHT = 2; wBISHOP = 3; wROOK = 4; wQUEEN = 5; wKING = 6
bPAWN = 9; bKNIGHT = 10; bBISHOP = 11; bROOK = 12; bQUEEN = 13; bKING = 14
WHITE = 0; BLACK = 1
MAX_DEPTH = 4
INF = 30000
# ─── Direction Tables ─────────────────────────────────────────
KN_DIR = [-17, -15, -10, -6, 6, 10, 15, 17]
RK_DIR = [-8, 8, -1, 1]
BP_DIR = [-9, -7, 7, 9]
# ─── Material Values (index = piece code) ─────────────────────
MAT_VAL = [0] * 15
MAT_VAL[wPAWN] = 100; MAT_VAL[wKNIGHT] = 320; MAT_VAL[wBISHOP] = 330
MAT_VAL[wROOK] = 500; MAT_VAL[wQUEEN] = 900; MAT_VAL[wKING] = 20000
MAT_VAL[bPAWN] = 100; MAT_VAL[bKNIGHT] = 320; MAT_VAL[bBISHOP] = 330
MAT_VAL[bROOK] = 500; MAT_VAL[bQUEEN] = 900; MAT_VAL[bKING] = 20000
# ─── Piece-Square Tables (1-indexed, mirrors RPG dim(64)) ────
pst_pawn = [0] * 65
pst_pawn[28] = 20; pst_pawn[29] = 30; pst_pawn[30] = 30; pst_pawn[27] = 20
pst_pawn[36] = 20; pst_pawn[37] = 30; pst_pawn[38] = 30; pst_pawn[35] = 20
pst_pawn[20] = 10; pst_pawn[21] = 15; pst_pawn[22] = 15; pst_pawn[19] = 10
pst_knight = [0] * 65
pst_knight[27] = 10; pst_knight[28] = 15; pst_knight[29] = 15; pst_knight[30] = 10
pst_knight[35] = 10; pst_knight[36] = 15; pst_knight[37] = 15; pst_knight[38] = 10
pst_knight[19] = 5; pst_knight[20] = 10; pst_knight[21] = 10; pst_knight[22] = 5
pst_knight[43] = 5; pst_knight[44] = 10; pst_knight[45] = 10; pst_knight[46] = 5
pst_king = [0] * 65
pst_king[62] = 20; pst_king[63] = 30
pst_king[58] = 20; pst_king[59] = 30
# ─── Board (1-indexed, index 0 unused) ───────────────────────
board = [0] * 65
# ─── Move Stack ───────────────────────────────────────────────
stk_from = []
stk_to = []
stk_cap = []
nodes_searched = 0
def init_board():
global board
board = [0] * 65
board[1] = bROOK; board[2] = bKNIGHT; board[3] = bBISHOP; board[4] = bQUEEN
board[5] = bKING; board[6] = bBISHOP; board[7] = bKNIGHT; board[8] = bROOK
for i in range(9, 17):
board[i] = bPAWN
for i in range(49, 57):
board[i] = wPAWN
board[57] = wROOK; board[58] = wKNIGHT; board[59] = wBISHOP; board[60] = wQUEEN
board[61] = wKING; board[62] = wBISHOP; board[63] = wKNIGHT; board[64] = wROOK
def sq_row(sq):
return (sq - 1) // 8 + 1
def sq_col(sq):
return (sq - 1) % 8 + 1
def sq_to_alg(sq):
c = (sq - 1) % 8
r = 8 - (sq - 1) // 8
return "abcdefgh"[c] + str(r)
def generate_moves(side):
moves = []
for sq in range(1, 65):
pc = board[sq]
if pc == EMPTY:
continue
if pc >= 9:
pc_side = BLACK; pc_type = pc - 8
else:
pc_side = WHITE; pc_type = pc
if pc_side != side:
continue
row = sq_row(sq)
col = sq_col(sq)
if pc_type == 1: # Pawn
if side == WHITE:
tgt = sq - 8
if 1 <= tgt and board[tgt] == EMPTY:
moves.append((sq, tgt))
if row == 7:
tgt2 = sq - 16
if board[tgt2] == EMPTY:
moves.append((sq, tgt2))
if col > 1:
tgt = sq - 9
if tgt >= 1 and board[tgt] >= 9:
moves.append((sq, tgt))
if col < 8:
tgt = sq - 7
if tgt >= 1 and board[tgt] >= 9:
moves.append((sq, tgt))
else:
tgt = sq + 8
if tgt <= 64 and board[tgt] == EMPTY:
moves.append((sq, tgt))
if row == 2:
tgt2 = sq + 16
if board[tgt2] == EMPTY:
moves.append((sq, tgt2))
if col > 1:
tgt = sq + 7
if tgt <= 64 and 1 <= board[tgt] <= 6:
moves.append((sq, tgt))
if col < 8:
tgt = sq + 9
if tgt <= 64 and 1 <= board[tgt] <= 6:
moves.append((sq, tgt))
elif pc_type == 2: # Knight
for d in KN_DIR:
tgt = sq + d
if 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if abs(tr - row) + abs(tc - col) == 3:
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt))
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt))
elif pc_type == 3: # Bishop
for d in BP_DIR:
tgt = sq + d
cr, cc = row, col
while 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if abs(tc - cc) != abs(tr - cr):
break
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt)); break
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt)); break
else:
break
cr, cc = tr, tc
tgt += d
elif pc_type == 4: # Rook
for d in RK_DIR:
tgt = sq + d
while 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if d in (1, -1) and tr != row:
break
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt)); break
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt)); break
else:
break
tgt += d
elif pc_type == 5: # Queen = Rook + Bishop
for d in RK_DIR:
tgt = sq + d
while 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if d in (1, -1) and tr != row:
break
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt)); break
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt)); break
else:
break
tgt += d
cr, cc = row, col
for d in BP_DIR:
tgt = sq + d
cr, cc = row, col
while 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if abs(tc - cc) != abs(tr - cr):
break
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt)); break
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt)); break
else:
break
cr, cc = tr, tc
tgt += d
elif pc_type == 6: # King
for d in RK_DIR:
tgt = sq + d
if 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if d in (1, -1) and tr != row:
continue
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt))
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt))
for d in BP_DIR:
tgt = sq + d
if 1 <= tgt <= 64:
tr = sq_row(tgt); tc = sq_col(tgt)
if abs(tc - col) == 1 and abs(tr - row) == 1:
tp = board[tgt]
if tp == EMPTY:
moves.append((sq, tgt))
elif side == WHITE and tp >= 9:
moves.append((sq, tgt))
elif side == BLACK and 1 <= tp <= 6:
moves.append((sq, tgt))
return moves
def make_move(frm, to):
stk_from.append(frm)
stk_to.append(to)
stk_cap.append(board[to])
board[to] = board[frm]
board[frm] = EMPTY
def undo_move():
frm = stk_from.pop()
to = stk_to.pop()
cap = stk_cap.pop()
board[frm] = board[to]
board[to] = cap
def evaluate():
score = 0
for i in range(1, 65):
pc = board[i]
if pc == EMPTY:
continue
if 1 <= pc <= 6:
score += MAT_VAL[pc]
if pc == wPAWN: score += pst_pawn[i]
elif pc == wKNIGHT: score += pst_knight[i]
elif pc == wKING: score += pst_king[i]
elif 9 <= pc <= 14:
score -= MAT_VAL[pc]
mi = 65 - i
if pc == bPAWN: score -= pst_pawn[mi]
elif pc == bKNIGHT: score -= pst_knight[mi]
elif pc == bKING: score -= pst_king[mi]
return score
def minimax(depth, alpha, beta, mm_side):
global nodes_searched
nodes_searched += 1
if depth == 0:
s = evaluate()
return s if mm_side == WHITE else -s
moves = generate_moves(mm_side)
if not moves:
return -INF
# Order captures first for better pruning
def move_order(m):
cap = board[m[1]]
return -MAT_VAL[cap] if cap != EMPTY else 0
moves.sort(key=move_order)
best = -INF
next_side = BLACK if mm_side == WHITE else WHITE
for frm, to in moves:
make_move(frm, to)
score = -minimax(depth - 1, -beta, -alpha, next_side)
undo_move()
if score > best:
best = score
if best > alpha:
alpha = best
if alpha >= beta:
break
return best
def find_best_move(side):
global nodes_searched
nodes_searched = 0
moves = generate_moves(side)
if not moves:
return None, None
def move_order(m):
cap = board[m[1]]
return -MAT_VAL[cap] if cap != EMPTY else 0
moves.sort(key=move_order)
best_score = -INF
best_move = moves[0]
next_side = BLACK if side == WHITE else WHITE
for frm, to in moves:
make_move(frm, to)
score = -minimax(MAX_DEPTH - 1, -INF, -best_score, next_side)
undo_move()
if score > best_score:
best_score = score
best_move = (frm, to)
return best_move
def print_board():
pieces = {
EMPTY: '.', wPAWN: 'P', wKNIGHT: 'N', wBISHOP: 'B',
wROOK: 'R', wQUEEN: 'Q', wKING: 'K',
bPAWN: 'p', bKNIGHT: 'n', bBISHOP: 'b',
bROOK: 'r', bQUEEN: 'q', bKING: 'k',
}
print()
for r in range(1, 9):
rank = str(9 - r) + " "
for c in range(1, 9):
sq = (r - 1) * 8 + c
rank += " " + pieces.get(board[sq], "?")
print(rank)
print(" a b c d e f g h")
print()
def main():
init_board()
print("=== RPG CHESS ENGINE (Python verification) ===")
print(f"Negamax alpha-beta, depth {MAX_DEPTH}")
print_board()
t_total = time.time()
for move_num in range(1, 41):
# White
t0 = time.time()
wm = find_best_move(WHITE)
wt = time.time() - t0
if wm is None:
print("White has no moves.")
break
frm, to = wm
print(f"{move_num:2d}. {sq_to_alg(frm)}{sq_to_alg(to)} "
f"({nodes_searched:,} nodes, {wt:.1f}s)")
make_move(frm, to)
# Black
t0 = time.time()
bm = find_best_move(BLACK)
bt = time.time() - t0
if bm is None:
print("Black has no moves.")
break
frm, to = bm
print(f" {sq_to_alg(frm)}{sq_to_alg(to)} "
f"({nodes_searched:,} nodes, {bt:.1f}s)")
make_move(frm, to)
if move_num % 5 == 0:
print_board()
elapsed = time.time() - t_total
print(f"\n=== GAME COMPLETE ({elapsed:.1f}s) ===")
print_board()
if __name__ == "__main__":
main()