|
import gradio as gr |
|
import pygame |
|
import numpy as np |
|
from Scripts.Engine import * |
|
from Scripts.Variables import * |
|
|
|
|
|
pygame.init() |
|
|
|
|
|
display = None |
|
board = Board.newBoard() |
|
|
|
|
|
def draw_board_on_gradio(): |
|
|
|
surface = pygame.Surface((dimension, dimension)) |
|
surface.fill((255, 255, 255)) |
|
|
|
Board.draw_board(surface) |
|
Board.draw_pieces(board, surface) |
|
|
|
|
|
image = pygame.image.tostring(surface, 'RGB') |
|
return image |
|
|
|
|
|
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) |
|
|
|
|
|
piece = board[from_row][from_col] |
|
board[to_row][to_col] = piece |
|
board[from_row][from_col] = 0 |
|
|
|
|
|
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 |
|
|
|
|
|
chance, next = next, chance |
|
|
|
|
|
Board.check_promotion(board) |
|
|
|
|
|
return draw_board_on_gradio() |
|
|
|
|
|
def reset_board(): |
|
global board |
|
board = Board.newBoard() |
|
return draw_board_on_gradio() |
|
|
|
|
|
iface = gr.Interface( |
|
fn=make_move, |
|
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, |
|
title="Checkers AI vs AI", |
|
description="Play Checkers. Move your pieces by selecting start and end positions." |
|
) |
|
|
|
|
|
reset_button = gr.Button("Reset Board", reset_board) |
|
|
|
|
|
iface.launch() |
|
|
|
|