pratikshahp commited on
Commit
71ea2dc
Β·
verified Β·
1 Parent(s): 87b6a3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -27,7 +27,13 @@ def check_draw(board):
27
  return all(cell != "" for row in board for cell in row)
28
 
29
  # Handle a move
30
- def handle_move(board, current_player, button_idx):
 
 
 
 
 
 
31
  row, col = divmod(button_idx, 3)
32
  if board[row][col] != "":
33
  status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
@@ -38,12 +44,12 @@ def handle_move(board, current_player, button_idx):
38
  winner = check_winner(board)
39
  if winner:
40
  status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! πŸŽ‰"
41
- buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
42
  return board, current_player, status, *buttons
43
 
44
  if check_draw(board):
45
  status = "It's a draw! 🀝"
46
- buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
47
  return board, current_player, status, *buttons
48
 
49
  current_player = "O" if current_player == "X" else "X"
@@ -74,7 +80,7 @@ with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text
74
  for idx, btn in enumerate(buttons):
75
  btn.click(
76
  handle_move,
77
- inputs=[board_state, current_player, gr.Number(idx, visible=False)],
78
  outputs=[board_state, current_player, game_status, *buttons], # Return all outputs
79
  )
80
 
 
27
  return all(cell != "" for row in board for cell in row)
28
 
29
  # Handle a move
30
+ def handle_move(board, current_player, button_idx, game_status):
31
+ # If the game is already over (winner or draw), don't allow further moves
32
+ if "wins" in game_status or "draw" in game_status:
33
+ status = game_status # No change to status if the game is over
34
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
35
+ return board, current_player, status, *buttons
36
+
37
  row, col = divmod(button_idx, 3)
38
  if board[row][col] != "":
39
  status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
 
44
  winner = check_winner(board)
45
  if winner:
46
  status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! πŸŽ‰"
47
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
48
  return board, current_player, status, *buttons
49
 
50
  if check_draw(board):
51
  status = "It's a draw! 🀝"
52
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
53
  return board, current_player, status, *buttons
54
 
55
  current_player = "O" if current_player == "X" else "X"
 
80
  for idx, btn in enumerate(buttons):
81
  btn.click(
82
  handle_move,
83
+ inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status],
84
  outputs=[board_state, current_player, game_status, *buttons], # Return all outputs
85
  )
86