Update app.py
Browse files
app.py
CHANGED
@@ -79,25 +79,37 @@ def board_to_image(board, state):
|
|
79 |
return img
|
80 |
|
81 |
def click_handler(evt, state):
|
|
|
|
|
|
|
82 |
board, player, lu, la, history = state
|
83 |
-
x,y=evt.index
|
84 |
-
|
85 |
-
|
86 |
-
#
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
89 |
if is_game_over(board):
|
90 |
-
|
91 |
-
|
|
|
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 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
99 |
|
100 |
-
def reset_handler
|
|
|
101 |
return initialize_board(),-1,None,None,[]
|
102 |
|
103 |
with gr.Blocks() as demo:
|
|
|
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 = 360 // 8
|
88 |
+
c, r = int(x // cell), int((y - cell * 2) // cell)
|
89 |
+
# Player move
|
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 |
+
# Update after player
|
94 |
+
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
95 |
+
# Check end
|
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 |
+
# AI thinking indicator
|
102 |
+
yield board_to_image(board, (board, player, lu, la, history)), (board, player, lu, la, history)
|
103 |
time.sleep(2)
|
104 |
+
# AI move
|
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 |
+
# def reset_handler placeholders for replacement
|
112 |
+
#def reset_handler():
|
113 |
return initialize_board(),-1,None,None,[]
|
114 |
|
115 |
with gr.Blocks() as demo:
|