Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
-
import random, time
|
4 |
|
5 |
DIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
|
6 |
|
7 |
history_log = []
|
8 |
|
9 |
-
#
|
10 |
try:
|
11 |
-
FONT_L = ImageFont.truetype("DejaVuSans-Bold.ttf",
|
12 |
-
FONT_S = ImageFont.truetype("DejaVuSans-Bold.ttf",
|
13 |
except:
|
14 |
FONT_L = ImageFont.load_default()
|
15 |
FONT_S = ImageFont.load_default()
|
@@ -53,19 +53,19 @@ def is_game_over(board):
|
|
53 |
def board_to_image(board, state):
|
54 |
board_state, player, last_user, last_ai, history = state
|
55 |
size, cell = 400, 400//8
|
56 |
-
img=Image.new('RGB',(size,size+cell*
|
57 |
draw=ImageDraw.Draw(img)
|
58 |
|
59 |
b,w=count_score(board_state)
|
60 |
draw.rectangle([0,0,size,cell],fill='navy')
|
61 |
draw.text((10,2),f"BLACK: {b}",font=FONT_L,fill='white')
|
62 |
-
draw.text((size-
|
63 |
|
64 |
if is_game_over(board_state):
|
65 |
-
winner = "
|
66 |
draw.rectangle([0,cell,size,cell*2],fill='darkred')
|
67 |
-
draw.text((size//2-
|
68 |
-
draw.text((size//2-
|
69 |
|
70 |
for r in range(8):
|
71 |
for c in range(8):
|
@@ -82,8 +82,10 @@ def board_to_image(board, state):
|
|
82 |
draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
|
83 |
|
84 |
y=cell*2+8*cell+10
|
85 |
-
|
86 |
-
draw.text((10,y),
|
|
|
|
|
87 |
|
88 |
return img
|
89 |
|
@@ -91,7 +93,6 @@ def click_handler(evt, state):
|
|
91 |
if state is None:
|
92 |
state = (initialize_board(), -1, None, None, [])
|
93 |
board, player, lu, la, history = state
|
94 |
-
# Handle evt.index callable vs attribute
|
95 |
if callable(evt.index):
|
96 |
x, y = evt.index()
|
97 |
else:
|
@@ -106,7 +107,7 @@ def click_handler(evt, state):
|
|
106 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
107 |
|
108 |
if is_game_over(board):
|
109 |
-
winner = "
|
110 |
history.append(f"Game {len(history)+1}: {winner}")
|
111 |
history_log.append(f"Game {len(history_log)+1}: {winner}")
|
112 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
@@ -133,5 +134,5 @@ with gr.Blocks() as demo:
|
|
133 |
img=gr.Image(value=board_to_image(initialize_board(),(initialize_board(),-1,None,None,[])),interactive=True)
|
134 |
new_btn=gr.Button("New Game")
|
135 |
img.select(click_handler,inputs=[state],outputs=[img,state])
|
136 |
-
new_btn.click(fn=
|
137 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import random, time
|
4 |
|
5 |
DIRECTIONS = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
|
6 |
|
7 |
history_log = []
|
8 |
|
9 |
+
# Load custom fonts or fall back
|
10 |
try:
|
11 |
+
FONT_L = ImageFont.truetype("DejaVuSans-Bold.ttf", 36)
|
12 |
+
FONT_S = ImageFont.truetype("DejaVuSans-Bold.ttf", 20)
|
13 |
except:
|
14 |
FONT_L = ImageFont.load_default()
|
15 |
FONT_S = ImageFont.load_default()
|
|
|
53 |
def board_to_image(board, state):
|
54 |
board_state, player, last_user, last_ai, history = state
|
55 |
size, cell = 400, 400//8
|
56 |
+
img=Image.new('RGB',(size,size+cell*5),'darkgreen')
|
57 |
draw=ImageDraw.Draw(img)
|
58 |
|
59 |
b,w=count_score(board_state)
|
60 |
draw.rectangle([0,0,size,cell],fill='navy')
|
61 |
draw.text((10,2),f"BLACK: {b}",font=FONT_L,fill='white')
|
62 |
+
draw.text((size-200,2),f"WHITE: {w}",font=FONT_L,fill='white')
|
63 |
|
64 |
if is_game_over(board_state):
|
65 |
+
winner = "DRAW" if b==w else ("BLACK WINS" if b>w else "WHITE WINS")
|
66 |
draw.rectangle([0,cell,size,cell*2],fill='darkred')
|
67 |
+
draw.text((size//2-120,cell+2),winner,font=FONT_L,fill='yellow')
|
68 |
+
draw.text((size//2-120,cell+30),"Click 'New Game' to restart",font=FONT_S,fill='white')
|
69 |
|
70 |
for r in range(8):
|
71 |
for c in range(8):
|
|
|
82 |
draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
|
83 |
|
84 |
y=cell*2+8*cell+10
|
85 |
+
if history:
|
86 |
+
draw.text((10,y),"History:",font=FONT_S,fill='white'); y+=cell//2
|
87 |
+
for res in history[-5:]:
|
88 |
+
draw.text((10,y),res,font=FONT_S,fill='white'); y+=cell//2
|
89 |
|
90 |
return img
|
91 |
|
|
|
93 |
if state is None:
|
94 |
state = (initialize_board(), -1, None, None, [])
|
95 |
board, player, lu, la, history = state
|
|
|
96 |
if callable(evt.index):
|
97 |
x, y = evt.index()
|
98 |
else:
|
|
|
107 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
108 |
|
109 |
if is_game_over(board):
|
110 |
+
winner = "DRAW" if count_score(board)[0] == count_score(board)[1] else ("BLACK" if count_score(board)[0] > count_score(board)[1] else "WHITE")
|
111 |
history.append(f"Game {len(history)+1}: {winner}")
|
112 |
history_log.append(f"Game {len(history_log)+1}: {winner}")
|
113 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
|
|
134 |
img=gr.Image(value=board_to_image(initialize_board(),(initialize_board(),-1,None,None,[])),interactive=True)
|
135 |
new_btn=gr.Button("New Game")
|
136 |
img.select(click_handler,inputs=[state],outputs=[img,state])
|
137 |
+
new_btn.click(fn=lambda: (initialize_board(), -1, None, None, history_log.copy()), outputs=[state, img])
|
138 |
demo.launch()
|