Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pygame
|
3 |
+
import numpy as np
|
4 |
+
from Scripts.Engine import *
|
5 |
+
from Scripts.Variables import *
|
6 |
+
|
7 |
+
# Initialize the Pygame environment
|
8 |
+
pygame.init()
|
9 |
+
|
10 |
+
# Global variables
|
11 |
+
display = None
|
12 |
+
board = Board.newBoard()
|
13 |
+
|
14 |
+
# Function to create a checkers board
|
15 |
+
def draw_board_on_gradio():
|
16 |
+
# Create a new surface to draw the board on
|
17 |
+
surface = pygame.Surface((dimension, dimension))
|
18 |
+
surface.fill((255, 255, 255)) # Fill with white background
|
19 |
+
|
20 |
+
Board.draw_board(surface) # Draw the board grid
|
21 |
+
Board.draw_pieces(board, surface) # Draw the pieces on the board
|
22 |
+
|
23 |
+
# Convert Pygame surface to image that Gradio can display
|
24 |
+
image = pygame.image.tostring(surface, 'RGB')
|
25 |
+
return image
|
26 |
+
|
27 |
+
# Function to handle player moves
|
28 |
+
def make_move(from_pos, to_pos):
|
29 |
+
global board, chance, next
|
30 |
+
|
31 |
+
from_row, from_col = helper.toIndex(from_pos)
|
32 |
+
to_row, to_col = helper.toIndex(to_pos)
|
33 |
+
|
34 |
+
# Make the move
|
35 |
+
piece = board[from_row][from_col]
|
36 |
+
board[to_row][to_col] = piece
|
37 |
+
board[from_row][from_col] = 0
|
38 |
+
|
39 |
+
# Handle capture logic (remove captured piece)
|
40 |
+
if abs(from_row - to_row) > 1 or abs(from_col - to_col) > 1:
|
41 |
+
captured_row = (from_row + to_row) // 2
|
42 |
+
captured_col = (from_col + to_col) // 2
|
43 |
+
board[captured_row][captured_col] = 0
|
44 |
+
|
45 |
+
# Switch turn
|
46 |
+
chance, next = next, chance
|
47 |
+
|
48 |
+
# Check for promotion
|
49 |
+
Board.check_promotion(board)
|
50 |
+
|
51 |
+
# Return updated board image
|
52 |
+
return draw_board_on_gradio()
|
53 |
+
|
54 |
+
# Function to handle reset
|
55 |
+
def reset_board():
|
56 |
+
global board
|
57 |
+
board = Board.newBoard()
|
58 |
+
return draw_board_on_gradio()
|
59 |
+
|
60 |
+
# Gradio Interface setup
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=make_move, # Function to handle the move action
|
63 |
+
inputs=[
|
64 |
+
gr.inputs.Dropdown(choices=[(f'{i},{j}') for i in range(8) for j in range(8)], label="From Position"),
|
65 |
+
gr.inputs.Dropdown(choices=[(f'{i},{j}') for i in range(8) for j in range(8)], label="To Position")
|
66 |
+
],
|
67 |
+
outputs=gr.outputs.Image(label="Checkers Board"),
|
68 |
+
live=True, # Real-time interaction
|
69 |
+
title="Checkers AI vs AI",
|
70 |
+
description="Play Checkers. Move your pieces by selecting start and end positions."
|
71 |
+
)
|
72 |
+
|
73 |
+
# Button to reset the board
|
74 |
+
reset_button = gr.Button("Reset Board", reset_board)
|
75 |
+
|
76 |
+
# Launch the interface
|
77 |
+
iface.launch()
|
78 |
+
|