Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
class TicTacToe:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.board = [' '] * 9
|
| 8 |
+
self.current_player = 'X'
|
| 9 |
+
|
| 10 |
+
def make_move(self, position):
|
| 11 |
+
if self.board[position] == ' ':
|
| 12 |
+
self.board[position] = self.current_player
|
| 13 |
+
return True
|
| 14 |
+
return False
|
| 15 |
+
|
| 16 |
+
def switch_player(self):
|
| 17 |
+
self.current_player = 'O' if self.current_player == 'X' else 'X'
|
| 18 |
+
|
| 19 |
+
def check_winner(self):
|
| 20 |
+
winning_combinations = [
|
| 21 |
+
[0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
|
| 22 |
+
[0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
|
| 23 |
+
[0, 4, 8], [2, 4, 6] # Diagonals
|
| 24 |
+
]
|
| 25 |
+
for combo in winning_combinations:
|
| 26 |
+
if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != ' ':
|
| 27 |
+
return self.board[combo[0]]
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
def is_draw(self):
|
| 31 |
+
return ' ' not in self.board
|
| 32 |
+
|
| 33 |
+
def reset_board(self):
|
| 34 |
+
self.board = [' '] * 9
|
| 35 |
+
self.current_player = 'X'
|
| 36 |
+
|
| 37 |
+
class MinimaxPlayer:
|
| 38 |
+
def __init__(self, symbol):
|
| 39 |
+
self.symbol = symbol
|
| 40 |
+
|
| 41 |
+
def minimax(self, game, is_maximizing):
|
| 42 |
+
winner = game.check_winner()
|
| 43 |
+
if winner == self.symbol:
|
| 44 |
+
return 1
|
| 45 |
+
elif winner == ('O' if self.symbol == 'X' else 'X'):
|
| 46 |
+
return -1
|
| 47 |
+
elif game.is_draw():
|
| 48 |
+
return 0
|
| 49 |
+
|
| 50 |
+
if is_maximizing:
|
| 51 |
+
best_score = -float('inf')
|
| 52 |
+
for i in range(9):
|
| 53 |
+
if game.board[i] == ' ':
|
| 54 |
+
game.board[i] = self.symbol
|
| 55 |
+
score = self.minimax(game, False)
|
| 56 |
+
game.board[i] = ' '
|
| 57 |
+
best_score = max(score, best_score)
|
| 58 |
+
return best_score
|
| 59 |
+
else:
|
| 60 |
+
best_score = float('inf')
|
| 61 |
+
for i in range(9):
|
| 62 |
+
if game.board[i] == ' ':
|
| 63 |
+
game.board[i] = ('O' if self.symbol == 'X' else 'X')
|
| 64 |
+
score = self.minimax(game, True)
|
| 65 |
+
game.board[i] = ' '
|
| 66 |
+
best_score = min(score, best_score)
|
| 67 |
+
return best_score
|
| 68 |
+
|
| 69 |
+
def get_move(self, game):
|
| 70 |
+
best_score = -float('inf')
|
| 71 |
+
best_move = None
|
| 72 |
+
for i in range(9):
|
| 73 |
+
if game.board[i] == ' ':
|
| 74 |
+
game.board[i] = self.symbol
|
| 75 |
+
score = self.minimax(game, False)
|
| 76 |
+
game.board[i] = ' '
|
| 77 |
+
if score > best_score:
|
| 78 |
+
best_score = score
|
| 79 |
+
best_move = i
|
| 80 |
+
return best_move
|
| 81 |
+
|
| 82 |
+
# Global game instance
|
| 83 |
+
game = TicTacToe()
|
| 84 |
+
minimax_player = MinimaxPlayer('O')
|
| 85 |
+
|
| 86 |
+
def render_board():
|
| 87 |
+
return [[game.board[i * 3 + j] for j in range(3)] for i in range(3)]
|
| 88 |
+
|
| 89 |
+
def make_move(row, col):
|
| 90 |
+
position = row * 3 + col
|
| 91 |
+
if game.board[position] == ' ':
|
| 92 |
+
game.make_move(position)
|
| 93 |
+
winner = game.check_winner()
|
| 94 |
+
if winner:
|
| 95 |
+
return render_board(), f"Player {winner} wins!"
|
| 96 |
+
elif game.is_draw():
|
| 97 |
+
return render_board(), "It's a draw!"
|
| 98 |
+
game.switch_player()
|
| 99 |
+
|
| 100 |
+
# Minimax AI Move
|
| 101 |
+
ai_move = minimax_player.get_move(game)
|
| 102 |
+
game.make_move(ai_move)
|
| 103 |
+
winner = game.check_winner()
|
| 104 |
+
if winner:
|
| 105 |
+
return render_board(), f"Player {winner} wins!"
|
| 106 |
+
elif game.is_draw():
|
| 107 |
+
return render_board(), "It's a draw!"
|
| 108 |
+
game.switch_player()
|
| 109 |
+
|
| 110 |
+
return render_board(), f"Current player: {game.current_player}"
|
| 111 |
+
|
| 112 |
+
def reset_game():
|
| 113 |
+
game.reset_board()
|
| 114 |
+
return render_board(), "Game reset! Current player: X"
|
| 115 |
+
|
| 116 |
+
with gr.Blocks() as demo:
|
| 117 |
+
gr.Markdown("# Tic Tac Toe with AI")
|
| 118 |
+
board = gr.DataFrame(render_board(), headers=False, interactive=True)
|
| 119 |
+
status = gr.Textbox(value="Game reset! Current player: X")
|
| 120 |
+
reset_button = gr.Button("Reset Game")
|
| 121 |
+
|
| 122 |
+
board.select(make_move, ["row", "column"], [board, status])
|
| 123 |
+
reset_button.click(reset_game, [], [board, status])
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
demo.launch()
|