Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,109 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def check_winner(board):
|
6 |
-
for
|
7 |
-
if
|
8 |
-
return
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
if board[0][0] == board[1][1] == board[2][2] != "":
|
13 |
return board[0][0]
|
14 |
-
if board[0][2] == board[1][1] == board[2][0] != "":
|
15 |
return board[0][2]
|
16 |
return None
|
17 |
|
18 |
-
|
19 |
-
# Check if it's a draw
|
20 |
def check_draw(board):
|
21 |
-
for row in board
|
22 |
-
if "" in row:
|
23 |
-
return False
|
24 |
-
return True
|
25 |
-
|
26 |
|
27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def get_best_move(board, difficulty):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def handle_move(board, current_player, button_idx, game_status, difficulty):
|
35 |
if "wins" in game_status or "draw" in game_status:
|
36 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
37 |
-
return board, current_player, game_status,
|
38 |
|
39 |
row, col = divmod(button_idx, 3)
|
40 |
if board[row][col] != "":
|
41 |
status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
|
42 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
|
43 |
-
return board, current_player, status,
|
44 |
|
45 |
board[row][col] = current_player
|
46 |
winner = check_winner(board)
|
47 |
if winner:
|
48 |
status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! ๐"
|
49 |
-
winner_message = f"<h1 style='font-size: 2.5em; color: green; text-align: center;'>Player {1 if winner == 'X' else 2} ({winner}) Wins! ๐</h1>"
|
50 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
51 |
-
return board, current_player, status,
|
52 |
|
53 |
if check_draw(board):
|
54 |
status = "It's a draw! ๐ค"
|
55 |
-
winner_message = "<h1 style='font-size: 2.5em; color: blue; text-align: center;'>It's a Draw! ๐ค</h1>"
|
56 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
57 |
-
return board, current_player, status,
|
58 |
|
59 |
# AI's turn
|
60 |
if current_player == "X":
|
@@ -64,59 +113,52 @@ def handle_move(board, current_player, button_idx, game_status, difficulty):
|
|
64 |
winner = check_winner(board)
|
65 |
if winner:
|
66 |
status = f"AI ({winner}) wins! ๐"
|
67 |
-
winner_message = f"<h1 style='font-size: 2.5em; color: red; text-align: center;'>AI ({winner}) Wins! ๐</h1>"
|
68 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
69 |
-
return board, current_player, status,
|
70 |
|
71 |
if check_draw(board):
|
72 |
status = "It's a draw! ๐ค"
|
73 |
-
winner_message = "<h1 style='font-size: 2.5em; color: blue; text-align: center;'>It's a Draw! ๐ค</h1>"
|
74 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
75 |
-
return board, current_player, status,
|
76 |
|
77 |
current_player = "X"
|
78 |
status = f"Player 1's turn (X)"
|
79 |
|
80 |
-
winner_message = "" # No winner yet
|
81 |
buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
|
82 |
-
return board, current_player, status,
|
83 |
-
|
84 |
|
85 |
-
#
|
86 |
-
|
87 |
-
|
88 |
-
current_player = "X"
|
89 |
-
status = "Player 1's turn (X)"
|
90 |
-
winner_message = ""
|
91 |
-
buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
|
92 |
-
return board, current_player, status, winner_message, *buttons
|
93 |
-
|
94 |
-
|
95 |
-
# Initial game setup
|
96 |
-
board = [[""] * 3 for _ in range(3)]
|
97 |
-
buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
|
98 |
-
|
99 |
-
with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; margin: 5px;}") as app:
|
100 |
-
gr.Markdown("<h1 style='text-align: center;'>Tic Tac Toe Game</h1>")
|
101 |
difficulty = gr.Radio(["easy", "intermediate", "impossible"], value="impossible", label="Difficulty Level")
|
102 |
-
game_status = gr.Textbox("Player 1's turn (X)", interactive=False, label="Game Status")
|
103 |
-
winner_display = gr.HTML()
|
104 |
-
with gr.Row():
|
105 |
-
button_components = []
|
106 |
-
for i in range(3):
|
107 |
-
with gr.Row():
|
108 |
-
for j in range(3):
|
109 |
-
button_components.append(gr.Button("", elem_classes=["cell-btn"], interactive=True))
|
110 |
-
current_player = gr.State("X")
|
111 |
-
board_state = gr.State([[""] * 3 for _ in range(3)])
|
112 |
-
reset_button = gr.Button("Reset Game")
|
113 |
-
reset_button.click(reset_game, [], outputs=[board_state, current_player, game_status, winner_display, *button_components])
|
114 |
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
handle_move,
|
118 |
-
inputs=[board_state, current_player, gr.
|
119 |
-
outputs=[board_state, current_player, game_status,
|
120 |
)
|
121 |
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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":
|
|
|
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()
|