File size: 1,939 Bytes
1ab7c42 | 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 | #!/usr/bin/env python3
"""Interactive Vela-Chess. Run this in your terminal."""
import chess, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from vela_chess_engine_v2 import VelaChessV2
UPPER = {'P':'P','N':'N','B':'B','R':'R','Q':'Q','K':'K'}
LOWER = {'p':'p','n':'n','b':'b','r':'r','q':'q','k':'k'}
def show(board):
print()
print(" a b c d e f g h")
print(" +---------------+")
for r in range(7, -1, -1):
row = "%d |" % (r + 1)
for f in range(8):
p = board.piece_at(chess.square(f, r))
if p:
sym = UPPER.get(p.symbol()) if p.color == chess.WHITE else LOWER.get(p.symbol())
row += " " + sym
else:
row += " ."
row += " |"
print(row)
print(" +---------------+")
print(" a b c d e f g h")
print()
engine = VelaChessV2(str(Path(__file__).parent.parent / "checkpoints" / "vela_chess_v2" / "final.pt"))
board = chess.Board()
print("=== VELA-CHESS ===")
print("You = WHITE (uppercase)")
print("Vela = BLACK (lowercase)")
print("Commands: UCI move (e2e4), 'moves', 'quit'")
print()
show(board)
while not board.is_game_over():
if board.turn == chess.WHITE:
cmd = input("Your move: ").strip().lower()
if cmd == "quit":
break
if cmd == "moves":
print(", ".join(m.uci() for m in board.legal_moves))
continue
try:
move = chess.Move.from_uci(cmd)
except:
print("Bad format. Use UCI: e2e4")
continue
if move not in board.legal_moves:
print("Illegal! Type 'moves' for legal moves")
continue
else:
print("Vela thinking...")
move = engine.choose_move(board)
cmd = move.uci()
print("Vela plays: %s" % cmd)
board.push(move)
show(board)
print("Game over: %s" % board.result())
|