Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import chess
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
def init_board():
|
| 6 |
+
# Initialize a new chess board
|
| 7 |
+
board = chess.Board()
|
| 8 |
+
return board
|
| 9 |
+
|
| 10 |
+
def make_move(board, move):
|
| 11 |
+
try:
|
| 12 |
+
board.push_san(move)
|
| 13 |
+
except ValueError:
|
| 14 |
+
return "Invalid move. Please try again.", board
|
| 15 |
+
|
| 16 |
+
def computer_move(board, difficulty):
|
| 17 |
+
# Simple random AI for the computer move
|
| 18 |
+
legal_moves = list(board.legal_moves)
|
| 19 |
+
move = random.choice(legal_moves) # Random move as a simple AI example
|
| 20 |
+
board.push(move)
|
| 21 |
+
return board
|
| 22 |
+
|
| 23 |
+
def render_chessboard(board):
|
| 24 |
+
# Representing the chessboard as a string for simplicity (you can use libraries for a visual rendering)
|
| 25 |
+
return str(board)
|
| 26 |
+
|
| 27 |
+
def play_game(player_move, game_mode, difficulty):
|
| 28 |
+
board = init_board()
|
| 29 |
+
response = ""
|
| 30 |
+
|
| 31 |
+
if game_mode == 'Play with Computer':
|
| 32 |
+
if player_move:
|
| 33 |
+
response, board = make_move(board, player_move)
|
| 34 |
+
|
| 35 |
+
if board.is_game_over():
|
| 36 |
+
return response + "\nGame Over!", render_chessboard(board)
|
| 37 |
+
|
| 38 |
+
# Simulate computer move
|
| 39 |
+
board = computer_move(board, difficulty)
|
| 40 |
+
return response + f"\nComputer Move: {board.peek()}", render_chessboard(board)
|
| 41 |
+
|
| 42 |
+
elif game_mode == 'Two Player':
|
| 43 |
+
player1_move = player_move
|
| 44 |
+
if player1_move:
|
| 45 |
+
response, board = make_move(board, player1_move)
|
| 46 |
+
|
| 47 |
+
if board.is_game_over():
|
| 48 |
+
return response + "\nGame Over!", render_chessboard(board)
|
| 49 |
+
|
| 50 |
+
player2_move = input("Player 2 move (e.g., e7e5): ") # Asking Player 2
|
| 51 |
+
if player2_move:
|
| 52 |
+
response, board = make_move(board, player2_move)
|
| 53 |
+
|
| 54 |
+
return response, render_chessboard(board)
|
| 55 |
+
|
| 56 |
+
# Define Gradio UI components
|
| 57 |
+
def gradio_interface():
|
| 58 |
+
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("# 3D Chess Game")
|
| 60 |
+
|
| 61 |
+
game_mode = gr.Radio(choices=["Play with Computer", "Two Player"], label="Game Mode", value="Play with Computer")
|
| 62 |
+
difficulty = gr.Slider(minimum=1, maximum=10, label="Difficulty", value=3)
|
| 63 |
+
player_move = gr.Textbox(label="Your move (e.g., e2e4): ")
|
| 64 |
+
|
| 65 |
+
output_text = gr.Textbox(label="Game Status", interactive=False)
|
| 66 |
+
chessboard_output = gr.Textbox(label="Current Board", interactive=False)
|
| 67 |
+
|
| 68 |
+
# Button for making moves and starting game
|
| 69 |
+
play_button = gr.Button("Play Move")
|
| 70 |
+
|
| 71 |
+
play_button.click(
|
| 72 |
+
play_game,
|
| 73 |
+
inputs=[player_move, game_mode, difficulty],
|
| 74 |
+
outputs=[output_text, chessboard_output]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|
| 78 |
+
|
| 79 |
+
# Run Gradio app
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
gradio_interface()
|