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()