pratikshahp commited on
Commit
a3e00d4
·
verified ·
1 Parent(s): cd2b97b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -127
app.py CHANGED
@@ -1,164 +1,146 @@
1
  import gradio as gr
2
  import random
 
3
 
4
- # Initialize the game board and state
5
- def initialize_game():
6
- board = [["" for _ in range(3)] for _ in range(3)]
7
- current_player = "X"
8
- status = "Player 1's turn (X)"
9
- buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
10
- return board, current_player, status, *buttons
11
 
12
- # Check for a winner
13
  def check_winner(board):
14
- for i in range(3):
15
- if board[i][0] == board[i][1] == board[i][2] and board[i][0] != "":
16
- return board[i][0]
17
- if board[0][i] == board[1][i] == board[2][i] and board[0][i] != "":
18
- return board[0][i]
 
19
  if board[0][0] == board[1][1] == board[2][2] and board[0][0] != "":
20
  return board[0][0]
21
  if board[0][2] == board[1][1] == board[2][0] and board[0][2] != "":
22
  return board[0][2]
 
 
23
  return None
24
 
25
- # Check for a draw
26
- def check_draw(board):
27
- return all(cell != "" for row in board for cell in row)
28
-
29
- # Minimax algorithm for AI's move
30
- def minimax(board, depth, is_maximizing, difficulty):
31
  winner = check_winner(board)
32
  if winner == "X":
33
  return -10 + depth
34
  elif winner == "O":
35
  return 10 - depth
36
- elif check_draw(board):
37
  return 0
38
 
39
  if is_maximizing:
40
- best = -float('inf')
41
- for i in range(3):
42
- for j in range(3):
43
- if board[i][j] == "":
44
- board[i][j] = "O"
45
- best = max(best, minimax(board, depth + 1, False, difficulty))
46
- board[i][j] = ""
47
- return best
 
48
  else:
49
- best = float('inf')
50
- for i in range(3):
51
- for j in range(3):
52
- if board[i][j] == "":
53
- board[i][j] = "X"
54
- best = min(best, minimax(board, depth + 1, True, difficulty))
55
- board[i][j] = ""
56
- return best
57
-
58
- # Find the best move for AI
59
- def get_best_move(board, difficulty):
60
- if difficulty == "easy":
61
- # Random move for easy level
62
- empty_cells = [(i, j) for i in range(3) for j in range(3) if board[i][j] == ""]
63
- if empty_cells:
 
 
 
 
64
  return random.choice(empty_cells)
65
 
66
- if difficulty == "intermediate":
67
- # Mixed strategy for intermediate
68
- if random.random() < 0.5:
69
- return get_best_move(board, "easy")
70
-
71
- best_val = -float('inf')
72
- best_move = (-1, -1)
73
- for i in range(3):
74
- for j in range(3):
75
- if board[i][j] == "":
76
- board[i][j] = "O"
77
- move_val = minimax(board, 0, False, difficulty)
78
- board[i][j] = ""
79
- if move_val > best_val:
80
- best_move = (i, j)
81
- best_val = move_val
82
  return best_move
83
 
84
  # Handle a move
85
- def handle_move(board, current_player, button_idx, game_status, difficulty):
86
- if "wins" in game_status or "draw" in game_status:
87
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
88
- return board, current_player, game_status, *buttons
89
-
90
- row, col = divmod(button_idx, 3)
91
- if board[row][col] != "":
92
- status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
93
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
94
- return board, current_player, status, *buttons
95
 
96
  board[row][col] = current_player
97
  winner = check_winner(board)
 
98
  if winner:
99
- status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! 🎉"
100
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
101
- return board, current_player, status, *buttons
102
-
103
- if check_draw(board):
104
- status = "It's a draw! 🤝"
105
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
106
- return board, current_player, status, *buttons
107
-
108
- # AI's turn
109
- if current_player == "X":
110
- current_player = "O"
111
- ai_row, ai_col = get_best_move(board, difficulty)
112
  board[ai_row][ai_col] = "O"
113
  winner = check_winner(board)
114
  if winner:
115
- status = f"AI ({winner}) wins! 🎉"
116
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
117
- return board, current_player, status, *buttons
118
 
119
- if check_draw(board):
120
- status = "It's a draw! 🤝"
121
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
122
- return board, current_player, status, *buttons
123
 
124
- current_player = "X"
125
- status = f"Player 1's turn (X)"
126
-
127
- buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
128
- return board, current_player, status, *buttons
129
-
130
- # Build the Gradio UI
131
- with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe:
132
- gr.Markdown("## Tic-Tac-Toe with AI 🎮")
133
- difficulty = gr.Radio(["easy", "intermediate", "impossible"], value="impossible", label="Difficulty Level")
134
-
135
- # Initialize states
136
- board_state = gr.State([["" for _ in range(3)] for _ in range(3)])
137
- current_player = gr.State("X")
138
- game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False)
139
-
140
- # Create grid buttons
141
- buttons = []
142
- for i in range(3):
143
- with gr.Row():
144
- for j in range(3):
145
- btn = gr.Button(value="", elem_classes=["cell-btn"])
146
- buttons.append(btn)
147
-
148
- # Update buttons dynamically on click
149
- for idx, btn in enumerate(buttons):
150
- btn.click(
151
- handle_move,
152
- inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status, difficulty],
153
- outputs=[board_state, current_player, game_status, *buttons],
 
 
 
 
 
 
 
154
  )
155
 
156
- # Reset game button
157
- reset_button = gr.Button("Reset Game")
158
- reset_button.click(
159
- initialize_game,
160
- inputs=[],
161
- outputs=[board_state, current_player, game_status, *buttons],
162
- )
163
 
164
- tic_tac_toe.launch()
 
 
1
  import gradio as gr
2
  import random
3
+ import copy
4
 
5
+ # Initialize the board
6
+ def initialize_board():
7
+ return [["" for _ in range(3)] for _ in range(3)]
 
 
 
 
8
 
9
+ # Check for winner
10
  def check_winner(board):
11
+ for row in board:
12
+ if row[0] == row[1] == row[2] and row[0] != "":
13
+ return row[0]
14
+ for col in range(3):
15
+ if board[0][col] == board[1][col] == board[2][col] and board[0][col] != "":
16
+ return board[0][col]
17
  if board[0][0] == board[1][1] == board[2][2] and board[0][0] != "":
18
  return board[0][0]
19
  if board[0][2] == board[1][1] == board[2][0] and board[0][2] != "":
20
  return board[0][2]
21
+ if all(board[row][col] != "" for row in range(3) for col in range(3)):
22
+ return "Draw"
23
  return None
24
 
25
+ # Minimax algorithm
26
+ def minimax(board, depth, is_maximizing):
 
 
 
 
27
  winner = check_winner(board)
28
  if winner == "X":
29
  return -10 + depth
30
  elif winner == "O":
31
  return 10 - depth
32
+ elif winner == "Draw":
33
  return 0
34
 
35
  if is_maximizing:
36
+ best_score = -float("inf")
37
+ for row in range(3):
38
+ for col in range(3):
39
+ if board[row][col] == "":
40
+ board[row][col] = "O"
41
+ score = minimax(board, depth + 1, False)
42
+ board[row][col] = ""
43
+ best_score = max(score, best_score)
44
+ return best_score
45
  else:
46
+ best_score = float("inf")
47
+ for row in range(3):
48
+ for col in range(3):
49
+ if board[row][col] == "":
50
+ board[row][col] = "X"
51
+ score = minimax(board, depth + 1, True)
52
+ board[row][col] = ""
53
+ best_score = min(score, best_score)
54
+ return best_score
55
+
56
+ # AI move using Minimax
57
+ def ai_move(board, difficulty):
58
+ if difficulty == "Easy":
59
+ empty_cells = [(row, col) for row in range(3) for col in range(3) if board[row][col] == ""]
60
+ return random.choice(empty_cells)
61
+
62
+ if difficulty == "Intermediate":
63
+ if random.random() < 0.5:
64
+ empty_cells = [(row, col) for row in range(3) for col in range(3) if board[row][col] == ""]
65
  return random.choice(empty_cells)
66
 
67
+ best_score = -float("inf")
68
+ best_move = None
69
+ for row in range(3):
70
+ for col in range(3):
71
+ if board[row][col] == "":
72
+ board[row][col] = "O"
73
+ score = minimax(board, 0, False)
74
+ board[row][col] = ""
75
+ if score > best_score:
76
+ best_score = score
77
+ best_move = (row, col)
 
 
 
 
 
78
  return best_move
79
 
80
  # Handle a move
81
+ def handle_move(state, row, col, difficulty):
82
+ board, current_player, status = state
83
+ if board[row][col] != "" or status != "Game On":
84
+ return state
 
 
 
 
 
 
85
 
86
  board[row][col] = current_player
87
  winner = check_winner(board)
88
+
89
  if winner:
90
+ return board, current_player, f"{winner} wins!" if winner != "Draw" else "It's a Draw!"
91
+
92
+ current_player = "O" if current_player == "X" else "X"
93
+
94
+ if current_player == "O":
95
+ ai_row, ai_col = ai_move(board, difficulty)
 
 
 
 
 
 
 
96
  board[ai_row][ai_col] = "O"
97
  winner = check_winner(board)
98
  if winner:
99
+ return board, current_player, f"{winner} wins!" if winner != "Draw" else "It's a Draw!"
100
+ current_player = "X"
 
101
 
102
+ return board, current_player, "Game On"
 
 
 
103
 
104
+ # Gradio Interface
105
+ def reset_game():
106
+ return initialize_board(), "X", "Game On"
107
+
108
+ def display_board(board):
109
+ return [[gr.Button.update(value=board[row][col], interactive=(board[row][col] == "")) for col in range(3)] for row in range(3)]
110
+
111
+ def on_click(state, row, col, difficulty):
112
+ new_state = handle_move(state, row, col, difficulty)
113
+ board, _, status = new_state
114
+ return new_state, display_board(board), status
115
+
116
+ def main():
117
+ with gr.Blocks() as app:
118
+ gr.Markdown("# Tic Tac Toe with Smart AI")
119
+
120
+ difficulty = gr.Radio(["Easy", "Intermediate", "Impossible"], value="Impossible", label="Select Difficulty")
121
+
122
+ board = gr.State(initialize_board())
123
+ current_player = gr.State("X")
124
+ status = gr.State("Game On")
125
+
126
+ output_board = gr.Grid([[gr.Button("", elem_id=f"cell-{row}-{col}", interactive=True) for col in range(3)] for row in range(3)])
127
+ status_display = gr.Textbox("Game On", interactive=False, label="Status")
128
+
129
+ reset_button = gr.Button("Reset Game")
130
+
131
+ for row in range(3):
132
+ for col in range(3):
133
+ output_board[row][col].click(
134
+ on_click,
135
+ inputs=[board, current_player, status, gr.State(row), gr.State(col), difficulty],
136
+ outputs=[board, output_board, status_display]
137
+ )
138
+
139
+ reset_button.click(
140
+ reset_game, inputs=[], outputs=[board, current_player, status, output_board, status_display]
141
  )
142
 
143
+ return app
 
 
 
 
 
 
144
 
145
+ app = main()
146
+ app.launch()