ParallaxOpen's picture
Upload play.py with huggingface_hub
1ab7c42 verified
Raw History Blame Contribute Delete
1.94 kB
#!/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())