Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,169 +1,135 @@
|
|
| 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 =
|
| 8 |
-
self.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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
|
| 130 |
-
if
|
| 131 |
-
return
|
| 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 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 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 = [" "] * 9
|
| 7 |
+
self.current_winner = None
|
| 8 |
+
|
| 9 |
+
def print_board(self):
|
| 10 |
+
for row in [self.board[i * 3:(i + 1) * 3] for i in range(3)]:
|
| 11 |
+
print("| " + " | ".join(row) + " |")
|
| 12 |
+
|
| 13 |
+
def available_moves(self):
|
| 14 |
+
return [i for i, spot in enumerate(self.board) if spot == " "]
|
| 15 |
+
|
| 16 |
+
def empty_squares(self):
|
| 17 |
+
return " " in self.board
|
| 18 |
+
|
| 19 |
+
def num_empty_squares(self):
|
| 20 |
+
return len(self.available_moves())
|
| 21 |
+
|
| 22 |
+
def make_move(self, square, letter):
|
| 23 |
+
if self.board[square] == " ":
|
| 24 |
+
self.board[square] = letter
|
| 25 |
+
if self.winner(square, letter):
|
| 26 |
+
self.current_winner = letter
|
| 27 |
+
return True
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
def winner(self, square, letter):
|
| 31 |
+
# Check row
|
| 32 |
+
row_ind = square // 3
|
| 33 |
+
row = self.board[row_ind * 3:(row_ind + 1) * 3]
|
| 34 |
+
if all([spot == letter for spot in row]):
|
| 35 |
+
return True
|
| 36 |
+
# Check column
|
| 37 |
+
col_ind = square % 3
|
| 38 |
+
column = [self.board[col_ind + i * 3] for i in range(3)]
|
| 39 |
+
if all([spot == letter for spot in column]):
|
| 40 |
+
return True
|
| 41 |
+
# Check diagonals
|
| 42 |
+
if square % 2 == 0:
|
| 43 |
+
diagonal1 = [self.board[i] for i in [0, 4, 8]]
|
| 44 |
+
if all([spot == letter for spot in diagonal1]):
|
| 45 |
+
return True
|
| 46 |
+
diagonal2 = [self.board[i] for i in [2, 4, 6]]
|
| 47 |
+
if all([spot == letter for spot in diagonal2]):
|
| 48 |
+
return True
|
| 49 |
+
return False
|
| 50 |
+
|
| 51 |
+
def minimax(self, state, player):
|
| 52 |
+
max_player = "O"
|
| 53 |
+
other_player = "X" if player == "O" else "O"
|
| 54 |
+
|
| 55 |
+
if self.current_winner == other_player:
|
| 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 |
+
best = {"position": None, "score": float("inf")}
|
| 65 |
+
|
| 66 |
+
for possible_move in self.available_moves():
|
| 67 |
+
self.board[possible_move] = player
|
| 68 |
+
sim_score = self.minimax(self.board, other_player)
|
| 69 |
+
|
| 70 |
+
self.board[possible_move] = " "
|
| 71 |
+
sim_score["position"] = possible_move
|
| 72 |
+
|
| 73 |
+
if player == max_player:
|
| 74 |
+
if sim_score["score"] > best["score"]:
|
| 75 |
+
best = sim_score
|
| 76 |
+
else:
|
| 77 |
+
if sim_score["score"] < best["score"]:
|
| 78 |
+
best = sim_score
|
| 79 |
+
|
| 80 |
+
return best
|
| 81 |
+
|
| 82 |
+
def ai_move(self):
|
| 83 |
+
if len(self.available_moves()) == 9:
|
| 84 |
+
square = 4
|
| 85 |
+
else:
|
| 86 |
+
square = self.minimax(self.board, "O")["position"]
|
| 87 |
+
self.make_move(square, "O")
|
| 88 |
+
return square
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
game = TicTacToe()
|
| 93 |
|
| 94 |
+
def play_tic_tac_toe(human_move):
|
| 95 |
+
if not game.empty_squares() or game.current_winner is not None:
|
| 96 |
+
return {"output": "Game Over! Reset to play again.", "board": game.board}
|
| 97 |
+
|
| 98 |
+
if game.board[human_move] != " ":
|
| 99 |
+
return {"output": "Invalid move. Try again.", "board": game.board}
|
| 100 |
+
|
| 101 |
+
game.make_move(human_move, "X")
|
| 102 |
+
if game.current_winner:
|
| 103 |
+
return {"output": "You win!", "board": game.board}
|
| 104 |
+
|
| 105 |
+
if game.empty_squares():
|
| 106 |
+
ai_move = game.ai_move()
|
| 107 |
+
if game.current_winner:
|
| 108 |
+
return {"output": "AI wins!", "board": game.board}
|
| 109 |
+
|
| 110 |
+
return {"output": "Your move.", "board": game.board}
|
| 111 |
+
|
| 112 |
+
def reset_game():
|
| 113 |
+
global game
|
| 114 |
+
game = TicTacToe()
|
| 115 |
+
return {"output": "Game reset! Your move.", "board": game.board}
|
| 116 |
+
|
| 117 |
+
# Gradio app
|
| 118 |
+
def render_game(human_move=None):
|
| 119 |
+
if human_move is None:
|
| 120 |
+
return reset_game()
|
| 121 |
+
return play_tic_tac_toe(int(human_move))
|
| 122 |
+
|
| 123 |
+
interface = gr.Interface(
|
| 124 |
+
fn=render_game,
|
| 125 |
+
inputs=gr.Dropdown(choices=[str(i) for i in range(9)], label="Your Move (0-8)"),
|
| 126 |
+
outputs=[
|
| 127 |
+
gr.Textbox(label="Game Status"),
|
| 128 |
+
gr.Textbox(label="Current Board State")
|
| 129 |
+
],
|
| 130 |
+
live=True,
|
| 131 |
+
title="Tic Tac Toe with AI",
|
| 132 |
+
description="Play Tic Tac Toe against an AI powered by the Minimax algorithm!"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
interface.launch()
|