-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
187 lines (154 loc) · 5.73 KB
/
tic_tac_toe.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
import pygame
import sys
import time
# Initialize Pygame
pygame.init()
# Game Constants
WIDTH, HEIGHT = 600, 600
LINE_WIDTH = 10
BOARD_ROWS, BOARD_COLS = 3, 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 = (66, 66, 66)
# Initialize screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tic-Tac-Toe AI")
screen.fill(BG_COLOR)
# Board
board = [[0 for _ in range(BOARD_COLS)] for _ in range(BOARD_ROWS)]
# Players
PLAYER = 1 # Human
AI = 2
def draw_lines():
"""Draw the board 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():
"""Draw X and O on the board."""
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == PLAYER:
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)
pygame.draw.line(screen, CROSS_COLOR,
(col * SQUARE_SIZE + SQUARE_SIZE - SPACE, row * SQUARE_SIZE + SPACE),
(col * SQUARE_SIZE + SPACE, row * SQUARE_SIZE + SQUARE_SIZE - SPACE),
CROSS_WIDTH)
elif board[row][col] == AI:
pygame.draw.circle(screen, CIRCLE_COLOR,
(int(col * SQUARE_SIZE + SQUARE_SIZE // 2), int(row * SQUARE_SIZE + SQUARE_SIZE // 2)),
CIRCLE_RADIUS, CIRCLE_WIDTH)
def mark_square(row, col, player):
"""Mark the square on the board."""
board[row][col] = player
def available_square(row, col):
"""Check if a square is available."""
return board[row][col] == 0
def is_full():
"""Check if the board is full."""
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if board[row][col] == 0:
return False
return True
def check_win(player):
"""Check if the given player has won."""
# Vertical win
for col in range(BOARD_COLS):
if board[0][col] == board[1][col] == board[2][col] == player:
return True
# Horizontal win
for row in range(BOARD_ROWS):
if board[row][0] == board[row][1] == board[row][2] == player:
return True
# Diagonal win
if board[0][0] == board[1][1] == board[2][2] == player or board[0][2] == board[1][1] == board[2][0] == player:
return True
return False
def minimax(board, depth, is_maximizing):
"""Minimax algorithm to determine the best move for AI."""
if check_win(AI):
return 1
if check_win(PLAYER):
return -1
if is_full():
return 0
if is_maximizing:
best_score = -float('inf')
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if available_square(row, col):
board[row][col] = AI
score = minimax(board, depth + 1, False)
board[row][col] = 0
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 available_square(row, col):
board[row][col] = PLAYER
score = minimax(board, depth + 1, True)
board[row][col] = 0
best_score = min(score, best_score)
return best_score
def best_move():
"""Determine the best move for AI using Minimax."""
best_score = -float('inf')
move = (0, 0)
for row in range(BOARD_ROWS):
for col in range(BOARD_COLS):
if available_square(row, col):
board[row][col] = AI
score = minimax(board, 0, False)
board[row][col] = 0
if score > best_score:
best_score = score
move = (row, col)
return move
# Draw initial board lines
draw_lines()
# Main game loop
game_over = False
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 = mouseY // SQUARE_SIZE
clicked_col = mouseX // SQUARE_SIZE
if available_square(clicked_row, clicked_col):
mark_square(clicked_row, clicked_col, PLAYER)
if check_win(PLAYER):
print("Player wins!")
game_over = True
elif is_full():
print("It's a draw!")
game_over = True
else:
ai_row, ai_col = best_move()
mark_square(ai_row, ai_col, AI)
if check_win(AI):
print("AI wins!")
game_over = True
screen.fill(BG_COLOR)
draw_lines()
draw_figures()
pygame.display.update()