18
18
LINE_COLOR = (23 , 145 , 135 )
19
19
CIRCLE_COLOR = (239 , 231 , 200 )
20
20
CROSS_COLOR = (66 , 66 , 66 )
21
+ TEXT_COLOR = (255 , 255 , 255 )
21
22
22
23
screen = pygame .display .set_mode ((WIDTH , HEIGHT ))
23
24
pygame .display .set_caption ('Tic Tac Toe' )
24
25
screen .fill (BG_COLOR )
25
26
27
+ font = pygame .font .Font (None , 40 ) # Font for displaying messages
26
28
board = np .zeros ((BOARD_ROWS , BOARD_COLS ))
27
29
30
+ difficulty = "medium" # AI difficulty level ("easy", "medium", "hard")
31
+
28
32
def draw_lines ():
29
33
for row in range (1 , BOARD_ROWS ):
30
34
pygame .draw .line (screen , LINE_COLOR , (0 , row * SQUARE_SIZE ), (WIDTH , row * SQUARE_SIZE ), LINE_WIDTH )
@@ -48,19 +52,16 @@ def draw_figures():
48
52
(col * SQUARE_SIZE + SQUARE_SIZE - SPACE , row * SQUARE_SIZE + SQUARE_SIZE - SPACE ),
49
53
CROSS_WIDTH )
50
54
51
-
52
55
def mark_square (row , col , player ):
53
56
board [row ][col ] = player
54
57
55
-
56
58
def available_square (row , col ):
57
59
return board [row ][col ] == 0
58
60
59
61
def is_board_full ():
60
62
return np .all (board != 0 )
61
63
62
64
def check_win (player ):
63
-
64
65
for row in range (BOARD_ROWS ):
65
66
if np .all (board [row , :] == player ):
66
67
return True
@@ -73,8 +74,14 @@ def check_win(player):
73
74
return True
74
75
return False
75
76
77
+ def display_message (message ):
78
+ text = font .render (message , True , TEXT_COLOR )
79
+ text_rect = text .get_rect (center = (WIDTH // 2 , HEIGHT // 2 ))
80
+ screen .blit (text , text_rect )
81
+ pygame .display .update ()
82
+ pygame .time .wait (2000 ) # Wait for 2 seconds to display the message
76
83
77
- # Minimax algorithm
84
+ # Minimax algorithm with difficulty levels
78
85
def minimax (board , depth , is_maximizing ):
79
86
if check_win (2 ): # AI win
80
87
return 1
@@ -104,8 +111,7 @@ def minimax(board, depth, is_maximizing):
104
111
best_score = min (score , best_score )
105
112
return best_score
106
113
107
-
108
- # AI Move
114
+ # AI Move based on difficulty level
109
115
def ai_move ():
110
116
best_score = - np .inf
111
117
move = None
@@ -118,16 +124,23 @@ def ai_move():
118
124
if score > best_score :
119
125
best_score = score
120
126
move = (row , col )
127
+
121
128
if move :
122
129
mark_square (move [0 ], move [1 ], 2 )
123
130
131
+ # Easy AI move: choose a random available square
132
+ def easy_ai_move ():
133
+ available_moves = [(row , col ) for row in range (BOARD_ROWS ) for col in range (BOARD_COLS ) if available_square (row , col )]
134
+ if available_moves :
135
+ move = available_moves [np .random .randint (len (available_moves ))]
136
+ mark_square (move [0 ], move [1 ], 2 )
137
+
124
138
def restart ():
125
139
screen .fill (BG_COLOR )
126
140
draw_lines ()
127
141
global board
128
142
board = np .zeros ((BOARD_ROWS , BOARD_COLS ))
129
143
130
-
131
144
player = 1 # Player 1 is human
132
145
game_over = False
133
146
@@ -149,12 +162,18 @@ def restart():
149
162
if available_square (clicked_row , clicked_col ):
150
163
mark_square (clicked_row , clicked_col , player )
151
164
if check_win (player ):
165
+ display_message ("Player Wins!" )
152
166
game_over = True
153
167
player = 2
154
168
155
169
if player == 2 and not game_over :
156
- ai_move ()
170
+ if difficulty == "easy" :
171
+ easy_ai_move ()
172
+ else :
173
+ ai_move ()
174
+
157
175
if check_win (2 ):
176
+ display_message ("AI Wins!" )
158
177
game_over = True
159
178
player = 1
160
179
@@ -164,5 +183,9 @@ def restart():
164
183
game_over = False
165
184
player = 1
166
185
186
+ if game_over and is_board_full ():
187
+ display_message ("It's a Draw!" )
188
+ game_over = True
189
+
167
190
draw_figures ()
168
191
pygame .display .update ()
0 commit comments