Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ def initialize_game():
|
|
23 |
status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
|
24 |
difficulty = "Medium"
|
25 |
buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
|
26 |
-
return board, current_player, difficulty, status, *buttons
|
27 |
|
28 |
# Check for a winner
|
29 |
def check_winner(board):
|
@@ -104,25 +104,25 @@ def get_ai_move(board, difficulty):
|
|
104 |
def handle_move(board, current_player, button_idx, difficulty, game_status):
|
105 |
if "wins" in game_status or "draw" in game_status:
|
106 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
107 |
-
return board, current_player, difficulty, game_status, *buttons
|
108 |
|
109 |
row, col = divmod(button_idx, 3)
|
110 |
if board[row][col] != "":
|
111 |
status = f"<div style='font-size: 1.5em; color: orange; font-weight: bold;'>Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})</div>"
|
112 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
|
113 |
-
return board, current_player, difficulty, status, *buttons
|
114 |
|
115 |
board[row][col] = current_player
|
116 |
winner = check_winner(board)
|
117 |
if winner:
|
118 |
status = f"<div style='font-size: 2em; color: red; font-weight: bold;'>Player {1 if winner == 'X' else 2} ({winner}) wins! π</div>"
|
119 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
120 |
-
return board, current_player, difficulty, status, *buttons
|
121 |
|
122 |
if check_draw(board):
|
123 |
status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! π€</div>"
|
124 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
125 |
-
return board, current_player, difficulty, status, *buttons
|
126 |
|
127 |
if current_player == "X":
|
128 |
current_player = "O"
|
@@ -132,18 +132,18 @@ def handle_move(board, current_player, button_idx, difficulty, game_status):
|
|
132 |
if winner:
|
133 |
status = f"<div style='font-size: 2em; color: purple; font-weight: bold;'>AI ({winner}) wins! π</div>"
|
134 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
135 |
-
return board, current_player, difficulty, status, *buttons
|
136 |
|
137 |
if check_draw(board):
|
138 |
status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! π€</div>"
|
139 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
140 |
-
return board, current_player, difficulty, status, *buttons
|
141 |
|
142 |
current_player = "X"
|
143 |
status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
|
144 |
|
145 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
|
146 |
-
return board, current_player, difficulty, status, *buttons
|
147 |
|
148 |
# Generate a hint using LLM
|
149 |
def get_hint_from_llm(board):
|
@@ -151,7 +151,6 @@ def get_hint_from_llm(board):
|
|
151 |
hint = llm(prompt)
|
152 |
return hint
|
153 |
|
154 |
-
|
155 |
# Build the Gradio UI
|
156 |
css = """
|
157 |
.cell-btn {
|
@@ -194,25 +193,24 @@ with gr.Blocks(css=css) as tic_tac_toe:
|
|
194 |
btn = gr.Button(value="", elem_classes=["cell-btn"])
|
195 |
buttons.append(btn)
|
196 |
|
197 |
-
|
198 |
hint_button = gr.Button("Get Hint")
|
199 |
hint_display = gr.Textbox(value="", label="Hint", interactive=False)
|
200 |
hint_button.click(get_hint_from_llm, inputs=[board_state], outputs=[hint_display])
|
201 |
|
202 |
-
|
203 |
-
|
204 |
for idx, btn in enumerate(buttons):
|
205 |
btn.click(
|
206 |
handle_move,
|
207 |
inputs=[board_state, current_player, gr.Number(idx, visible=False), difficulty, game_status],
|
208 |
-
outputs=[board_state, current_player, difficulty, game_status, *buttons],
|
209 |
)
|
210 |
|
211 |
reset_button = gr.Button("Reset Game")
|
212 |
reset_button.click(
|
213 |
initialize_game,
|
214 |
inputs=[],
|
215 |
-
outputs=[board_state, current_player, difficulty, game_status, *buttons],
|
216 |
)
|
217 |
|
218 |
-
tic_tac_toe.launch()
|
|
|
23 |
status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
|
24 |
difficulty = "Medium"
|
25 |
buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
|
26 |
+
return board, current_player, difficulty, status, *buttons, ""
|
27 |
|
28 |
# Check for a winner
|
29 |
def check_winner(board):
|
|
|
104 |
def handle_move(board, current_player, button_idx, difficulty, game_status):
|
105 |
if "wins" in game_status or "draw" in game_status:
|
106 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
107 |
+
return board, current_player, difficulty, game_status, *buttons, ""
|
108 |
|
109 |
row, col = divmod(button_idx, 3)
|
110 |
if board[row][col] != "":
|
111 |
status = f"<div style='font-size: 1.5em; color: orange; font-weight: bold;'>Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})</div>"
|
112 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
|
113 |
+
return board, current_player, difficulty, status, *buttons, ""
|
114 |
|
115 |
board[row][col] = current_player
|
116 |
winner = check_winner(board)
|
117 |
if winner:
|
118 |
status = f"<div style='font-size: 2em; color: red; font-weight: bold;'>Player {1 if winner == 'X' else 2} ({winner}) wins! π</div>"
|
119 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
120 |
+
return board, current_player, difficulty, status, *buttons, ""
|
121 |
|
122 |
if check_draw(board):
|
123 |
status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! π€</div>"
|
124 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
125 |
+
return board, current_player, difficulty, status, *buttons, ""
|
126 |
|
127 |
if current_player == "X":
|
128 |
current_player = "O"
|
|
|
132 |
if winner:
|
133 |
status = f"<div style='font-size: 2em; color: purple; font-weight: bold;'>AI ({winner}) wins! π</div>"
|
134 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
135 |
+
return board, current_player, difficulty, status, *buttons, ""
|
136 |
|
137 |
if check_draw(board):
|
138 |
status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! π€</div>"
|
139 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
140 |
+
return board, current_player, difficulty, status, *buttons, ""
|
141 |
|
142 |
current_player = "X"
|
143 |
status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
|
144 |
|
145 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
|
146 |
+
return board, current_player, difficulty, status, *buttons, ""
|
147 |
|
148 |
# Generate a hint using LLM
|
149 |
def get_hint_from_llm(board):
|
|
|
151 |
hint = llm(prompt)
|
152 |
return hint
|
153 |
|
|
|
154 |
# Build the Gradio UI
|
155 |
css = """
|
156 |
.cell-btn {
|
|
|
193 |
btn = gr.Button(value="", elem_classes=["cell-btn"])
|
194 |
buttons.append(btn)
|
195 |
|
196 |
+
# Hint button
|
197 |
hint_button = gr.Button("Get Hint")
|
198 |
hint_display = gr.Textbox(value="", label="Hint", interactive=False)
|
199 |
hint_button.click(get_hint_from_llm, inputs=[board_state], outputs=[hint_display])
|
200 |
|
201 |
+
# Handle button clicks
|
|
|
202 |
for idx, btn in enumerate(buttons):
|
203 |
btn.click(
|
204 |
handle_move,
|
205 |
inputs=[board_state, current_player, gr.Number(idx, visible=False), difficulty, game_status],
|
206 |
+
outputs=[board_state, current_player, difficulty, game_status, *buttons, hint_display],
|
207 |
)
|
208 |
|
209 |
reset_button = gr.Button("Reset Game")
|
210 |
reset_button.click(
|
211 |
initialize_game,
|
212 |
inputs=[],
|
213 |
+
outputs=[board_state, current_player, difficulty, game_status, *buttons, hint_display],
|
214 |
)
|
215 |
|
216 |
+
tic_tac_toe.launch()
|