pratikshahp commited on
Commit
57709f6
·
verified ·
1 Parent(s): 93c5816

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -91
app.py CHANGED
@@ -1,12 +1,9 @@
1
  import gradio as gr
2
  import random
3
- import copy
4
 
5
- # Initialize the board
6
  def initialize_board():
7
  return [["" for _ in range(3)] for _ in range(3)]
8
 
9
- # Check for winner
10
  def check_winner(board):
11
  for row in board:
12
  if row[0] == row[1] == row[2] and row[0] != "":
@@ -22,66 +19,17 @@ def check_winner(board):
22
  return "Draw"
23
  return None
24
 
25
- # Minimax algorithm
26
- def minimax(board, depth, is_maximizing):
27
- winner = check_winner(board)
28
- if winner == "X":
29
- return -10 + depth
30
- elif winner == "O":
31
- return 10 - depth
32
- elif winner == "Draw":
33
- return 0
34
-
35
- if is_maximizing:
36
- best_score = -float("inf")
37
- for row in range(3):
38
- for col in range(3):
39
- if board[row][col] == "":
40
- board[row][col] = "O"
41
- score = minimax(board, depth + 1, False)
42
- board[row][col] = ""
43
- best_score = max(score, best_score)
44
- return best_score
45
- else:
46
- best_score = float("inf")
47
- for row in range(3):
48
- for col in range(3):
49
- if board[row][col] == "":
50
- board[row][col] = "X"
51
- score = minimax(board, depth + 1, True)
52
- board[row][col] = ""
53
- best_score = min(score, best_score)
54
- return best_score
55
-
56
- # AI move using Minimax
57
  def ai_move(board, difficulty):
 
58
  if difficulty == "Easy":
59
- empty_cells = [(row, col) for row in range(3) for col in range(3) if board[row][col] == ""]
60
  return random.choice(empty_cells)
 
 
 
61
 
62
- if difficulty == "Intermediate":
63
- if random.random() < 0.5:
64
- empty_cells = [(row, col) for row in range(3) for col in range(3) if board[row][col] == ""]
65
- return random.choice(empty_cells)
66
-
67
- best_score = -float("inf")
68
- best_move = None
69
- for row in range(3):
70
- for col in range(3):
71
- if board[row][col] == "":
72
- board[row][col] = "O"
73
- score = minimax(board, 0, False)
74
- board[row][col] = ""
75
- if score > best_score:
76
- best_score = score
77
- best_move = (row, col)
78
- return best_move
79
-
80
- # Handle a move
81
- def handle_move(state, row, col, difficulty):
82
- board, current_player, status = state
83
- if board[row][col] != "" or status != "Game On":
84
- return state
85
 
86
  board[row][col] = current_player
87
  winner = check_winner(board)
@@ -101,53 +49,42 @@ def handle_move(state, row, col, difficulty):
101
 
102
  return board, current_player, "Game On"
103
 
104
- # Gradio Interface
105
  def reset_game():
106
  return initialize_board(), "X", "Game On"
107
 
108
- def display_board(board):
109
  return [[gr.Button.update(value=board[row][col], interactive=(board[row][col] == "")) for col in range(3)] for row in range(3)]
110
 
111
- def on_click(state, row, col, difficulty):
112
- new_state = handle_move(state, row, col, difficulty)
113
- board, _, status = new_state
114
- return new_state, display_board(board), status
115
-
116
  def main():
117
  with gr.Blocks() as app:
118
- gr.Markdown("# Tic Tac Toe with Smart AI")
119
 
120
- difficulty = gr.Radio(["Easy", "Intermediate", "Impossible"], value="Impossible", label="Select Difficulty")
121
 
122
  board = gr.State(initialize_board())
123
  current_player = gr.State("X")
124
- status = gr.State("Game On")
125
-
126
- board_buttons = [
127
- [gr.Button("", elem_id=f"cell-{row}-{col}", interactive=True) for col in range(3)]
128
- for row in range(3)
129
- ]
130
-
131
- output_board = []
132
- for row_buttons in board_buttons:
133
- with gr.Row():
134
- for button in row_buttons:
135
- output_board.append(button)
136
-
137
- status_display = gr.Textbox("Game On", interactive=False, label="Status")
138
-
139
- reset_button = gr.Button("Reset Game")
140
 
 
141
  for row in range(3):
142
- for col in range(3):
143
- board_buttons[row][col].click(
144
- on_click,
145
- inputs=[board, current_player, status, gr.State(row), gr.State(col), difficulty],
146
- outputs=[board, status_display]
147
- )
 
 
 
148
 
 
149
  reset_button.click(
150
- reset_game, inputs=[], outputs=[board, current_player, status, status_display]
 
 
151
  )
152
 
153
  return app
 
1
  import gradio as gr
2
  import random
 
3
 
 
4
  def initialize_board():
5
  return [["" for _ in range(3)] for _ in range(3)]
6
 
 
7
  def check_winner(board):
8
  for row in board:
9
  if row[0] == row[1] == row[2] and row[0] != "":
 
19
  return "Draw"
20
  return None
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def ai_move(board, difficulty):
23
+ empty_cells = [(row, col) for row in range(3) for col in range(3) if board[row][col] == ""]
24
  if difficulty == "Easy":
 
25
  return random.choice(empty_cells)
26
+ if difficulty == "Intermediate" and random.random() < 0.5:
27
+ return random.choice(empty_cells)
28
+ return random.choice(empty_cells)
29
 
30
+ def handle_move(board, current_player, row, col, difficulty):
31
+ if board[row][col] != "":
32
+ return board, current_player, "Invalid move!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  board[row][col] = current_player
35
  winner = check_winner(board)
 
49
 
50
  return board, current_player, "Game On"
51
 
 
52
  def reset_game():
53
  return initialize_board(), "X", "Game On"
54
 
55
+ def update_board(board):
56
  return [[gr.Button.update(value=board[row][col], interactive=(board[row][col] == "")) for col in range(3)] for row in range(3)]
57
 
58
+ def on_click(board, current_player, row, col, difficulty):
59
+ board, current_player, status = handle_move(board, current_player, row, col, difficulty)
60
+ return board, current_player, update_board(board), status
 
 
61
  def main():
62
  with gr.Blocks() as app:
63
+ gr.Markdown("# Tic Tac Toe")
64
 
65
+ difficulty = gr.Radio(["Easy", "Intermediate"], label="Difficulty", value="Easy")
66
 
67
  board = gr.State(initialize_board())
68
  current_player = gr.State("X")
69
+ status = gr.Textbox("Game On", label="Status")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ board_buttons = []
72
  for row in range(3):
73
+ with gr.Row() as row_ui:
74
+ for col in range(3):
75
+ btn = gr.Button("", elem_id=f"cell-{row}-{col}", interactive=True)
76
+ btn.click(
77
+ on_click,
78
+ inputs=[board, current_player, gr.State(row), gr.State(col), difficulty],
79
+ outputs=[board, current_player, row_ui, status]
80
+ )
81
+ board_buttons.append(btn)
82
 
83
+ reset_button = gr.Button("Reset Game")
84
  reset_button.click(
85
+ reset_game,
86
+ inputs=[],
87
+ outputs=[board, current_player, update_board(initialize_board()), status]
88
  )
89
 
90
  return app