mrhaseenullah commited on
Commit
5c34f36
·
verified ·
1 Parent(s): e15098b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -144
app.py CHANGED
@@ -2,146 +2,7 @@ import random
2
  import numpy as np
3
  import gradio as gr
4
 
5
- # [All classes remain exactly the same - TicTacToe, MinimaxPlayer, QLearningPlayer]
6
- class TicTacToe:
7
- def __init__(self):
8
- self.board = [' '] * 9
9
- self.current_player = 'X'
10
-
11
- def display_board(self):
12
- print("\n")
13
- for i in range(3):
14
- print(" | ".join(self.board[i * 3:(i + 1) * 3]))
15
- if i < 2:
16
- print("---------")
17
- print("\n")
18
-
19
- def make_move(self, position):
20
- if self.board[position] == ' ':
21
- self.board[position] = self.current_player
22
- return True
23
- return False
24
-
25
- def switch_player(self):
26
- self.current_player = 'O' if self.current_player == 'X' else 'X'
27
-
28
- def check_winner(self):
29
- winning_combinations = [
30
- [0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
31
- [0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
32
- [0, 4, 8], [2, 4, 6] # Diagonals
33
- ]
34
- for combo in winning_combinations:
35
- if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != ' ':
36
- return self.board[combo[0]]
37
- return None
38
-
39
- def is_draw(self):
40
- return ' ' not in self.board
41
-
42
- def reset_board(self):
43
- self.board = [' '] * 9
44
- self.current_player = 'X'
45
-
46
- class MinimaxPlayer:
47
- def __init__(self, symbol):
48
- self.symbol = symbol
49
-
50
- def minimax(self, game, is_maximizing):
51
- winner = game.check_winner()
52
- if winner == self.symbol:
53
- return 1
54
- elif winner == ('O' if self.symbol == 'X' else 'X'):
55
- return -1
56
- elif game.is_draw():
57
- return 0
58
-
59
- if is_maximizing:
60
- best_score = -float('inf')
61
- for i in range(9):
62
- if game.board[i] == ' ':
63
- game.board[i] = self.symbol
64
- score = self.minimax(game, False)
65
- game.board[i] = ' '
66
- best_score = max(score, best_score)
67
- return best_score
68
- else:
69
- best_score = float('inf')
70
- for i in range(9):
71
- if game.board[i] == ' ':
72
- game.board[i] = ('O' if self.symbol == 'X' else 'X')
73
- score = self.minimax(game, True)
74
- game.board[i] = ' '
75
- best_score = min(score, best_score)
76
- return best_score
77
-
78
- def get_move(self, game):
79
- best_score = -float('inf')
80
- best_move = None
81
- for i in range(9):
82
- if game.board[i] == ' ':
83
- game.board[i] = self.symbol
84
- score = self.minimax(game, False)
85
- game.board[i] = ' '
86
- if score > best_score:
87
- best_score = score
88
- best_move = i
89
- return best_move
90
-
91
- class QLearningPlayer:
92
- def __init__(self, symbol, learning_rate=0.1, discount_factor=0.9, exploration_rate=1.0):
93
- self.symbol = symbol
94
- self.q_table = {}
95
- self.learning_rate = learning_rate
96
- self.discount_factor = discount_factor
97
- self.exploration_rate = exploration_rate
98
-
99
- def get_state(self, game):
100
- return ''.join(game.board)
101
-
102
- def choose_action(self, game):
103
- state = self.get_state(game)
104
- if random.random() < self.exploration_rate:
105
- return random.choice([i for i in range(9) if game.board[i] == ' '])
106
- if state not in self.q_table:
107
- self.q_table[state] = np.zeros(9)
108
- return np.argmax(self.q_table[state])
109
-
110
- def update_q_table(self, state, action, reward, next_state):
111
- if state not in self.q_table:
112
- self.q_table[state] = np.zeros(9)
113
- if next_state not in self.q_table:
114
- self.q_table[next_state] = np.zeros(9)
115
- self.q_table[state][action] += self.learning_rate * (
116
- reward + self.discount_factor * np.max(self.q_table[next_state]) - self.q_table[state][action]
117
- )
118
-
119
- def train(self, episodes):
120
- for _ in range(episodes):
121
- game = TicTacToe()
122
- state = self.get_state(game)
123
- while True:
124
- action = self.choose_action(game)
125
- game.make_move(action)
126
- next_state = self.get_state(game)
127
- winner = game.check_winner()
128
- if winner == self.symbol:
129
- reward = 1
130
- self.update_q_table(state, action, reward, next_state)
131
- break
132
- elif winner:
133
- reward = -1
134
- self.update_q_table(state, action, reward, next_state)
135
- break
136
- elif game.is_draw():
137
- reward = 0.5
138
- self.update_q_table(state, action, reward, next_state)
139
- break
140
- else:
141
- reward = 0
142
- self.update_q_table(state, action, reward, next_state)
143
- game.switch_player()
144
- state = next_state
145
 
146
  # Global game instance
147
  game = TicTacToe()
@@ -162,9 +23,9 @@ def render_board():
162
  board.append(row)
163
  return board
164
 
165
- def make_move(evt: gr.SelectData, game_mode, difficulty): # Changed the parameters here
166
  """Handle player moves and AI responses"""
167
- row, col = evt.index # Get row and col from the event
168
  position = row * 3 + col
169
  status = ""
170
 
@@ -225,9 +86,10 @@ def create_gui():
225
  value="medium"
226
  )
227
 
 
228
  board = gr.DataFrame(
229
  render_board(),
230
- headers=False,
231
  interactive=True,
232
  col_count=(3, "fixed"),
233
  row_count=(3, "fixed")
@@ -236,7 +98,7 @@ def create_gui():
236
  status = gr.Textbox(value="Current player: X", label="Status")
237
  reset_btn = gr.Button("Reset Game")
238
 
239
- # Event handlers - Fixed the event handling here
240
  board.select(
241
  make_move,
242
  [game_mode, difficulty],
 
2
  import numpy as np
3
  import gradio as gr
4
 
5
+ [Previous TicTacToe, MinimaxPlayer, and QLearningPlayer classes remain exactly the same]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Global game instance
8
  game = TicTacToe()
 
23
  board.append(row)
24
  return board
25
 
26
+ def make_move(evt: gr.SelectData, game_mode, difficulty):
27
  """Handle player moves and AI responses"""
28
+ row, col = evt.index
29
  position = row * 3 + col
30
  status = ""
31
 
 
86
  value="medium"
87
  )
88
 
89
+ # Fixed DataFrame initialization
90
  board = gr.DataFrame(
91
  render_board(),
92
+ headers=[""]*3, # Empty headers for 3 columns
93
  interactive=True,
94
  col_count=(3, "fixed"),
95
  row_count=(3, "fixed")
 
98
  status = gr.Textbox(value="Current player: X", label="Status")
99
  reset_btn = gr.Button("Reset Game")
100
 
101
+ # Event handlers
102
  board.select(
103
  make_move,
104
  [game_mode, difficulty],