Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,169 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
# Tic Tac Toe logic with Minimax AI
|
| 4 |
class TicTacToe:
|
| 5 |
def __init__(self):
|
| 6 |
-
self.board =
|
| 7 |
-
self.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return {"position": None, "score": 1 * (state.count(" ") + 1) if other_player == max_player else -1 * (
|
| 57 |
-
state.count(" ") + 1)}
|
| 58 |
-
elif not self.empty_squares():
|
| 59 |
-
return {"position": None, "score": 0}
|
| 60 |
-
|
| 61 |
-
if player == max_player:
|
| 62 |
-
best = {"position": None, "score": -float("inf")}
|
| 63 |
else:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
game = TicTacToe()
|
| 93 |
|
| 94 |
-
def
|
| 95 |
-
if
|
| 96 |
-
return
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from typing import List, Tuple, Optional
|
| 4 |
|
|
|
|
| 5 |
class TicTacToe:
|
| 6 |
def __init__(self):
|
| 7 |
+
self.board = np.zeros((3, 3))
|
| 8 |
+
self.human_player = 1
|
| 9 |
+
self.ai_player = -1
|
| 10 |
+
|
| 11 |
+
def reset_board(self):
|
| 12 |
+
self.board = np.zeros((3, 3))
|
| 13 |
+
return self.format_board()
|
| 14 |
+
|
| 15 |
+
def check_winner(self) -> Optional[int]:
|
| 16 |
+
# Check rows, columns and diagonals
|
| 17 |
+
for player in [self.human_player, self.ai_player]:
|
| 18 |
+
# Rows and columns
|
| 19 |
+
for i in range(3):
|
| 20 |
+
if all(self.board[i, :] == player) or all(self.board[:, i] == player):
|
| 21 |
+
return player
|
| 22 |
+
# Diagonals
|
| 23 |
+
if all(np.diag(self.board) == player) or all(np.diag(np.fliplr(self.board)) == player):
|
| 24 |
+
return player
|
| 25 |
+
|
| 26 |
+
# Check for draw
|
| 27 |
+
if np.all(self.board != 0):
|
| 28 |
+
return 0
|
| 29 |
+
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
def get_valid_moves(self) -> List[Tuple[int, int]]:
|
| 33 |
+
return [(i, j) for i in range(3) for j in range(3) if self.board[i, j] == 0]
|
| 34 |
+
|
| 35 |
+
def minimax(self, depth: int, is_maximizing: bool, alpha: float = float('-inf'), beta: float = float('inf')) -> Tuple[int, Optional[Tuple[int, int]]]:
|
| 36 |
+
winner = self.check_winner()
|
| 37 |
+
if winner is not None:
|
| 38 |
+
return winner * 100, None
|
| 39 |
+
|
| 40 |
+
if is_maximizing:
|
| 41 |
+
best_score = float('-inf')
|
| 42 |
+
best_move = None
|
| 43 |
+
for move in self.get_valid_moves():
|
| 44 |
+
self.board[move] = self.ai_player
|
| 45 |
+
score, _ = self.minimax(depth + 1, False, alpha, beta)
|
| 46 |
+
self.board[move] = 0
|
| 47 |
+
|
| 48 |
+
if score > best_score:
|
| 49 |
+
best_score = score
|
| 50 |
+
best_move = move
|
| 51 |
+
|
| 52 |
+
alpha = max(alpha, best_score)
|
| 53 |
+
if beta <= alpha:
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
return best_score, best_move
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
+
best_score = float('inf')
|
| 59 |
+
best_move = None
|
| 60 |
+
for move in self.get_valid_moves():
|
| 61 |
+
self.board[move] = self.human_player
|
| 62 |
+
score, _ = self.minimax(depth + 1, True, alpha, beta)
|
| 63 |
+
self.board[move] = 0
|
| 64 |
+
|
| 65 |
+
if score < best_score:
|
| 66 |
+
best_score = score
|
| 67 |
+
best_move = move
|
| 68 |
+
|
| 69 |
+
beta = min(beta, best_score)
|
| 70 |
+
if beta <= alpha:
|
| 71 |
+
break
|
| 72 |
+
|
| 73 |
+
return best_score, best_move
|
| 74 |
+
|
| 75 |
+
def ai_move(self) -> str:
|
| 76 |
+
if len(self.get_valid_moves()) == 0:
|
| 77 |
+
return self.format_board()
|
| 78 |
+
|
| 79 |
+
_, best_move = self.minimax(0, True)
|
| 80 |
+
if best_move is not None:
|
| 81 |
+
self.board[best_move] = self.ai_player
|
| 82 |
+
|
| 83 |
+
return self.format_board()
|
| 84 |
+
|
| 85 |
+
def format_board(self) -> str:
|
| 86 |
+
symbols = {0: " ", 1: "X", -1: "O"}
|
| 87 |
+
return "\n".join([
|
| 88 |
+
"-------------",
|
| 89 |
+
f"| {symbols[self.board[0,0]]} | {symbols[self.board[0,1]]} | {symbols[self.board[0,2]]} |",
|
| 90 |
+
"-------------",
|
| 91 |
+
f"| {symbols[self.board[1,0]]} | {symbols[self.board[1,1]]} | {symbols[self.board[1,2]]} |",
|
| 92 |
+
"-------------",
|
| 93 |
+
f"| {symbols[self.board[2,0]]} | {symbols[self.board[2,1]]} | {symbols[self.board[2,2]]} |",
|
| 94 |
+
"-------------"
|
| 95 |
+
])
|
| 96 |
+
|
| 97 |
+
def make_move(self, row: int, col: int) -> Tuple[str, str]:
|
| 98 |
+
# Check if move is valid
|
| 99 |
+
if self.board[row, col] != 0:
|
| 100 |
+
return self.format_board(), "Invalid move! Cell already taken."
|
| 101 |
+
|
| 102 |
+
# Make human move
|
| 103 |
+
self.board[row, col] = self.human_player
|
| 104 |
+
|
| 105 |
+
# Check if game is over after human move
|
| 106 |
+
winner = self.check_winner()
|
| 107 |
+
if winner is not None:
|
| 108 |
+
if winner == self.human_player:
|
| 109 |
+
return self.format_board(), "You win!"
|
| 110 |
+
elif winner == 0:
|
| 111 |
+
return self.format_board(), "It's a draw!"
|
| 112 |
+
|
| 113 |
+
# Make AI move
|
| 114 |
+
self.ai_move()
|
| 115 |
+
|
| 116 |
+
# Check if game is over after AI move
|
| 117 |
+
winner = self.check_winner()
|
| 118 |
+
if winner is not None:
|
| 119 |
+
if winner == self.ai_player:
|
| 120 |
+
return self.format_board(), "AI wins!"
|
| 121 |
+
elif winner == 0:
|
| 122 |
+
return self.format_board(), "It's a draw!"
|
| 123 |
+
|
| 124 |
+
return self.format_board(), "Game in progress"
|
| 125 |
+
|
| 126 |
+
# Create game instance
|
| 127 |
game = TicTacToe()
|
| 128 |
|
| 129 |
+
def play_game(row: int, col: int, board_state: str, game_status: str) -> Tuple[str, str]:
|
| 130 |
+
if "win" in game_status or "draw" in game_status:
|
| 131 |
+
return board_state, game_status
|
| 132 |
+
return game.make_move(row, col)
|
| 133 |
+
|
| 134 |
+
def reset_game() -> Tuple[str, str]:
|
| 135 |
+
return game.reset_board(), "Game reset! Your turn (X)"
|
| 136 |
+
|
| 137 |
+
# Create Gradio interface
|
| 138 |
+
with gr.Blocks() as demo:
|
| 139 |
+
gr.Markdown("# Tic Tac Toe vs AI")
|
| 140 |
+
gr.Markdown("You play as X, AI plays as O. Click the buttons to make your move!")
|
| 141 |
+
|
| 142 |
+
with gr.Row():
|
| 143 |
+
# Game board display
|
| 144 |
+
board_display = gr.Textbox(value=game.format_board(), label="Game Board", lines=7)
|
| 145 |
+
game_status = gr.Textbox(value="Your turn (X)", label="Game Status")
|
| 146 |
+
|
| 147 |
+
# Create 3x3 grid of buttons
|
| 148 |
+
with gr.Row():
|
| 149 |
+
for i in range(3):
|
| 150 |
+
with gr.Column():
|
| 151 |
+
for j in range(3):
|
| 152 |
+
btn = gr.Button(f"Row {i}, Col {j}")
|
| 153 |
+
btn.click(
|
| 154 |
+
fn=play_game,
|
| 155 |
+
inputs=[
|
| 156 |
+
gr.Number(value=i, visible=False),
|
| 157 |
+
gr.Number(value=j, visible=False),
|
| 158 |
+
board_display,
|
| 159 |
+
game_status
|
| 160 |
+
],
|
| 161 |
+
outputs=[board_display, game_status]
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Reset button
|
| 165 |
+
reset_btn = gr.Button("Reset Game")
|
| 166 |
+
reset_btn.click(fn=reset_game, inputs=[], outputs=[board_display, game_status])
|
| 167 |
+
|
| 168 |
+
# Launch app
|
| 169 |
+
demo.launch()
|
|
|