Spaces:
Runtime error
Runtime error
| import chess | |
| import gradio as gr | |
| from stockfish import Stockfish | |
| # Use local stockfish binary included in the repo (make sure it's chmod +x) | |
| stockfish = Stockfish(path="./stockfish", depth=18, parameters={ | |
| "Threads": 2, | |
| "Minimum Thinking Time": 30 | |
| }) | |
| board = chess.Board() | |
| def make_move(move_uci): | |
| global board | |
| if board.is_game_over(): | |
| return board.fen(), "Game over. Reset to play again." | |
| try: | |
| move = chess.Move.from_uci(move_uci) | |
| if move in board.legal_moves: | |
| board.push(move) | |
| return board.fen(), "Move played" | |
| else: | |
| return board.fen(), "Illegal move" | |
| except: | |
| return board.fen(), "Invalid move format (e.g., e2e4)" | |
| def get_cheat_move(): | |
| stockfish.set_fen_position(board.fen()) | |
| best = stockfish.get_best_move() | |
| return best | |
| def reset_board(): | |
| global board | |
| board = chess.Board() | |
| return board.fen(), "Board reset" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ♟️ Chess Wizard: Grandmaster Cheat Assistant") | |
| gr.Markdown("Play legal chess with discreet Stockfish-powered move suggestions.") | |
| move_input = gr.Textbox(label="Your Move (e.g., e2e4)") | |
| move_btn = gr.Button("Make Move") | |
| cheat_btn = gr.Button("Suggest Best Move") | |
| reset_btn = gr.Button("Reset Board") | |
| board_output = gr.Textbox(label="Current Board FEN", value=board.fen(), interactive=False) | |
| status_output = gr.Textbox(label="Status", interactive=False) | |
| cheat_output = gr.Textbox(label="Cheat Move Suggestion", interactive=False) | |
| move_btn.click(fn=make_move, inputs=move_input, outputs=[board_output, status_output]) | |
| cheat_btn.click(fn=get_cheat_move, outputs=cheat_output) | |
| reset_btn.click(fn=reset_board, outputs=[board_output, status_output]) | |
| demo.launch() | |