-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic_tac_toe_ai.py
More file actions
190 lines (163 loc) · 6.62 KB
/
tic_tac_toe_ai.py
File metadata and controls
190 lines (163 loc) · 6.62 KB
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
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 300, 300
LINE_WIDTH = 5
BOARD_ROWS = 3
BOARD_COLS = 3
SQUARE_SIZE = WIDTH // BOARD_COLS
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)
# Initialize screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tic-Tac-Toe')
screen.fill(BG_COLOR)
def draw_lines():
# Horizontal 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)
# Vertical lines
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)
def draw_figures(board):
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
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)
draw_lines()
def check_winner(board, player):
# Check rows
for row in range(BOARD_ROWS):
if board[row][0] == board[row][1] == board[row][2] == player:
return True
# Check columns
for col in range(BOARD_COLS):
if board[0][col] == board[1][col] == board[2][col] == player:
return True
# Check diagonals
if board[0][0] == board[1][1] == board[2][2] == player:
return True
if board[2][0] == board[1][1] == board[0][2] == player:
return True
return False
def restart():
screen.fill(BG_COLOR)
draw_lines()
return [[" " for _ in range(BOARD_COLS)] for _ in range(BOARD_ROWS)]
board = restart()
current_player = 'X'
game_over = False
# This code is use to play for 2 players
# while True:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# pygame.quit()
# sys.exit()
# if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
# mouseX = event.pos[0] # X coordinate
# mouseY = event.pos[1] # Y coordinate
# clicked_row = int(mouseY // SQUARE_SIZE)
# clicked_col = int(mouseX // SQUARE_SIZE)
# if board[clicked_row][clicked_col] == " ":
# board[clicked_row][clicked_col] = current_player
# draw_figures(board)
# if check_winner(board, current_player):
# game_over = True
# current_player = 'O' if current_player == 'X' else 'X'
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_r:
# board = restart()
# game_over = False
# current_player = 'X'
# pygame.display.update()
# for this extra function is used to play with ai
def check_draw(board):
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == " ":
return False
return True
def minimax(board, depth, is_maximizing):
if check_winner(board, 'O'):
return 1
if check_winner(board, 'X'):
return -1
if check_draw(board):
return 0
if is_maximizing:
best_score = -float('inf')
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == " ":
board[row][col] = 'O'
score = minimax(board, depth + 1, False)
board[row][col] = " "
best_score = max(score, best_score)
return best_score
else:
best_score = float('inf')
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == " ":
board[row][col] = 'X'
score = minimax(board, depth + 1, True)
board[row][col] = " "
best_score = min(score, best_score)
return best_score
def best_move(board):
best_score = -float('inf')
move = None
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == " ":
board[row][col] = 'O'
score = minimax(board, 0, False)
board[row][col] = " "
if score > best_score:
best_score = score
move = (row, col)
return move
# Modify the game loop to include AI moves
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
mouseX = event.pos[0] # X coordinate
mouseY = event.pos[1] # Y coordinate
clicked_row = int(mouseY // SQUARE_SIZE)
clicked_col = int(mouseX // SQUARE_SIZE)
if board[clicked_row][clicked_col] == " ":
board[clicked_row][clicked_col] = current_player
draw_figures(board)
if check_winner(board, current_player):
game_over = True
current_player = 'O' if current_player == 'X' else 'X'
# AI move
if not game_over and current_player == 'O':
ai_move = best_move(board)
if ai_move:
board[ai_move[0]][ai_move[1]] = 'O'
draw_figures(board)
if check_winner(board, 'O'):
game_over = True
current_player = 'X'
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
board = restart()
game_over = False
current_player = 'X'
pygame.display.update()