Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
# Othello (Reversi) logic implementation
|
4 |
def initialize_board():
|
@@ -37,23 +38,21 @@ def apply_move(board, row, col, player):
|
|
37 |
board[r][c] = player
|
38 |
return True
|
39 |
|
40 |
-
# Generate
|
41 |
-
def
|
42 |
-
|
|
|
|
|
43 |
for r in range(8):
|
44 |
-
html += '<tr>'
|
45 |
for c in range(8):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
html += '</tr>'
|
55 |
-
html += '</table>'
|
56 |
-
return html
|
57 |
|
58 |
# Count score
|
59 |
def count_score(board):
|
@@ -61,34 +60,32 @@ def count_score(board):
|
|
61 |
white = sum(cell == 1 for row in board for cell in row)
|
62 |
return black, white
|
63 |
|
64 |
-
# Gradio interaction
|
65 |
-
def play_move(
|
66 |
board, player = state
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
if not apply_move(board, row, col, player):
|
71 |
-
return
|
72 |
# Switch player
|
73 |
player = -player
|
74 |
black_score, white_score = count_score(board)
|
75 |
status = f"Black: {black_score} | White: {white_score} | {'Black' if player == -1 else 'White'} to move"
|
76 |
-
return
|
77 |
|
78 |
# Initialize Gradio app
|
79 |
def main():
|
80 |
with gr.Blocks() as demo:
|
81 |
gr.HTML("<h2>Othello (Reversi) Game</h2>")
|
82 |
-
|
|
|
83 |
status = gr.Text(value="Black: 2 | White: 2 | Black to move", interactive=False)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
state = gr.State((initialize_board(), -1)) # -1: Black, 1: White
|
89 |
-
submit.click(fn=play_move,
|
90 |
-
inputs=[row_input, col_input, state],
|
91 |
-
outputs=[board_html, status, state])
|
92 |
demo.launch()
|
93 |
|
94 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
|
4 |
# Othello (Reversi) logic implementation
|
5 |
def initialize_board():
|
|
|
38 |
board[r][c] = player
|
39 |
return True
|
40 |
|
41 |
+
# Generate PIL image representation
|
42 |
+
def board_to_image(board, img_size=320):
|
43 |
+
cell_size = img_size // 8
|
44 |
+
img = Image.new('RGB', (img_size, img_size), 'green')
|
45 |
+
draw = ImageDraw.Draw(img)
|
46 |
for r in range(8):
|
|
|
47 |
for c in range(8):
|
48 |
+
x0, y0 = c * cell_size, r * cell_size
|
49 |
+
x1, y1 = x0 + cell_size, y0 + cell_size
|
50 |
+
draw.rectangle([x0, y0, x1, y1], outline='black')
|
51 |
+
if board[r][c] == 1:
|
52 |
+
draw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='white')
|
53 |
+
elif board[r][c] == -1:
|
54 |
+
draw.ellipse([x0+4, y0+4, x1-4, y1-4], fill='black')
|
55 |
+
return img
|
|
|
|
|
|
|
56 |
|
57 |
# Count score
|
58 |
def count_score(board):
|
|
|
60 |
white = sum(cell == 1 for row in board for cell in row)
|
61 |
return black, white
|
62 |
|
63 |
+
# Gradio interaction with clickable board
|
64 |
+
def play_move(evt, state):
|
65 |
board, player = state
|
66 |
+
img_size = 320
|
67 |
+
cell_size = img_size / 8
|
68 |
+
x, y = evt['x'], evt['y']
|
69 |
+
col, row = int(x // cell_size), int(y // cell_size)
|
70 |
if not apply_move(board, row, col, player):
|
71 |
+
return board_to_image(board), f"Invalid move at ({row+1},{col+1})", state
|
72 |
# Switch player
|
73 |
player = -player
|
74 |
black_score, white_score = count_score(board)
|
75 |
status = f"Black: {black_score} | White: {white_score} | {'Black' if player == -1 else 'White'} to move"
|
76 |
+
return board_to_image(board), status, (board, player)
|
77 |
|
78 |
# Initialize Gradio app
|
79 |
def main():
|
80 |
with gr.Blocks() as demo:
|
81 |
gr.HTML("<h2>Othello (Reversi) Game</h2>")
|
82 |
+
state = gr.State((initialize_board(), -1)) # -1: Black starts
|
83 |
+
board_img = gr.Image(value=board_to_image(initialize_board()), interactive=True, tool="click", label="Click to place a stone")
|
84 |
status = gr.Text(value="Black: 2 | White: 2 | Black to move", interactive=False)
|
85 |
+
# Bind click event
|
86 |
+
board_img.select(fn=play_move,
|
87 |
+
inputs=[state],
|
88 |
+
outputs=[board_img, status, state])
|
|
|
|
|
|
|
|
|
89 |
demo.launch()
|
90 |
|
91 |
if __name__ == "__main__":
|