Adversarial Search Project

Connect 4 AI Report

Brandon Temple | NetID: bt1142 | April 28, 2026

I built a local browser game that demonstrates depth-limited minimax, alpha-beta pruning, heuristic evaluation, adjustable difficulty, local two-player play, adjustable drop speed, Autopilot, experiments, and headless testing.

Game 7 x 6 Connect 4
Search Minimax
Optimization Alpha-Beta
Max UI Depth 8 Ply

What I Built

I built a local browser-based Connect 4 game with a standard 7-column by 6-row board. In the default Player vs AI mode, red is the human side and yellow is the AI side. A Game Mode selector can switch to local Two Player mode, where both red and yellow are manually controlled from the same board and number buttons. The app validates legal moves, prevents drops into full columns, applies gravity to the lowest open row, detects horizontal, vertical, and both diagonal wins, detects natural draws, and resets the game. It also includes red Autopilot with its own difficulty setting, adjustable live drop speed, post-game replay controls with optional drop animations, an Experiment Lab, downloadable experiment results, and a Node headless test runner.

Project Features

Static site organized into game, style, and validation folders
Game Mode selector for Player vs AI or local Two Player play
7 x 6 board with gravity drops, legal move checks, reset, wins, and draws
Adjustable live drop speed from 0.5x to 2.0x
Depth-limited minimax with yellow maximizing and red minimizing
Alpha-beta pruning with move, node, runtime, algorithm, and depth stats
Heuristic scoring for wins, threats, open spaces, and center control
Easy, Medium, Hard, Expert, and Good Luck settings for both AI sides
Replay mode with move-by-move scrubbing, animated drops, and a no-animation toggle
Optional browser-generated sound effects for drops, wins, and draws
Experiment Lab with JSON, CSV, and text evidence exports
Headless Node tests for game rules, search behavior, and AI matchups

How to Run

The game runs by opening index.html; no server or internet connection is required. Source files are grouped into game/ for gameplay and search, style/ for the visual design, and checks/ for optional Node validation. The Controls panel selects Player vs AI or Two Player mode, difficulty, alpha-beta pruning, sound, and drop speed. The main validation command is node checks/headless-test.js.

Minimax

Minimax searches future game states as a tree. Yellow is the maximizing player because it tries to choose the highest score. Red is the minimizing player because it tries to reduce yellow's outcome. Terminal scores use the required base values, then add a small depth adjustment: yellow wins score +100000 + remaining depth, red wins score -100000 - remaining depth, and draws score 0. This makes the AI prefer quicker wins and delay unavoidable losses.

Alpha-Beta

Alpha-beta pruning keeps the same minimax result while avoiding branches that cannot affect the final move. Alpha tracks the best score available to the maximizing player; beta tracks the best score available to the minimizing player. When alpha is at least beta, the rest of that branch cannot change the chosen move.

Search Details

State A board position, current player, depth remaining, alpha, and beta.
Successors Each legal column creates one child board after gravity places the piece.
Backup Rule Yellow keeps the highest child value; red keeps the lowest child value.
Cutoff Stop at wins, losses, draws, depth zero, or when alpha reaches beta.

With up to seven legal columns, plain minimax grows near O(7^d) before the board starts filling. Alpha-beta has the same final choice as minimax, but good move ordering lets it skip many branches. I order moves from the center outward because center columns are usually stronger in Connect 4 and also improve pruning.

Heuristic Evaluation

Yellow four in a row+100000 base
Red four in a row-100000 base
Yellow three plus open space+100
Red three plus open space-120
Yellow two plus open spaces+10
Red two plus open spaces-15
Center-column controlBonus

Center control matters because center pieces participate in more possible winning lines. This static evaluation estimates position quality when the depth limit is reached.

Experiment Results

These measurements were taken from the empty starting board. Times vary by browser and computer, but the main result is stable: alpha-beta finds the same move while searching fewer nodes.

Algorithm Depth Measured Nodes Measured Time Notes
Minimax 3 400 0.0054 s Baseline, selected column 4
Alpha-Beta 3 82 0.0006 s Same move, fewer nodes
Minimax 4 2,801 0.0125 s Much slower as depth grows
Alpha-Beta 4 234 0.0006 s More practical
Alpha-Beta 5 1,268 0.0034 s Stronger AI at acceptable runtime

Testing and Validation

I tested the project with the browser game and the headless Node runner. The automated checks verify legal moves, invalid-column rejection, gravity stacking, full-column rejection, center-first move ordering, horizontal wins, vertical wins, diagonal wins, full-board draw detection, immediate threat detection, red's minimizing choices, depth-adjusted terminal scoring, plain minimax versus alpha-beta consistency, equal-score move variation, and uneven-depth AI matchups such as red depth 2 against yellow depth 6. The CLI can also save experiment results as JSON, CSV, or a text summary using --format and --out.

Testing Commands

node checks/headless-test.js node game/connect4-game-controller.js --headless --red-depth=2 --yellow-depth=6 --games=6 --format=summary node game/connect4-game-controller.js --experiment --board=midgame --max-depth=6 --format=csv --out=experiment.csv

Sources and Libraries

The main algorithm sources were the CS 4633/6633 lecture notes. The implementation is custom vanilla JavaScript; I did not use a game engine or a Connect 4/minimax library. Browser documentation was used only for standard HTML/CSS/DOM behavior, browser-generated sound, and the optional Node.js headless runner.

Lecture 16: Search algorithms for two-player gamesAI16AdversarialSearch.pdf; minimax, MAX/MIN players, alpha-beta pruning, depth-limited search, and static evaluation.
Lecture 13: Introduction to state-space searchAI13StateSpaceSearch.pdf; states, legal actions, successor generation, and terminal-state checks.
Lecture 18: Game tree search continuedAI18MonteCarlo.pdf; game-tree search context, evaluation functions, and comparison to Monte Carlo Tree Search.
Lecture 1: Course introduction and overviewAI01Intro.pdf; framing the project as classical problem-solving search.
MDN: Document Object ModelBrowser page structure and event-driven interaction. MDN: CSSLayout, responsive styling, and visual presentation. MDN: Web Audio APIBrowser-generated sound effects. Node.js DocumentationHeadless validation runner and command-line test output.

What I Learned

Increasing search depth usually improves decision quality because the AI sees more future threats, but it also grows the game tree quickly. Alpha-beta pruning makes deeper search practical by removing branches that cannot affect the minimax choice. The heuristic matters because it guides the AI whenever the depth limit stops before a win, loss, or draw. Good Luck uses depth 8 as the strongest practical local-browser setting in this version. It is not a solved Connect 4 oracle, but it searches two plies deeper than Expert while staying responsive with alpha-beta pruning.