mrhaseenullah commited on
Commit
282332e
·
verified ·
1 Parent(s): d436fd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -152
app.py CHANGED
@@ -2,152 +2,7 @@ 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 display_board(self):
11
- print("\n")
12
- for i in range(3):
13
- print(" | ".join(self.board[i * 3:(i + 1) * 3]))
14
- if i < 2:
15
- print("---------")
16
- print("\n")
17
-
18
- def make_move(self, position):
19
- if self.board[position] == ' ':
20
- self.board[position] = self.current_player
21
- return True
22
- return False
23
-
24
- def switch_player(self):
25
- self.current_player = 'O' if self.current_player == 'X' else 'X'
26
-
27
- def check_winner(self):
28
- winning_combinations = [
29
- [0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
30
- [0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
31
- [0, 4, 8], [2, 4, 6] # Diagonals
32
- ]
33
- for combo in winning_combinations:
34
- if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != ' ':
35
- return self.board[combo[0]]
36
- return None
37
-
38
- def is_draw(self):
39
- return ' ' not in self.board
40
-
41
- def reset_board(self):
42
- self.board = [' '] * 9
43
- self.current_player = 'X'
44
-
45
- class MinimaxPlayer:
46
- def __init__(self, symbol):
47
- self.symbol = symbol
48
-
49
- def minimax(self, game, is_maximizing):
50
- winner = game.check_winner()
51
- if winner == self.symbol:
52
- return 1
53
- elif winner == ('O' if self.symbol == 'X' else 'X'):
54
- return -1
55
- elif game.is_draw():
56
- return 0
57
-
58
- if is_maximizing:
59
- best_score = -float('inf')
60
- for i in range(9):
61
- if game.board[i] == ' ':
62
- game.board[i] = self.symbol
63
- score = self.minimax(game, False)
64
- game.board[i] = ' '
65
- best_score = max(score, best_score)
66
- return best_score
67
- else:
68
- best_score = float('inf')
69
- for i in range(9):
70
- if game.board[i] == ' ':
71
- game.board[i] = ('O' if self.symbol == 'X' else 'X')
72
- score = self.minimax(game, True)
73
- game.board[i] = ' '
74
- best_score = min(score, best_score)
75
- return best_score
76
-
77
- def get_move(self, game):
78
- best_score = -float('inf')
79
- best_move = None
80
- for i in range(9):
81
- if game.board[i] == ' ':
82
- game.board[i] = self.symbol
83
- score = self.minimax(game, False)
84
- game.board[i] = ' '
85
- if score > best_score:
86
- best_score = score
87
- best_move = i
88
- return best_move
89
-
90
- class QLearningPlayer:
91
- def __init__(self, symbol, learning_rate=0.1, discount_factor=0.9, exploration_rate=1.0):
92
- self.symbol = symbol
93
- self.q_table = {}
94
- self.learning_rate = learning_rate
95
- self.discount_factor = discount_factor
96
- self.exploration_rate = exploration_rate
97
-
98
- def get_state(self, game):
99
- return ''.join(game.board)
100
-
101
- def choose_action(self, game):
102
- state = self.get_state(game)
103
- if random.random() < self.exploration_rate:
104
- return random.choice([i for i in range(9) if game.board[i] == ' '])
105
- if state not in self.q_table:
106
- self.q_table[state] = np.zeros(9)
107
- return np.argmax(self.q_table[state])
108
-
109
- def update_q_table(self, state, action, reward, next_state):
110
- if state not in self.q_table:
111
- self.q_table[state] = np.zeros(9)
112
- if next_state not in self.q_table:
113
- self.q_table[next_state] = np.zeros(9)
114
- self.q_table[state][action] += self.learning_rate * (
115
- reward + self.discount_factor * np.max(self.q_table[next_state]) - self.q_table[state][action]
116
- )
117
-
118
- def train(self, episodes):
119
- for _ in range(episodes):
120
- game = TicTacToe()
121
- state = self.get_state(game)
122
- while True:
123
- action = self.choose_action(game)
124
- game.make_move(action)
125
- next_state = self.get_state(game)
126
- winner = game.check_winner()
127
- if winner == self.symbol:
128
- reward = 1
129
- self.update_q_table(state, action, reward, next_state)
130
- break
131
- elif winner:
132
- reward = -1
133
- self.update_q_table(state, action, reward, next_state)
134
- break
135
- elif game.is_draw():
136
- reward = 0.5
137
- self.update_q_table(state, action, reward, next_state)
138
- break
139
- else:
140
- reward = 0
141
- self.update_q_table(state, action, reward, next_state)
142
- game.switch_player()
143
- state = next_state
144
-
145
- # Global game instance
146
- game = TicTacToe()
147
- game_mode = "human_vs_human"
148
- minimax_player = MinimaxPlayer('O')
149
- qlearning_player = QLearningPlayer('O')
150
- qlearning_player.train(1000) # Pre-train the Q-learning agent
151
 
152
  def render_board():
153
  """Convert the game board to a grid of buttons"""
@@ -171,9 +26,13 @@ def make_move(evt: gr.SelectData, game_mode, difficulty):
171
  game.make_move(position)
172
  if check_game_end():
173
  return render_board(), get_game_status()
174
-
175
- # AI move (if playing against AI)
176
- if game_mode != "human_vs_human":
 
 
 
 
177
  game.switch_player()
178
  if game_mode == "minimax":
179
  ai_move = minimax_player.get_move(game)
@@ -199,12 +58,16 @@ def get_game_status():
199
  elif game.is_draw():
200
  return "It's a draw!"
201
  else:
202
- return f"Current player: {game.current_player}"
 
 
 
 
203
 
204
  def reset_game(game_mode, difficulty):
205
  """Reset the game board"""
206
  game.reset_board()
207
- return render_board(), "Game reset! Current player: X"
208
 
209
  def create_gui():
210
  """Create the Gradio interface"""
@@ -231,7 +94,7 @@ def create_gui():
231
  row_count=(3, "fixed")
232
  )
233
 
234
- status = gr.Textbox(value="Current player: X", label="Status")
235
  reset_btn = gr.Button("Reset Game")
236
 
237
  # Event handlers
 
2
  import numpy as np
3
  import gradio as gr
4
 
5
+ # [Previous class definitions remain the same until render_board()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def render_board():
8
  """Convert the game board to a grid of buttons"""
 
26
  game.make_move(position)
27
  if check_game_end():
28
  return render_board(), get_game_status()
29
+
30
+ if game_mode == "human_vs_human":
31
+ # Switch to other player for human vs human
32
+ game.switch_player()
33
+ return render_board(), get_game_status()
34
+ else:
35
+ # AI move (if playing against AI)
36
  game.switch_player()
37
  if game_mode == "minimax":
38
  ai_move = minimax_player.get_move(game)
 
58
  elif game.is_draw():
59
  return "It's a draw!"
60
  else:
61
+ current_symbol = game.current_player
62
+ if game_mode == "human_vs_human":
63
+ return f"Player {current_symbol}'s turn"
64
+ else:
65
+ return f"Current player: {current_symbol}"
66
 
67
  def reset_game(game_mode, difficulty):
68
  """Reset the game board"""
69
  game.reset_board()
70
+ return render_board(), "Player X's turn"
71
 
72
  def create_gui():
73
  """Create the Gradio interface"""
 
94
  row_count=(3, "fixed")
95
  )
96
 
97
+ status = gr.Textbox(value="Player X's turn", label="Status")
98
  reset_btn = gr.Button("Reset Game")
99
 
100
  # Event handlers