Update app.py
Browse files
app.py
CHANGED
@@ -20,14 +20,14 @@ CAMPS = [
|
|
20 |
]
|
21 |
ESCAPES = [(i,j) for i in [0,8] for j in range(BOARD_SIZE)] + [(i,j) for j in [0,8] for i in range(BOARD_SIZE) if (i,j) not in CAMPS]
|
22 |
COLORS = {
|
23 |
-
'empty': (255, 255, 255),
|
24 |
-
'castle': (128, 128, 128),
|
25 |
-
'camp': (139, 69, 19),
|
26 |
-
'escape': (0, 255, 0),
|
27 |
-
'white': (255, 255, 255),
|
28 |
-
'black': (0, 0, 0),
|
29 |
-
'king': (255, 215, 0),
|
30 |
-
'highlight': (255, 255, 0)
|
31 |
}
|
32 |
|
33 |
# Game state class
|
@@ -322,9 +322,9 @@ def generate_board_image(state, selected_pos=None):
|
|
322 |
def click_board(state, selected_pos, evt: gr.SelectData):
|
323 |
if state.turn != 'WHITE':
|
324 |
return state, "It's the AI's turn", generate_board_image(state), selected_pos
|
325 |
-
|
326 |
-
|
327 |
-
pos = (
|
328 |
if selected_pos is None:
|
329 |
if state.board[pos] in [WHITE_SOLDIER, KING]:
|
330 |
return state, f"Selected {pos_to_coord(pos)}", generate_board_image(state, pos), pos
|
@@ -347,8 +347,9 @@ def new_game():
|
|
347 |
state = TablutState()
|
348 |
return state, "New game started. Your turn (White).", generate_board_image(state), None
|
349 |
|
350 |
-
# Gradio interface
|
351 |
-
|
|
|
352 |
state = gr.State()
|
353 |
selected_pos = gr.State(value=None)
|
354 |
board_image = gr.Image(label="Board", type="pil")
|
@@ -356,6 +357,4 @@ with gr.Blocks(title="Tablut Game") as demo:
|
|
356 |
new_game_button = gr.Button("New Game")
|
357 |
board_image.select(fn=click_board, inputs=[state, selected_pos], outputs=[state, message_label, board_image, selected_pos])
|
358 |
new_game_button.click(fn=new_game, outputs=[state, message_label, board_image, selected_pos])
|
359 |
-
demo.load(fn=new_game, outputs=[state, message_label, board_image, selected_pos])
|
360 |
-
|
361 |
-
# Note: demo.launch() is not needed for HF Spaces
|
|
|
20 |
]
|
21 |
ESCAPES = [(i,j) for i in [0,8] for j in range(BOARD_SIZE)] + [(i,j) for j in [0,8] for i in range(BOARD_SIZE) if (i,j) not in CAMPS]
|
22 |
COLORS = {
|
23 |
+
'empty': (255, 255, 255),
|
24 |
+
'castle': (128, 128, 128),
|
25 |
+
'camp': (139, 69, 19),
|
26 |
+
'escape': (0, 255, 0),
|
27 |
+
'white': (255, 255, 255),
|
28 |
+
'black': (0, 0, 0),
|
29 |
+
'king': (255, 215, 0),
|
30 |
+
'highlight': (255, 255, 0)
|
31 |
}
|
32 |
|
33 |
# Game state class
|
|
|
322 |
def click_board(state, selected_pos, evt: gr.SelectData):
|
323 |
if state.turn != 'WHITE':
|
324 |
return state, "It's the AI's turn", generate_board_image(state), selected_pos
|
325 |
+
y = evt.index[0] // CELL_SIZE # Image coordinates (x, y) map to board (row, col)
|
326 |
+
x = evt.index[1] // CELL_SIZE
|
327 |
+
pos = (y, x)
|
328 |
if selected_pos is None:
|
329 |
if state.board[pos] in [WHITE_SOLDIER, KING]:
|
330 |
return state, f"Selected {pos_to_coord(pos)}", generate_board_image(state, pos), pos
|
|
|
347 |
state = TablutState()
|
348 |
return state, "New game started. Your turn (White).", generate_board_image(state), None
|
349 |
|
350 |
+
# Define Gradio interface
|
351 |
+
demo = gr.Blocks(title="Tablut Game")
|
352 |
+
with demo:
|
353 |
state = gr.State()
|
354 |
selected_pos = gr.State(value=None)
|
355 |
board_image = gr.Image(label="Board", type="pil")
|
|
|
357 |
new_game_button = gr.Button("New Game")
|
358 |
board_image.select(fn=click_board, inputs=[state, selected_pos], outputs=[state, message_label, board_image, selected_pos])
|
359 |
new_game_button.click(fn=new_game, outputs=[state, message_label, board_image, selected_pos])
|
360 |
+
demo.load(fn=new_game, outputs=[state, message_label, board_image, selected_pos])
|
|
|
|