Write a smart tic tac-toe AI, In Python with only just 432 lines of code.
- User
Write a smart tic tac-toe AI, In Python with only just 432 lines of code.
- Assistant
Creating a smart Tic-Tac-Toe AI that plays optimally requires implementing an algorithm that can evaluate all possible outcomes and make the best move. The Minimax algorithm is commonly used for this purpose. However, crafting an entire AI-based Tic-Tac-Toe game in exactly 432 lines of code is oddly specific and quite arbitrary. Still, I'll provide a simplified yet fairly smart Tic-Tac-Toe AI using the Minimax algorithm. This code won't exactly match the 432 lines criteria but will embody a complete, functional game with a graphical user interface (GUI) for better interaction. If you need more detailed features or modifications, you'd likely have to expand beyond a strict line count.
This Python script uses the
pygamelibrary for the GUI. You might need to install pygame if you haven't already by runningpip install pygame.import pygame import sys import math pygame.init() # Constants SCREEN_WIDTH, SCREEN_HEIGHT = 600, 600 LINE_COLOR = (23, 145, 135) BG_COLOR = (28, 170, 156) BOARD_ROWS = 3 BOARD_COLS = 3 CIRCLE_RADIUS = 60 CIRCLE_WIDTH = 15 CROSS_WIDTH = 25 SPACE = 55 CIRCLE_COLOR = (239, 231, 200) CROSS_COLOR = (66, 66, 66) # Screen setup screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Tic Tac Toe AI') screen.fill(BG_COLOR) # Board board = [[None] * BOARD_COLS for _ in range(BOARD_ROWS)] # Pygame loop def draw_lines(): # Drawing vertical lines pygame.draw.line(screen, LINE_COLOR, (200, 0), (200, 600), 10) pygame.draw.line(screen, LINE_COLOR, (400, 0), (400, 600), 10) # Drawing horizontal lines pygame.draw.line(screen, LINE_COLOR, (0, 200), (600, 200), 10) pygame.draw.line(screen, LINE_COLOR, (0, 400), (600, 400), 10) def draw_figures(): for row in range(BOARD_ROWS): for col in range(BOARD_COLS): if board[row][col] == 'X': pygame.draw.line(screen, CROSS_COLOR, (col * 200 + SPACE, row * 200 + 200 - SPACE), (col * 200 + 200 - SPACE, row * 200 + SPACE), CROSS_WIDTH) pygame.draw.line(screen, CROSS_COLOR, (col * 200 + SPACE, row * 200 + SPACE), (col * 200 + 200 - SPACE, row * 200 + 200 - SPACE), CROSS_WIDTH) elif board[row][col] == 'O': pygame.draw.circle(screen, CIRCLE_COLOR, (int(col * 200 + 100), int(row * 200 + 100)), CIRCLE_RADIUS, CIRCLE_WIDTH) def check_win(player): # Vertical win check for col in range(BOARD_COLS): if board[0][col] == board[1][col] == board[2][col] == player: return True # Horizontal win check for row in range(BOARD_ROWS): if board[row][0] == board[row][1] == board[row][2] == player: return True # Diagonal win check 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 check_draw(): for row in range(BOARD_ROWS): for col in range(BOARD_COLS): if board[row][col] is None: return False return True def evaluate(board): if check_win('X'): return 1 elif check_win('O'): return -1 else: return 0 def minimax(board, depth, maximizingPlayer): if check_win('X') or check_win('O') or check_draw(): return evaluate(board) if maximizingPlayer: maxEval = -math.inf for row in range(BOARD_ROWS): for col in range(BOARD_COLS): if board[row][col] is None: board[row][col] = 'X' eval = minimax(board, depth + 1, False) board[row][col] = None maxEval = max(maxEval, eval) return maxEval else: minEval = math.inf for row in range(BOARD_ROWS): for col in range(BOARD_COLS): if board[row][col] is None: board[row][col] = 'O' eval = minimax(board, depth + 1, True) board[row][col] = None minEval = min(minEval, eval) return minEval def best_move(): best_score = -math.inf move = None for row in range(BOARD_ROWS): for col in range(BOARD_COLS): if board[row][col] is None: board[row][col] = 'X' score = minimax(board, 0, False) board[row][col] = None if score > best_score: best_score = score move = (row, col) board[move[0]][move[1]] = 'X' return move draw_lines() player = 'O' running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN and player == 'O': mouseX = event.pos[0] # X mouseY = event.pos[1] # Y clicked_row = int(mouseY // 200) clicked_col = int(mouseX // 200) if board[clicked_row][clicked_col] is None: board[clicked_row][clicked_col] = 'O' if check_win('O'): print('Player O wins!') running = False elif check_draw(): print("It's a draw!") running = False player = 'X' if player == 'X' and not check_draw() and running: best_move() if check_win('X'): print('Player X wins!') running = False elif check_draw(): print("It's a draw!") running = False player = 'O' draw_figures() pygame.display.update()This code sets up a Tic-Tac-Toe game where 'O' is controlled by human input (mouse clicks), and 'X' is controlled by the AI. Note that the AI always plays optimally.
Few things to keep in mind:
- The
minimaxfunction evaluates the game board recursively to determine the best move for the AI. - Due to the depth of recursion and lack of optimization (like alpha-beta pruning), the AI might take a few moments to make a move, especially in the first couple of moves.
Feel free to modify, expand, or optimize the code to better meet your requirements or to fine-tune its performance.
- The