Spaces:
Sleeping
Sleeping
pratikshahp
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
# Initialize the game board and state
|
4 |
+
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 |
+
buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
|
9 |
+
return board, current_player, status, *buttons
|
10 |
+
|
11 |
+
# Check for a winner
|
12 |
+
def check_winner(board):
|
13 |
+
for i in range(3):
|
14 |
+
if board[i][0] == board[i][1] == board[i][2] and board[i][0] != "":
|
15 |
+
return board[i][0]
|
16 |
+
if board[0][i] == board[1][i] == board[2][i] and board[0][i] != "":
|
17 |
+
return board[0][i]
|
18 |
+
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != "":
|
19 |
+
return board[0][0]
|
20 |
+
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != "":
|
21 |
+
return board[0][2]
|
22 |
+
return None
|
23 |
+
|
24 |
+
# Check for a draw
|
25 |
+
def check_draw(board):
|
26 |
+
return all(cell != "" for row in board for cell in row)
|
27 |
+
|
28 |
+
# Minimax algorithm for AI's move
|
29 |
+
def minimax(board, depth, is_maximizing):
|
30 |
+
winner = check_winner(board)
|
31 |
+
if winner == "X":
|
32 |
+
return -10 + depth
|
33 |
+
elif winner == "O":
|
34 |
+
return 10 - depth
|
35 |
+
elif check_draw(board):
|
36 |
+
return 0
|
37 |
+
|
38 |
+
if is_maximizing:
|
39 |
+
best = -float('inf')
|
40 |
+
for i in range(3):
|
41 |
+
for j in range(3):
|
42 |
+
if board[i][j] == "":
|
43 |
+
board[i][j] = "O"
|
44 |
+
best = max(best, minimax(board, depth + 1, False))
|
45 |
+
board[i][j] = ""
|
46 |
+
return best
|
47 |
+
else:
|
48 |
+
best = float('inf')
|
49 |
+
for i in range(3):
|
50 |
+
for j in range(3):
|
51 |
+
if board[i][j] == "":
|
52 |
+
board[i][j] = "X"
|
53 |
+
best = min(best, minimax(board, depth + 1, True))
|
54 |
+
board[i][j] = ""
|
55 |
+
return best
|
56 |
+
|
57 |
+
# Find the best move for AI
|
58 |
+
def get_best_move(board):
|
59 |
+
best_val = -float('inf')
|
60 |
+
best_move = (-1, -1)
|
61 |
+
for i in range(3):
|
62 |
+
for j in range(3):
|
63 |
+
if board[i][j] == "":
|
64 |
+
board[i][j] = "O"
|
65 |
+
move_val = minimax(board, 0, False)
|
66 |
+
board[i][j] = ""
|
67 |
+
if move_val > best_val:
|
68 |
+
best_move = (i, j)
|
69 |
+
best_val = move_val
|
70 |
+
return best_move
|
71 |
+
|
72 |
+
# Handle a move
|
73 |
+
def handle_move(board, current_player, button_idx, game_status):
|
74 |
+
if "wins" in game_status or "draw" in game_status:
|
75 |
+
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
76 |
+
return board, current_player, game_status, *buttons
|
77 |
+
|
78 |
+
row, col = divmod(button_idx, 3)
|
79 |
+
if board[row][col] != "":
|
80 |
+
status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
|
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, *buttons
|
83 |
+
|
84 |
+
board[row][col] = current_player
|
85 |
+
winner = check_winner(board)
|
86 |
+
if winner:
|
87 |
+
status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! ๐"
|
88 |
+
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
89 |
+
return board, current_player, status, *buttons
|
90 |
+
|
91 |
+
if check_draw(board):
|
92 |
+
status = "It's a draw! ๐ค"
|
93 |
+
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
94 |
+
return board, current_player, status, *buttons
|
95 |
+
|
96 |
+
# AI's turn
|
97 |
+
if current_player == "X":
|
98 |
+
current_player = "O"
|
99 |
+
ai_row, ai_col = get_best_move(board)
|
100 |
+
board[ai_row][ai_col] = "O"
|
101 |
+
winner = check_winner(board)
|
102 |
+
if winner:
|
103 |
+
status = f"AI ({winner}) wins! ๐"
|
104 |
+
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
105 |
+
return board, current_player, status, *buttons
|
106 |
+
|
107 |
+
if check_draw(board):
|
108 |
+
status = "It's a draw! ๐ค"
|
109 |
+
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
|
110 |
+
return board, current_player, status, *buttons
|
111 |
+
|
112 |
+
current_player = "X"
|
113 |
+
status = f"Player 1's turn (X)"
|
114 |
+
|
115 |
+
buttons = [gr.Button(value=board[i//3][i%3], elem_classes=["cell-btn"]) for i in range(9)]
|
116 |
+
return board, current_player, status, *buttons
|
117 |
+
|
118 |
+
# Build the Gradio UI
|
119 |
+
with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe:
|
120 |
+
gr.Markdown("## Tic-Tac-Toe with AI ๐ฎ")
|
121 |
+
|
122 |
+
# Initialize states
|
123 |
+
board_state = gr.State([["" for _ in range(3)] for _ in range(3)])
|
124 |
+
current_player = gr.State("X")
|
125 |
+
game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False)
|
126 |
+
|
127 |
+
# Create grid buttons
|
128 |
+
buttons = []
|
129 |
+
for i in range(3):
|
130 |
+
with gr.Row():
|
131 |
+
for j in range(3):
|
132 |
+
btn = gr.Button(value="", elem_classes=["cell-btn"])
|
133 |
+
buttons.append(btn)
|
134 |
+
|
135 |
+
# Update buttons dynamically on click
|
136 |
+
for idx, btn in enumerate(buttons):
|
137 |
+
btn.click(
|
138 |
+
handle_move,
|
139 |
+
inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status],
|
140 |
+
outputs=[board_state, current_player, game_status, *buttons],
|
141 |
+
)
|
142 |
+
|
143 |
+
# Reset game button
|
144 |
+
reset_button = gr.Button("Reset Game")
|
145 |
+
reset_button.click(
|
146 |
+
initialize_game,
|
147 |
+
inputs=[],
|
148 |
+
outputs=[board_state, current_player, game_status, *buttons],
|
149 |
+
)
|
150 |
+
|
151 |
+
tic_tac_toe.launch()
|