Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,19 @@
|
|
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 |
def initialize_board():
|
8 |
b = [[0]*8 for _ in range(8)]
|
9 |
b[3][3], b[4][4] = 1, 1
|
@@ -40,25 +50,23 @@ def count_score(board):
|
|
40 |
def is_game_over(board):
|
41 |
return not any(get_flips(board,r,c,p) for p in (-1,1) for r in range(8) for c in range(8))
|
42 |
|
43 |
-
FONT_L = ImageFont.load_default()
|
44 |
-
FONT_S = ImageFont.load_default()
|
45 |
-
|
46 |
def board_to_image(board, state):
|
47 |
board_state, player, last_user, last_ai, history = state
|
48 |
-
size, cell =
|
49 |
-
img=Image.new('RGB',(size,size+cell*
|
50 |
draw=ImageDraw.Draw(img)
|
51 |
-
|
52 |
b,w=count_score(board_state)
|
53 |
draw.rectangle([0,0,size,cell],fill='navy')
|
54 |
draw.text((10,2),f"BLACK: {b}",font=FONT_L,fill='white')
|
55 |
-
draw.text((size-
|
56 |
-
|
57 |
if is_game_over(board_state):
|
58 |
-
winner = "Draw" if b==w else ("
|
59 |
-
draw.rectangle([0,cell,size,cell*2],fill='
|
60 |
-
draw.text((size//2-
|
61 |
-
|
|
|
62 |
for r in range(8):
|
63 |
for c in range(8):
|
64 |
x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell
|
@@ -67,50 +75,54 @@ def board_to_image(board, state):
|
|
67 |
draw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')
|
68 |
if board_state[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')
|
69 |
if board_state[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')
|
70 |
-
|
71 |
for mark,clr in ((last_user,'red'),(last_ai,'blue')):
|
72 |
if mark:
|
73 |
mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+cell
|
74 |
draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
|
75 |
-
|
76 |
-
y=cell*2+8*cell+
|
77 |
for res in history[-5:]:
|
78 |
draw.text((10,y),res,font=FONT_S,fill='white'); y+=cell//2
|
|
|
79 |
return img
|
80 |
|
81 |
def click_handler(evt, state):
|
82 |
-
# Ensure state initialized
|
83 |
if state is None:
|
84 |
state = (initialize_board(), -1, None, None, [])
|
85 |
board, player, lu, la, history = state
|
86 |
x, y = evt.index
|
87 |
-
cell =
|
88 |
c, r = int(x // cell), int((y - cell * 2) // cell)
|
89 |
-
|
90 |
if 0 <= r < 8 and 0 <= c < 8 and not is_game_over(board):
|
91 |
if apply_move(board, r, c, player):
|
92 |
lu, player = (r, c), -player
|
93 |
-
|
94 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
95 |
-
|
96 |
if is_game_over(board):
|
97 |
winner = "Draw" if count_score(board)[0] == count_score(board)[1] else ("Black" if count_score(board)[0] > count_score(board)[1] else "White")
|
98 |
history.append(f"Game {len(history)+1}: {winner}")
|
|
|
99 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
100 |
return
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
105 |
ai_mv = choose_move(board, player)
|
106 |
if ai_mv:
|
107 |
apply_move(board, ai_mv[0], ai_mv[1], player)
|
108 |
la, player = ai_mv, -player
|
109 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
return
|
114 |
|
115 |
with gr.Blocks() as demo:
|
116 |
state=gr.State((initialize_board(),-1,None,None,[]))
|
@@ -118,4 +130,4 @@ with gr.Blocks() as demo:
|
|
118 |
new_btn=gr.Button("New Game")
|
119 |
img.select(click_handler,inputs=[state],outputs=[img,state])
|
120 |
new_btn.click(fn=reset_handler,outputs=[state,img])
|
121 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import random, time, os
|
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 |
+
# Attempt to load larger font
|
10 |
+
try:
|
11 |
+
FONT_L = ImageFont.truetype("DejaVuSans-Bold.ttf", 28)
|
12 |
+
FONT_S = ImageFont.truetype("DejaVuSans-Bold.ttf", 16)
|
13 |
+
except:
|
14 |
+
FONT_L = ImageFont.load_default()
|
15 |
+
FONT_S = ImageFont.load_default()
|
16 |
+
|
17 |
def initialize_board():
|
18 |
b = [[0]*8 for _ in range(8)]
|
19 |
b[3][3], b[4][4] = 1, 1
|
|
|
50 |
def is_game_over(board):
|
51 |
return not any(get_flips(board,r,c,p) for p in (-1,1) for r in range(8) for c in range(8))
|
52 |
|
|
|
|
|
|
|
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*4),'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-180,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-110,cell+2),winner,font=FONT_L,fill='yellow')
|
68 |
+
draw.text((size//2-90,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):
|
72 |
x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell
|
|
|
75 |
draw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')
|
76 |
if board_state[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')
|
77 |
if board_state[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')
|
78 |
+
|
79 |
for mark,clr in ((last_user,'red'),(last_ai,'blue')):
|
80 |
if mark:
|
81 |
mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+cell
|
82 |
draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
|
83 |
+
|
84 |
+
y=cell*2+8*cell+10
|
85 |
for res in history[-5:]:
|
86 |
draw.text((10,y),res,font=FONT_S,fill='white'); y+=cell//2
|
87 |
+
|
88 |
return img
|
89 |
|
90 |
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 |
x, y = evt.index
|
95 |
+
cell = 400 // 8
|
96 |
c, r = int(x // cell), int((y - cell * 2) // cell)
|
97 |
+
|
98 |
if 0 <= r < 8 and 0 <= c < 8 and not is_game_over(board):
|
99 |
if apply_move(board, r, c, player):
|
100 |
lu, player = (r, c), -player
|
101 |
+
|
102 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
103 |
+
|
104 |
if is_game_over(board):
|
105 |
winner = "Draw" if count_score(board)[0] == count_score(board)[1] else ("Black" if count_score(board)[0] > count_score(board)[1] else "White")
|
106 |
history.append(f"Game {len(history)+1}: {winner}")
|
107 |
+
history_log.append(f"Game {len(history_log)+1}: {winner}")
|
108 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
109 |
return
|
110 |
+
|
111 |
+
thinking_img = board_to_image(board, (board, player, lu, la, history))
|
112 |
+
draw = ImageDraw.Draw(thinking_img)
|
113 |
+
draw.text((150, 400 - 25), "AI Thinking...", font=FONT_S, fill='cyan')
|
114 |
+
yield thinking_img, (board, player, lu, la, history)
|
115 |
+
time.sleep(1.5)
|
116 |
+
|
117 |
ai_mv = choose_move(board, player)
|
118 |
if ai_mv:
|
119 |
apply_move(board, ai_mv[0], ai_mv[1], player)
|
120 |
la, player = ai_mv, -player
|
121 |
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
122 |
|
123 |
+
def reset_handler():
|
124 |
+
new_board = initialize_board()
|
125 |
+
return new_board, -1, None, None, history_log.copy()
|
126 |
|
127 |
with gr.Blocks() as demo:
|
128 |
state=gr.State((initialize_board(),-1,None,None,[]))
|
|
|
130 |
new_btn=gr.Button("New Game")
|
131 |
img.select(click_handler,inputs=[state],outputs=[img,state])
|
132 |
new_btn.click(fn=reset_handler,outputs=[state,img])
|
133 |
+
demo.launch()
|