import sys import pygame import math pygame.init() WIDTH = 900 HEIGHT = 900 screen =…
- User
import sys import pygame import math
pygame.init()
WIDTH = 900 HEIGHT = 900 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Tic-Tac-Toe")
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255)
FONT = pygame.font.SysFont("Arial", 80) SMALL_FONT = pygame.font.SysFont("Arial", 40)
board = [['', '', ''], ['', '', ''], ['', '', '']] players = ['X', 'O'] current_player = players[0] game_over = False winner = None computer_player = 'O' game_started = False num_players = 0 player_starts = True
def draw_home_screen(): screen.fill(WHITE) two_players_text = FONT.render("Press 1 For Two Players", True, BLACK) computer_text = FONT.render("Press 2 to Play with Computer", True, BLACK) two_players_rect = two_players_text.get_rect(center=(WIDTH/2, HEIGHT/2-50)) computer_rect = computer_text.get_rect(center=(WIDTH/2, HEIGHT/2+50)) screen.blit(two_players_text, two_players_rect) screen.blit(computer_text, computer_rect)
def draw_board(): for x in range(3): for y in range(3): pygame.draw.rect(screen, BLUE, (x300, y300, 300, 300), 2) text = FONT.render(board[y][x], True, RED) text_rect = text.get_rect(center=(x300+150, y300+150)) screen.blit(text, text_rect)
def check_win(): global game_over, winner for row in range(3): if board[row][0] == board[row][1] == board[row][2] != '': game_over = True winner = board[row][0] return for col in range(3): if board[0][col] == board[1][col] == board[2][col] != '': game_over = True winner = board[0][col] return if board[0][0] == board[1][1] == board[2][2] != '': game_over = True winner = board[0][0] return if board[0][2] == board[1][1] == board[2][0] != '': game_over = True winner = board[0][2] return if all([all(row) for row in board]) and not winner: game_over = True winner = 'Tie'
def draw_winner(): if winner == 'Tie': text = FONT.render('Tie!', True, RED) else: text = FONT.render(f'{winner} wins!', True, RED) text_rect = text.get_rect(center=(WIDTH/2, HEIGHT/2)) screen.blit(text, text_rect)
def draw_play_again(): text = SMALL_FONT.render('Press SPACE to play again', True, WHITE) text_rect = text.get_rect(center=(WIDTH/2, HEIGHT-50)) screen.blit(text, text_rect)
def evaluate(board): for row in range(3): if board[row][0] == board[row][1] == board[row][2] != '': return 10 if board[row][0] == computer_player else -10 for col in range(3): if board[0][col] == board[1][col] == board[2][col] != '': return 10 if board[0][col] == computer_player else -10 if board[0][0] == board[1][1] == board[2][2] != '': return 10 if board[0][0] == computer_player else -10 if board[0][2] == board[1][1] == board[2][0] != '': return 10 if board[0][2] == computer_player else -10 return 0
def minimax(board, depth, is_maximizing): score = evaluate(board) if score == 10 or score == -10: return score if all([all(row) for row in board]): return 0 if is_maximizing: best_score = -math.inf for row in range(3): for col in range(3): if board[row][col] == '': board[row][col] = computer_player score = minimax(board, depth + 1, False) board[row][col] = '' best_score = max(best_score, score) return best_score else: best_score = math.inf for row in range(3): for col in range(3): if board[row][col] == '': board[row][col] = 'X' if computer_player == 'O' else 'O' score = minimax(board, depth + 1, True) board[row][col] = '' best_score = min(best_score, score) return best_score
- Assistant
lly starts. To do so, add a line to set
game_startedtoTruewhen either key is pressed.Here's the updated code:
... while True: if not game_started: draw_home_screen() elif not game_over: screen.fill(BLACK) draw_board() if num_players == 1 and current_player == computer_player: play_ai_turn() elif game_over: draw_winner() draw_play_again() pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN and not game_started: if event.key == pygame.K_1: num_players = 2 game_started = True if event.key == pygame.K_2: num_players = 1 game_started = True if event.type == pygame.MOUSEBUTTONDOWN and game_started and not game_over: pygame.time.delay(500) x, y = pygame.mouse.get_pos() col = x // 200 row = y // 200 if board[row][col] == '': board[row][col] = current_player check_win() current_player = players[1] if current_player == players[0] else players[0] if event.type == pygame.KEYDOWN and game_over: if event.key == pygame.K_SPACE: reset() game_started = False