Game AI + Search Algorithms + Python

Chessmate

A chess application with human and AI modes. The engine uses minimax search to evaluate positions and alpha-beta pruning to play smarter, faster. Supports en passant, pawn promotion, move undo, and an AI hint system. Built entirely in Python with CMU Graphics.

Scroll

Two modes, one board

Chessmate home screen
Home ScreenPlay Human or Play AI, with instructions and available shortcuts
Chessmate gameplay
Live GameplayBoard state with status panel, game options, check detection

Minimax for the brain, alpha-beta pruning to make it smarter

How the AI thinks

The AI opponent uses the minimax algorithm to search through possible future board states. It builds a game tree where each node is a board position, alternating between the maximizing player (white, trying to increase the score) and the minimizing player (black, trying to decrease it). The search evaluates every branch to a configurable depth and picks the move that leads to the best guaranteed outcome.

Alpha-beta pruning makes this practical. It tracks two values as it searches: alpha (the best score white can guarantee so far) and beta (the best score black can guarantee). When beta drops below alpha, the remaining branches in that subtree cannot produce a better result, so the algorithm skips them entirely. This cuts the effective search space dramatically, allowing the AI to think deeper in the same time.

The board evaluation function scores positions using piece material values (pawn = 1, knight = 3, bishop = 3.25, rook = 5, queen = 9), positional bonuses for advanced pawns, mobility scoring based on legal move count, pawn structure analysis, and king safety heuristics.

minimax with alpha-beta pruning
def minimax(app, board, depth,
           isMaximizingPlayer, lastMove,
           alpha=float('-inf'),
           beta=float('inf')):

    if depth == 0 or checkGameOver(app):
        return evaluateBoard(board), None

    bestMove = None

    if isMaximizingPlayer:
        maxEval = float('-inf')
        for move in generateAllMoves(
                board, 'white', lastMove):
            newBoard, newLastMove = \
                applyMove(board, move)
            evaluation, _ = minimax(
                app, newBoard, depth - 1,
                False, newLastMove, alpha, beta)
            if evaluation > maxEval:
                maxEval = evaluation
                bestMove = move
            alpha = max(alpha, evaluation)
            if beta <= alpha:
                break  # prune
        return maxEval, bestMove

    else:
        minEval = float('inf')
        for move in generateAllMoves(
                board, 'black', lastMove):
            newBoard, newLastMove = \
                applyMove(board, move)
            evaluation, _ = minimax(
                app, newBoard, depth - 1,
                True, newLastMove, alpha, beta)
            if evaluation < minEval:
                minEval = evaluation
                bestMove = move
            beta = min(beta, evaluation)
            if beta <= alpha:
                break  # prune
        return minEval, bestMove

What went into building this

Python Object-Oriented Programming Minimax Algorithm Alpha-Beta Pruning Recursion & Backtracking Game Tree Search Heuristic Evaluation Event-Driven Programming CMU Graphics Library State Management UI/UX Design Git Version Control

Watch it in action

Full walkthrough covering both game modes, AI difficulty selection, legal move highlighting, en passant, pawn promotion, the undo system, and AI hints.

Keyboard shortcuts

E

En Passant

Toggle the en passant rule on or off mid-game

P

Pawn Promotion

Toggle pawn promotion on or off

U / ←

Undo Move

Take back the last move

H

Hint

AI suggests the optimal move (3 per game)

Click

Legal Moves

Click any piece to see valid destinations highlighted

Home

Mode Select

Switch between Human vs. Human and Human vs. AI

Watch Full Demo on YouTube