pratikshahp commited on
Commit
3c51ffb
ยท
verified ยท
1 Parent(s): e5f0047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -5,7 +5,7 @@ import random
5
  def initialize_game():
6
  board = [["" for _ in range(3)] for _ in range(3)]
7
  current_player = "X"
8
- status = "Player 1's turn (X)"
9
  buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
10
  return board, current_player, status, *buttons
11
 
@@ -27,7 +27,7 @@ def check_draw(board):
27
  return all(cell != "" for row in board for cell in row)
28
 
29
  # Minimax algorithm for AI's move
30
- def minimax(board, depth, is_maximizing, difficulty):
31
  winner = check_winner(board)
32
  if winner == "X":
33
  return -10 + depth
@@ -42,7 +42,7 @@ def minimax(board, depth, is_maximizing, difficulty):
42
  for j in range(3):
43
  if board[i][j] == "":
44
  board[i][j] = "O"
45
- best = max(best, minimax(board, depth + 1, False, difficulty))
46
  board[i][j] = ""
47
  return best
48
  else:
@@ -51,30 +51,19 @@ def minimax(board, depth, is_maximizing, difficulty):
51
  for j in range(3):
52
  if board[i][j] == "":
53
  board[i][j] = "X"
54
- best = min(best, minimax(board, depth + 1, True, difficulty))
55
  board[i][j] = ""
56
  return best
57
 
58
  # Find the best move for AI
59
- def get_best_move(board, difficulty):
60
- if difficulty == "easy":
61
- # Random move for easy level
62
- empty_cells = [(i, j) for i in range(3) for j in range(3) if board[i][j] == ""]
63
- if empty_cells:
64
- return random.choice(empty_cells)
65
-
66
- if difficulty == "intermediate":
67
- # Mixed strategy for intermediate
68
- if random.random() < 0.5:
69
- return get_best_move(board, "easy")
70
-
71
  best_val = -float('inf')
72
  best_move = (-1, -1)
73
  for i in range(3):
74
  for j in range(3):
75
  if board[i][j] == "":
76
  board[i][j] = "O"
77
- move_val = minimax(board, 0, False, difficulty)
78
  board[i][j] = ""
79
  if move_val > best_val:
80
  best_move = (i, j)
@@ -82,60 +71,72 @@ def get_best_move(board, difficulty):
82
  return best_move
83
 
84
  # Handle a move
85
- def handle_move(board, current_player, button_idx, game_status, difficulty):
86
  if "wins" in game_status or "draw" in game_status:
87
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
88
  return board, current_player, game_status, *buttons
89
 
90
  row, col = divmod(button_idx, 3)
91
  if board[row][col] != "":
92
- status = f"Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})"
93
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
94
  return board, current_player, status, *buttons
95
 
96
  board[row][col] = current_player
97
  winner = check_winner(board)
98
  if winner:
99
- status = f"Player {1 if winner == 'X' else 2} ({winner}) wins! ๐ŸŽ‰"
100
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
101
  return board, current_player, status, *buttons
102
 
103
  if check_draw(board):
104
- status = "It's a draw! ๐Ÿค"
105
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
106
  return board, current_player, status, *buttons
107
 
108
  # AI's turn
109
  if current_player == "X":
110
  current_player = "O"
111
- ai_row, ai_col = get_best_move(board, difficulty)
112
  board[ai_row][ai_col] = "O"
113
  winner = check_winner(board)
114
  if winner:
115
- status = f"AI ({winner}) wins! ๐ŸŽ‰"
116
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
117
  return board, current_player, status, *buttons
118
 
119
  if check_draw(board):
120
- status = "It's a draw! ๐Ÿค"
121
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
122
  return board, current_player, status, *buttons
123
 
124
  current_player = "X"
125
- status = f"Player 1's turn (X)"
126
 
127
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
128
  return board, current_player, status, *buttons
129
 
130
  # Build the Gradio UI
131
- with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text-align: center;}") as tic_tac_toe:
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  gr.Markdown("## Tic-Tac-Toe with AI ๐ŸŽฎ")
133
- difficulty = gr.Radio(["easy", "intermediate", "impossible"], value="impossible", label="Difficulty Level")
134
 
135
  # Initialize states
136
  board_state = gr.State([["" for _ in range(3)] for _ in range(3)])
137
  current_player = gr.State("X")
138
- game_status = gr.Textbox(value="Player 1's turn (X)", label="Game Status", interactive=False)
139
 
140
  # Create grid buttons
141
  buttons = []
@@ -149,7 +150,7 @@ with gr.Blocks(css=".cell-btn {height: 100px; width: 100px; font-size: 2em; text
149
  for idx, btn in enumerate(buttons):
150
  btn.click(
151
  handle_move,
152
- inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status, difficulty],
153
  outputs=[board_state, current_player, game_status, *buttons],
154
  )
155
 
 
5
  def initialize_game():
6
  board = [["" for _ in range(3)] for _ in range(3)]
7
  current_player = "X"
8
+ status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
9
  buttons = [gr.Button(value="", elem_classes=["cell-btn"], interactive=True) for _ in range(9)]
10
  return board, current_player, status, *buttons
11
 
 
27
  return all(cell != "" for row in board for cell in row)
28
 
29
  # Minimax algorithm for AI's move
30
+ def minimax(board, depth, is_maximizing):
31
  winner = check_winner(board)
32
  if winner == "X":
33
  return -10 + depth
 
42
  for j in range(3):
43
  if board[i][j] == "":
44
  board[i][j] = "O"
45
+ best = max(best, minimax(board, depth + 1, False))
46
  board[i][j] = ""
47
  return best
48
  else:
 
51
  for j in range(3):
52
  if board[i][j] == "":
53
  board[i][j] = "X"
54
+ best = min(best, minimax(board, depth + 1, True))
55
  board[i][j] = ""
56
  return best
57
 
58
  # Find the best move for AI
59
+ def get_best_move(board):
 
 
 
 
 
 
 
 
 
 
 
60
  best_val = -float('inf')
61
  best_move = (-1, -1)
62
  for i in range(3):
63
  for j in range(3):
64
  if board[i][j] == "":
65
  board[i][j] = "O"
66
+ move_val = minimax(board, 0, False)
67
  board[i][j] = ""
68
  if move_val > best_val:
69
  best_move = (i, j)
 
71
  return best_move
72
 
73
  # Handle a move
74
+ def handle_move(board, current_player, button_idx, game_status):
75
  if "wins" in game_status or "draw" in game_status:
76
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
77
  return board, current_player, game_status, *buttons
78
 
79
  row, col = divmod(button_idx, 3)
80
  if board[row][col] != "":
81
+ status = f"<div style='font-size: 1.5em; color: orange; font-weight: bold;'>Invalid move! Player {1 if current_player == 'X' else 2}'s turn ({current_player})</div>"
82
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
83
  return board, current_player, status, *buttons
84
 
85
  board[row][col] = current_player
86
  winner = check_winner(board)
87
  if winner:
88
+ status = f"<div style='font-size: 2em; color: red; font-weight: bold;'>Player {1 if winner == 'X' else 2} ({winner}) wins! ๐ŸŽ‰</div>"
89
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
90
  return board, current_player, status, *buttons
91
 
92
  if check_draw(board):
93
+ status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! ๐Ÿค</div>"
94
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
95
  return board, current_player, status, *buttons
96
 
97
  # AI's turn
98
  if current_player == "X":
99
  current_player = "O"
100
+ ai_row, ai_col = get_best_move(board)
101
  board[ai_row][ai_col] = "O"
102
  winner = check_winner(board)
103
  if winner:
104
+ status = f"<div style='font-size: 2em; color: purple; font-weight: bold;'>AI ({winner}) wins! ๐ŸŽ‰</div>"
105
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
106
  return board, current_player, status, *buttons
107
 
108
  if check_draw(board):
109
+ status = "<div style='font-size: 2em; color: blue; font-weight: bold;'>It's a draw! ๐Ÿค</div>"
110
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"], interactive=False) for i in range(9)]
111
  return board, current_player, status, *buttons
112
 
113
  current_player = "X"
114
+ status = "<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>"
115
 
116
  buttons = [gr.Button(value=board[i // 3][i % 3], elem_classes=["cell-btn"]) for i in range(9)]
117
  return board, current_player, status, *buttons
118
 
119
  # Build the Gradio UI
120
+ with gr.Blocks(css="""
121
+ .cell-btn {
122
+ height: 100px;
123
+ width: 100px;
124
+ font-size: 2em;
125
+ text-align: center;
126
+ background-color: #f0f0f0;
127
+ border: 2px solid #ddd;
128
+ transition: all 0.3s;
129
+ }
130
+ .cell-btn:hover {
131
+ background-color: #e0e0e0;
132
+ }
133
+ """) as tic_tac_toe:
134
  gr.Markdown("## Tic-Tac-Toe with AI ๐ŸŽฎ")
135
+ game_status = gr.Markdown(value="<div style='font-size: 1.5em; color: green; font-weight: bold;'>Player 1's turn (X)</div>")
136
 
137
  # Initialize states
138
  board_state = gr.State([["" for _ in range(3)] for _ in range(3)])
139
  current_player = gr.State("X")
 
140
 
141
  # Create grid buttons
142
  buttons = []
 
150
  for idx, btn in enumerate(buttons):
151
  btn.click(
152
  handle_move,
153
+ inputs=[board_state, current_player, gr.Number(idx, visible=False), game_status],
154
  outputs=[board_state, current_player, game_status, *buttons],
155
  )
156