saima730 commited on
Commit
ca7b645
ยท
verified ยท
1 Parent(s): f669438

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+
4
+ class TicTacToeAI:
5
+ def __init__(self):
6
+ self.board = np.full((3, 3), "", dtype=str)
7
+ self.human = "X"
8
+ self.ai = "O"
9
+
10
+ def is_winner(self, board, player):
11
+ return any([
12
+ np.all(row == player) for row in board
13
+ ]) or any([
14
+ np.all(board[:, i] == player) for i in range(3)
15
+ ]) or np.all(np.diag(board) == player) or np.all(np.diag(np.fliplr(board)) == player)
16
+
17
+ def is_draw(self, board):
18
+ return "" not in board and not self.is_winner(board, self.human) and not self.is_winner(board, self.ai)
19
+
20
+ def available_moves(self, board):
21
+ return [(r, c) for r in range(3) for c in range(3) if board[r, c] == ""]
22
+
23
+ def minimax(self, board, depth, is_maximizing):
24
+ if self.is_winner(board, self.ai):
25
+ return 10 - depth
26
+ if self.is_winner(board, self.human):
27
+ return depth - 10
28
+ if self.is_draw(board):
29
+ return 0
30
+
31
+ if is_maximizing:
32
+ best_score = -np.inf
33
+ for (r, c) in self.available_moves(board):
34
+ board[r, c] = self.ai
35
+ score = self.minimax(board, depth + 1, False)
36
+ board[r, c] = ""
37
+ best_score = max(best_score, score)
38
+ return best_score
39
+ else:
40
+ best_score = np.inf
41
+ for (r, c) in self.available_moves(board):
42
+ board[r, c] = self.human
43
+ score = self.minimax(board, depth + 1, True)
44
+ board[r, c] = ""
45
+ best_score = min(best_score, score)
46
+ return best_score
47
+
48
+ def best_move(self):
49
+ best_score = -np.inf
50
+ move = None
51
+ for (r, c) in self.available_moves(self.board):
52
+ self.board[r, c] = self.ai
53
+ score = self.minimax(self.board, 0, False)
54
+ self.board[r, c] = ""
55
+ if score > best_score:
56
+ best_score = score
57
+ move = (r, c)
58
+ return move
59
+
60
+ game = TicTacToeAI()
61
+ status = "Your Turn (X)"
62
+
63
+ def play(row, col):
64
+ global status, game
65
+ if game.board[row, col] != "":
66
+ return game.board.tolist(), status, "Invalid move."
67
+
68
+ game.board[row, col] = game.human
69
+ if game.is_winner(game.board, game.human):
70
+ status = "You Win! ๐ŸŽ‰"
71
+ return game.board.tolist(), status, "Try again?"
72
+ elif game.is_draw(game.board):
73
+ status = "Draw! ๐Ÿค"
74
+ return game.board.tolist(), status, "Try again?"
75
+
76
+ ai_r, ai_c = game.best_move()
77
+ if ai_r is not None:
78
+ game.board[ai_r, ai_c] = game.ai
79
+ if game.is_winner(game.board, game.ai):
80
+ status = "AI Wins! ๐Ÿค–"
81
+ return game.board.tolist(), status, "Try again?"
82
+ elif game.is_draw(game.board):
83
+ status = "Draw! ๐Ÿค"
84
+ return game.board.tolist(), status, "Try again?"
85
+
86
+ status = "Your Turn (X)"
87
+ return game.board.tolist(), status, ""
88
+
89
+ def reset():
90
+ global game, status
91
+ game = TicTacToeAI()
92
+ status = "Your Turn (X)"
93
+ return game.board.tolist(), status, ""
94
+
95
+ with gr.Blocks() as demo:
96
+ gr.Markdown("## ๐ŸŽฎ Smart Tic-Tac-Toe vs AI")
97
+ gr.Markdown("Click a cell to make your move. You are X, AI is O.")
98
+ board = gr.Dataframe(value=game.board.tolist(), row_count=3, col_count=3, interactive=False)
99
+ status_text = gr.Textbox(value=status, interactive=False, label="Game Status")
100
+ tips = gr.Textbox(label="Tip", interactive=False)
101
+
102
+ with gr.Row():
103
+ for r in range(3):
104
+ for c in range(3):
105
+ gr.Button(f"{r},{c}").click(
106
+ play, inputs=[gr.Number(value=r), gr.Number(value=c)],
107
+ outputs=[board, status_text, tips]
108
+ )
109
+ gr.Button("Reset Game ๐Ÿ”„").click(reset, outputs=[board, status_text, tips])
110
+
111
+ demo.launch()