# Imports import pygame, time, random # Animate X def animate_X(screen, cell_x, cell_y): # Position cell_x *= 100 cell_y *= 100 # Animation for i in range(35): pygame.draw.line(screen, (255, 100, 100), (cell_x + 50 - i, cell_y + 50 - i), (cell_x + 50 + i, cell_y + 50 + i), 6) pygame.draw.line(screen, (255, 100, 100), (cell_x + 50 - i, cell_y + 50 + i), (cell_x + 50 + i, cell_y + 50 - i), 6) pygame.display.update() time.sleep(0.006) # Animate 0 def animate_0(screen, cell_x, cell_y): # Position cell_x *= 100 cell_y *= 100 # Animation for i in range(39): pygame.draw.circle(screen, (100, 100, 255), (cell_x + 50, cell_y + 50), i) if i > 6: pygame.draw.circle(screen, (255, 255, 200), (cell_x + 50, cell_y + 50), i - 6) pygame.display.update() time.sleep(0.006) # Draw Board def draw_board(screen, board): # Background Color screen.fill((255, 255, 200)) # Grid for i in range(1,3): pygame.draw.line(screen, (0, 0, 0), (i * 100, 0), (i * 100, 300), 6) pygame.draw.line(screen, (0, 0, 0), (0, i * 100), (300, i * 100), 6) # Markers row_count = 0 for y in board: column_count = 0 for x in y: row_count *= 100 column_count *= 100 if x == 1: pygame.draw.line(screen, (255, 100, 100), (column_count + 15, row_count + 15), (column_count + 85, row_count + 85), 6) pygame.draw.line(screen, (255, 100, 100), (column_count + 85, row_count + 15), (column_count + 15, row_count + 85), 6) elif x == -1: pygame.draw.circle(screen, (100, 100, 255), (column_count + 50, row_count + 50), 39) pygame.draw.circle(screen, (255, 255, 200), (column_count + 50, row_count + 50), 33) row_count /= 100 column_count /= 100 column_count += 1 row_count += 1 # Check Winner def check_winner(board): # Winner winner = None # Check Draw found_zero = True for column in board: for row in column: if row == 0: found_zero = False if found_zero: winner = 0 # Check Rows if winner == None or winner == 0: for row in board: row_sum = 0 for column in row: row_sum += column if row_sum == 3: winner = 1 break elif row_sum == -3: winner = -1 break # Check Columns if winner == None or winner == 0: for column in range(3): column_sum = 0 for y in board: column_sum += y[column] if column_sum == 3: winner = 1 break elif column_sum == -3: winner = -1 break # Check Diagonals if winner == None or winner == 0: diagonal_sum = 0 for i in range(3): diagonal_sum += board[i][i] if diagonal_sum == 3: winner = 1 elif diagonal_sum == -3: winner = -1 if winner == None or winner == 0: diagonal_sum = 0 for i in range(3): row = 2 - i column = i diagonal_sum += board[row][column] if diagonal_sum == 3: winner = 1 elif diagonal_sum == -3: winner = -1 return winner # AI Move def ai_move(board): # Move Loop while True: # Block Rows for row_counter, row in enumerate(board): player_one = 0 player_two = 0 empty_space = None for column_counter, column in enumerate(row): if column == 1: player_one += 1 if column == -1: player_two += 1 if column == 0: empty_space = [row_counter, column_counter] if player_one == 2 or player_two == 2: if empty_space != None: return empty_space[0], empty_space[1] # Block Columns for column in range(3): player_one = 0 player_two = 0 empty_space = None for row in range(3): if board[row][column] == 1: player_one += 1 if board[row][column] == -1: player_two += 1 if board[row][column] == 0: empty_space = [row, column] if player_one == 2 or player_one == 2: if empty_space != None: return empty_space[0], empty_space[1] # Block Diagonals player_one = 0 player_two = 0 empty_space = None for i in range(3): if board[i][i] == 1: player_one += 1 if board[i][i] == -1: player_two += 1 if board[i][i] == 0: empty_space = [i, i] if player_one == 2 or player_two == 2: if empty_space != None: return empty_space[0], empty_space[1] player_one = 0 player_two = 0 empty_space = None if board[0][2] == 1: player_one += 1 if board[0][2] == -1: player_two += 1 if board[0][2] == 0: empty_space = [0, 2] if board[1][1] == 1: player_one += 1 if board[1][1] == -1: player_two += 1 if board[1][1] == 0: empty_space = [0, 0] if board[2][0] == 1: player_one += 1 if board[2][0] == -1: player_two += 1 if board[2][0] == 0: empty_space = [2, 0] if player_one == 2 or player_two == 2: if empty_space != None: return empty_space[0], empty_space[1] # Random while True: row = random.randint(0, 2) column = random.randint(0, 2) if board[row][column] == 0: return row, column