1818LINE_COLOR = (23 , 145 , 135 )
1919CIRCLE_COLOR = (239 , 231 , 200 )
2020CROSS_COLOR = (66 , 66 , 66 )
21+ TEXT_COLOR = (255 , 255 , 255 )
2122
2223screen = pygame .display .set_mode ((WIDTH , HEIGHT ))
2324pygame .display .set_caption ('Tic Tac Toe' )
2425screen .fill (BG_COLOR )
2526
27+ font = pygame .font .Font (None , 40 ) # Font for displaying messages
2628board = np .zeros ((BOARD_ROWS , BOARD_COLS ))
2729
30+ difficulty = "medium" # AI difficulty level ("easy", "medium", "hard")
31+
2832def draw_lines ():
2933 for row in range (1 , BOARD_ROWS ):
3034 pygame .draw .line (screen , LINE_COLOR , (0 , row * SQUARE_SIZE ), (WIDTH , row * SQUARE_SIZE ), LINE_WIDTH )
@@ -48,19 +52,16 @@ def draw_figures():
4852 (col * SQUARE_SIZE + SQUARE_SIZE - SPACE , row * SQUARE_SIZE + SQUARE_SIZE - SPACE ),
4953 CROSS_WIDTH )
5054
51-
5255def mark_square (row , col , player ):
5356 board [row ][col ] = player
5457
55-
5658def available_square (row , col ):
5759 return board [row ][col ] == 0
5860
5961def is_board_full ():
6062 return np .all (board != 0 )
6163
6264def check_win (player ):
63-
6465 for row in range (BOARD_ROWS ):
6566 if np .all (board [row , :] == player ):
6667 return True
@@ -73,8 +74,14 @@ def check_win(player):
7374 return True
7475 return False
7576
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
7683
77- # Minimax algorithm
84+ # Minimax algorithm with difficulty levels
7885def minimax (board , depth , is_maximizing ):
7986 if check_win (2 ): # AI win
8087 return 1
@@ -104,8 +111,7 @@ def minimax(board, depth, is_maximizing):
104111 best_score = min (score , best_score )
105112 return best_score
106113
107-
108- # AI Move
114+ # AI Move based on difficulty level
109115def ai_move ():
110116 best_score = - np .inf
111117 move = None
@@ -118,16 +124,23 @@ def ai_move():
118124 if score > best_score :
119125 best_score = score
120126 move = (row , col )
127+
121128 if move :
122129 mark_square (move [0 ], move [1 ], 2 )
123130
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+
124138def restart ():
125139 screen .fill (BG_COLOR )
126140 draw_lines ()
127141 global board
128142 board = np .zeros ((BOARD_ROWS , BOARD_COLS ))
129143
130-
131144player = 1 # Player 1 is human
132145game_over = False
133146
@@ -149,12 +162,18 @@ def restart():
149162 if available_square (clicked_row , clicked_col ):
150163 mark_square (clicked_row , clicked_col , player )
151164 if check_win (player ):
165+ display_message ("Player Wins!" )
152166 game_over = True
153167 player = 2
154168
155169 if player == 2 and not game_over :
156- ai_move ()
170+ if difficulty == "easy" :
171+ easy_ai_move ()
172+ else :
173+ ai_move ()
174+
157175 if check_win (2 ):
176+ display_message ("AI Wins!" )
158177 game_over = True
159178 player = 1
160179
@@ -164,5 +183,9 @@ def restart():
164183 game_over = False
165184 player = 1
166185
186+ if game_over and is_board_full ():
187+ display_message ("It's a Draw!" )
188+ game_over = True
189+
167190 draw_figures ()
168191 pygame .display .update ()
0 commit comments