Update app.py
Browse files
app.py
CHANGED
@@ -38,24 +38,27 @@ def count_score(board):
|
|
38 |
return b,w
|
39 |
|
40 |
def is_game_over(board):
|
41 |
-
return not any(get_flips(board,r,c,p) for p in
|
42 |
|
43 |
-
|
44 |
-
|
45 |
|
46 |
def board_to_image(board, state):
|
47 |
board_state, player, last_user, last_ai, history = state
|
48 |
-
size=360
|
49 |
img=Image.new('RGB',(size,size+cell*2),'darkgreen')
|
50 |
draw=ImageDraw.Draw(img)
|
|
|
51 |
b,w=count_score(board_state)
|
52 |
draw.rectangle([0,0,size,cell],fill='navy')
|
53 |
-
draw.text((10,2),f"BLACK: {b}",font=
|
54 |
-
draw.text((size-160,2),f"WHITE: {w}",font=
|
|
|
55 |
if is_game_over(board_state):
|
56 |
winner = "Draw" if b==w else ("Black Wins" if b>w else "White Wins")
|
57 |
draw.rectangle([0,cell,size,cell*2],fill='maroon')
|
58 |
-
draw.text((size//2-80,cell+2),winner,font=
|
|
|
59 |
for r in range(8):
|
60 |
for c in range(8):
|
61 |
x0,y0=c*cell,cell*2+r*cell; x1,y1=x0+cell,y0+cell
|
@@ -64,29 +67,34 @@ def board_to_image(board, state):
|
|
64 |
draw.ellipse([x0+cell*0.4,y0+cell*0.4,x0+cell*0.6,y0+cell*0.6],fill='yellow')
|
65 |
if board_state[r][c]==1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='white')
|
66 |
if board_state[r][c]==-1: draw.ellipse([x0+4,y0+4,x1-4,y1-4],fill='black')
|
|
|
67 |
for mark,clr in ((last_user,'red'),(last_ai,'blue')):
|
68 |
if mark:
|
69 |
mr,mc=mark; x0,y0=mc*cell,cell*2+mr*cell; x1,y1=x0+cell,y0+cell
|
70 |
draw.rectangle([x0,y0,x1,y1],outline=clr,width=4)
|
|
|
71 |
y=cell*2+8*cell+2
|
72 |
for res in history[-5:]:
|
73 |
-
draw.text((10,y),res,font=
|
74 |
-
y+=cell//2
|
75 |
return img
|
76 |
|
77 |
-
def click_handler(evt,state):
|
78 |
-
board,player,lu,la,history=state
|
79 |
x,y=evt.index; cell=360//8; c,r=int(x//cell),int((y-cell*2)//cell)
|
80 |
if 0<=r<8 and 0<=c<8 and not is_game_over(board):
|
81 |
-
if apply_move(board,r,c,player): lu=(r,c)
|
|
|
82 |
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
|
|
83 |
if is_game_over(board):
|
84 |
-
history.append(f"Game {len(history)+1}:
|
|
|
85 |
return
|
|
|
86 |
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
87 |
time.sleep(2)
|
88 |
ai_mv=choose_move(board,player)
|
89 |
-
if ai_mv: apply_move(board,ai_mv[0],ai_mv[1],player); la=ai_mv
|
90 |
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
91 |
|
92 |
def reset_handler():
|
|
|
38 |
return b,w
|
39 |
|
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 = 360, 360//8
|
49 |
img=Image.new('RGB',(size,size+cell*2),'darkgreen')
|
50 |
draw=ImageDraw.Draw(img)
|
51 |
+
# Scoreboard
|
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-160,2),f"WHITE: {w}",font=FONT_L,fill='white')
|
56 |
+
# Game over
|
57 |
if is_game_over(board_state):
|
58 |
winner = "Draw" if b==w else ("Black Wins" if b>w else "White Wins")
|
59 |
draw.rectangle([0,cell,size,cell*2],fill='maroon')
|
60 |
+
draw.text((size//2-80,cell+2),winner,font=FONT_L,fill='yellow')
|
61 |
+
# Board
|
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 |
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 |
+
# Last moves
|
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 |
+
# History
|
76 |
y=cell*2+8*cell+2
|
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 |
+
board, player, lu, la, history = state
|
83 |
x,y=evt.index; cell=360//8; c,r=int(x//cell),int((y-cell*2)//cell)
|
84 |
if 0<=r<8 and 0<=c<8 and not is_game_over(board):
|
85 |
+
if apply_move(board,r,c,player): lu,player=( (r,c), -player )
|
86 |
+
# after user
|
87 |
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
88 |
+
# if game over
|
89 |
if is_game_over(board):
|
90 |
+
history.append(f"Game {len(history)+1}: " + ("Draw" if count_score(board)[0]==count_score(board)[1] else ("Black" if count_score(board)[0]>count_score(board)[1] else "White")))
|
91 |
+
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
92 |
return
|
93 |
+
# AI thinking
|
94 |
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
95 |
time.sleep(2)
|
96 |
ai_mv=choose_move(board,player)
|
97 |
+
if ai_mv: apply_move(board,ai_mv[0],ai_mv[1],player); la,player=( ai_mv, -player )
|
98 |
yield board_to_image(board,(board,player,lu,la,history)),(board,player,lu,la,history)
|
99 |
|
100 |
def reset_handler():
|