Spaces:
Sleeping
Sleeping
pratikshahp
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,164 +1,146 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
|
|
3 |
|
4 |
-
# Initialize the
|
5 |
-
def
|
6 |
-
|
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
|
13 |
def check_winner(board):
|
14 |
-
for
|
15 |
-
if
|
16 |
-
return
|
17 |
-
|
18 |
-
|
|
|
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 |
-
#
|
26 |
-
def
|
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
|
37 |
return 0
|
38 |
|
39 |
if is_maximizing:
|
40 |
-
|
41 |
-
for
|
42 |
-
for
|
43 |
-
if board[
|
44 |
-
board[
|
45 |
-
|
46 |
-
board[
|
47 |
-
|
|
|
48 |
else:
|
49 |
-
|
50 |
-
for
|
51 |
-
for
|
52 |
-
if board[
|
53 |
-
board[
|
54 |
-
|
55 |
-
board[
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
empty_cells = [(
|
63 |
-
|
|
|
|
|
|
|
|
|
64 |
return random.choice(empty_cells)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
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(
|
86 |
-
|
87 |
-
|
88 |
-
return
|
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 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
if
|
104 |
-
|
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 |
-
|
116 |
-
|
117 |
-
return board, current_player, status, *buttons
|
118 |
|
119 |
-
|
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 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
)
|
155 |
|
156 |
-
|
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 |
-
|
|
|
|
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()
|