pratikshahp commited on
Commit
87b6a3d
Β·
verified Β·
1 Parent(s): 2cbfc08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -5,8 +5,9 @@ def initialize_game():
5
  board = [["" for _ in range(3)] for _ in range(3)]
6
  current_player = "X"
7
  status = "Player 1's turn (X)"
8
- flattened_board = [cell for row in board for cell in row]
9
- return board, current_player, status, flattened_board
 
10
 
11
  # Check for a winner
12
  def check_winner(board):
@@ -30,21 +31,27 @@ def handle_move(board, current_player, button_idx):
30
  row, col = divmod(button_idx, 3)
31
  if board[row][col] != "":
32
  status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
33
- return board, current_player, status, [cell for row in board for cell in row]
 
34
 
35
  board[row][col] = current_player
36
  winner = check_winner(board)
37
  if winner:
38
  status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! πŸŽ‰"
39
- return board, current_player, status, [cell for row in board for cell in row]
 
40
 
41
  if check_draw(board):
42
  status = "It's a draw! 🀝"
43
- return board, current_player, status, [cell for row in board for cell in row]
 
44
 
45
  current_player = "O" if current_player == "X" else "X"
46
  status = f"Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
47
- return board, current_player, status, [cell for row in board for cell in row]
 
 
 
48
 
49
  # Build the Gradio UI
50
  with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe:
@@ -55,21 +62,20 @@ with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text
55
  current_player = gr.State("X")
56
  game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False)
57
 
58
- # Create grid buttons without displaying any Number components
59
  buttons = []
60
  for i in range(3):
61
  with gr.Row():
62
  for j in range(3):
63
- # Each button is clickable, the index is passed in hidden and used internally
64
- btn = gr.Button(value="", elem_classes=["cell-btn"], elem_id=f"button_{i*3 + j}")
65
  buttons.append(btn)
66
 
67
  # Update buttons dynamically on click
68
  for idx, btn in enumerate(buttons):
69
  btn.click(
70
  handle_move,
71
- inputs=[board_state, current_player, gr.Number(idx, visible=False)], # Hide the number input
72
- outputs=[board_state, current_player, game_status, *buttons],
73
  )
74
 
75
  # Reset game button
@@ -77,7 +83,7 @@ with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text
77
  reset_button.click(
78
  initialize_game,
79
  inputs=[],
80
- outputs=[board_state, current_player, game_status, *buttons],
81
  )
82
 
83
  tic_tac_toe.launch()
 
5
  board = [["" for _ in range(3)] for _ in range(3)]
6
  current_player = "X"
7
  status = "Player 1's turn (X)"
8
+ # Create 9 buttons for the grid
9
+ buttons = [gr.Button(value="", elem_classes=["cell-btn"]) for _ in range(9)]
10
+ return board, current_player, status, *buttons
11
 
12
  # Check for a winner
13
  def check_winner(board):
 
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})"
34
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
35
+ return board, current_player, status, *buttons
36
 
37
  board[row][col] = current_player
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"
50
  status = f"Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
51
+
52
+ # Update the buttons to reflect the new board state
53
+ buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
54
+ return board, current_player, status, *buttons
55
 
56
  # Build the Gradio UI
57
  with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe:
 
62
  current_player = gr.State("X")
63
  game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False)
64
 
65
+ # Create grid buttons
66
  buttons = []
67
  for i in range(3):
68
  with gr.Row():
69
  for j in range(3):
70
+ btn = gr.Button(value="", elem_classes=["cell-btn"])
 
71
  buttons.append(btn)
72
 
73
  # Update buttons dynamically on click
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
 
81
  # Reset game button
 
83
  reset_button.click(
84
  initialize_game,
85
  inputs=[],
86
+ outputs=[board_state, current_player, game_status, *buttons], # Include buttons in reset
87
  )
88
 
89
  tic_tac_toe.launch()