File size: 2,281 Bytes
4a95442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import pygame
import numpy as np
from Scripts.Engine import *
from Scripts.Variables import *

# Initialize the Pygame environment
pygame.init()

# Global variables
display = None
board = Board.newBoard()

# Function to create a checkers board
def draw_board_on_gradio():
    # Create a new surface to draw the board on
    surface = pygame.Surface((dimension, dimension))
    surface.fill((255, 255, 255))  # Fill with white background
    
    Board.draw_board(surface)  # Draw the board grid
    Board.draw_pieces(board, surface)  # Draw the pieces on the board

    # Convert Pygame surface to image that Gradio can display
    image = pygame.image.tostring(surface, 'RGB')
    return image

# Function to handle player moves
def make_move(from_pos, to_pos):
    global board, chance, next

    from_row, from_col = helper.toIndex(from_pos)
    to_row, to_col = helper.toIndex(to_pos)

    # Make the move
    piece = board[from_row][from_col]
    board[to_row][to_col] = piece
    board[from_row][from_col] = 0

    # Handle capture logic (remove captured piece)
    if abs(from_row - to_row) > 1 or abs(from_col - to_col) > 1:
        captured_row = (from_row + to_row) // 2
        captured_col = (from_col + to_col) // 2
        board[captured_row][captured_col] = 0

    # Switch turn
    chance, next = next, chance

    # Check for promotion
    Board.check_promotion(board)

    # Return updated board image
    return draw_board_on_gradio()

# Function to handle reset
def reset_board():
    global board
    board = Board.newBoard()
    return draw_board_on_gradio()

# Gradio Interface setup
iface = gr.Interface(
    fn=make_move,  # Function to handle the move action
    inputs=[
        gr.inputs.Dropdown(choices=[(f'{i},{j}') for i in range(8) for j in range(8)], label="From Position"),
        gr.inputs.Dropdown(choices=[(f'{i},{j}') for i in range(8) for j in range(8)], label="To Position")
    ],
    outputs=gr.outputs.Image(label="Checkers Board"),
    live=True,  # Real-time interaction
    title="Checkers AI vs AI",
    description="Play Checkers. Move your pieces by selecting start and end positions."
)

# Button to reset the board
reset_button = gr.Button("Reset Board", reset_board)

# Launch the interface
iface.launch()