-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_ai_pygame.py
246 lines (211 loc) · 8.6 KB
/
tictactoe_ai_pygame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import pygame
import sys
import random
# Initialize the game board and variables
board = [[' ' for _ in range(3)] for _ in range(3)]
player_turn = 'X'
difficulty = 'Easy' # Default difficulty
# Pygame settings
pygame.init()
WIDTH, HEIGHT = 300, 300
SQUARE_SIZE = WIDTH // 3
LINE_WIDTH = 15
CIRCLE_RADIUS = SQUARE_SIZE // 3
CIRCLE_WIDTH = 15
CROSS_WIDTH = 25
SPACE = SQUARE_SIZE // 4
# Colors
BG_COLOR = (28, 170, 156)
LINE_COLOR = (23, 145, 135)
CIRCLE_COLOR = (239, 231, 200)
CROSS_COLOR = (84, 84, 84)
BUTTON_COLOR = (100, 100, 100)
TEXT_COLOR = (255, 255, 255)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tic Tac Toe')
font = pygame.font.Font(None, 36)
# Draw grid lines
def draw_lines():
pygame.draw.line(screen, LINE_COLOR, (0, SQUARE_SIZE), (WIDTH, SQUARE_SIZE), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (0, 2 * SQUARE_SIZE), (WIDTH, 2 * SQUARE_SIZE), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (SQUARE_SIZE, 0), (SQUARE_SIZE, HEIGHT), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (2 * SQUARE_SIZE, 0), (2 * SQUARE_SIZE, HEIGHT), LINE_WIDTH)
# Draw 'X' and 'O' marks
def draw_figures():
for row in range(3):
for col in range(3):
if board[row][col] == 'O':
pygame.draw.circle(screen, CIRCLE_COLOR, (int(col * SQUARE_SIZE + SQUARE_SIZE // 2), int(row * SQUARE_SIZE + SQUARE_SIZE // 2)), CIRCLE_RADIUS, CIRCLE_WIDTH)
elif board[row][col] == 'X':
pygame.draw.line(screen, CROSS_COLOR, (col * SQUARE_SIZE + SPACE, row * SQUARE_SIZE + SQUARE_SIZE - SPACE), (col * SQUARE_SIZE + SQUARE_SIZE - SPACE, row * SQUARE_SIZE + SPACE), CROSS_WIDTH)
pygame.draw.line(screen, CROSS_COLOR, (col * SQUARE_SIZE + SPACE, row * SQUARE_SIZE + SPACE), (col * SQUARE_SIZE + SQUARE_SIZE - SPACE, row * SQUARE_SIZE + SQUARE_SIZE - SPACE), CROSS_WIDTH)
# Check for a winner
def check_winner(player):
# Check rows, columns, and diagonals
for i in range(3):
if all([cell == player for cell in board[i]]): # Row
return True
if all([board[j][i] == player for j in range(3)]): # Column
return True
# Check diagonals
if all([board[i][i] == player for i in range(3)]) or all([board[i][2 - i] == player for i in range(3)]):
return True
return False
# Check for a draw
def is_draw():
return all([cell != ' ' for row in board for cell in row])
# AI move based on difficulty
def ai_move():
if difficulty == 'Easy':
random_move()
elif difficulty == 'Medium':
if random.choice([True, False]):
random_move()
else:
optimal_move()
elif difficulty == 'Hard':
optimal_move()
# Make a random move for the AI
def random_move():
available_moves = [(i, j) for i in range(3) for j in range(3) if board[i][j] == ' ']
if available_moves:
move = random.choice(available_moves)
board[move[0]][move[1]] = 'O'
# Make the optimal move using Minimax
def optimal_move():
best_score = -float('inf')
best_move = None
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
score = minimax(board, 0, False)
board[i][j] = ' '
if score > best_score:
best_score = score
best_move = (i, j)
if best_move:
board[best_move[0]][best_move[1]] = 'O'
# Minimax algorithm
def minimax(board, depth, is_maximizing):
if check_winner('O'):
return 1
if check_winner('X'):
return -1
if is_draw():
return 0
if is_maximizing:
best_score = -float('inf')
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
score = minimax(board, depth + 1, False)
board[i][j] = ' '
best_score = max(score, best_score)
return best_score
else:
best_score = float('inf')
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'X'
score = minimax(board, depth + 1, True)
board[i][j] = ' '
best_score = min(score, best_score)
return best_score
# Difficulty selection popup
def select_difficulty():
global difficulty
running = True
while running:
screen.fill(BG_COLOR)
# Draw buttons
easy_button = pygame.Rect(WIDTH // 2 - 75, 50, 150, 50)
medium_button = pygame.Rect(WIDTH // 2 - 75, 125, 150, 50)
hard_button = pygame.Rect(WIDTH // 2 - 75, 200, 150, 50)
pygame.draw.rect(screen, BUTTON_COLOR, easy_button)
pygame.draw.rect(screen, BUTTON_COLOR, medium_button)
pygame.draw.rect(screen, BUTTON_COLOR, hard_button)
# Draw button text
draw_text('Easy', font, TEXT_COLOR, screen, easy_button.center)
draw_text('Medium', font, TEXT_COLOR, screen, medium_button.center)
draw_text('Hard', font, TEXT_COLOR, screen, hard_button.center)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if easy_button.collidepoint(event.pos):
difficulty = 'Easy'
running = False
elif medium_button.collidepoint(event.pos):
difficulty = 'Medium'
running = False
elif hard_button.collidepoint(event.pos):
difficulty = 'Hard'
running = False
pygame.display.update()
# Clear the screen after choosing the difficulty
screen.fill(BG_COLOR)
# Draw text centered on a position
def draw_text(text, font, color, surface, position):
text_obj = font.render(text, True, color)
text_rect = text_obj.get_rect(center=position)
surface.blit(text_obj, text_rect)
# Display a popup message indicating the result
def show_result_message(message):
screen.fill(BG_COLOR)
draw_text(message, font, TEXT_COLOR, screen, (WIDTH // 2, HEIGHT // 2))
pygame.display.update()
pygame.time.wait(2000) # Wait for 2 seconds
# Main game loop
def main():
global player_turn
select_difficulty() # Show the difficulty selection menu
draw_lines() # Draw the grid after clearing the screen
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Handle player click
if event.type == pygame.MOUSEBUTTONDOWN:
if player_turn == 'X':
mouseX, mouseY = event.pos
clicked_row = mouseY // SQUARE_SIZE
clicked_col = mouseX // SQUARE_SIZE
if board[clicked_row][clicked_col] == ' ':
board[clicked_row][clicked_col] = 'X'
player_turn = 'O'
# Redraw the board after the player's move
screen.fill(BG_COLOR)
draw_lines()
draw_figures()
if check_winner('X'):
show_result_message("Player wins!")
pygame.quit()
sys.exit()
if is_draw():
show_result_message("It's a draw!")
pygame.quit()
sys.exit()
# AI move
ai_move()
player_turn = 'X'
# Redraw the board after the AI's move
screen.fill(BG_COLOR)
draw_lines()
draw_figures()
if check_winner('O'):
show_result_message("AI wins!")
pygame.quit()
sys.exit()
if is_draw():
show_result_message("It's a draw!")
pygame.quit()
sys.exit()
pygame.display.update()
if __name__ == "__main__":
main()