Tic-Tac-Toe with AI
An opponent that plays perfectly, using minimax with alpha-beta pruning.
- JavaScript
- Minimax
- HTML
About this project
Tic-tac-toe against an opponent that cannot be beaten. Not by rules like "take the centre" — it plays out every remaining game and picks the move with the best guaranteed outcome. From an empty board that is 255,168 possible games, which a browser explores in a couple of milliseconds.
Most versions of this project use heuristics, which are fiddly and usually beatable. This one is a genuine minimax implementation with alpha-beta pruning, and the README explains both well enough to defend in a viva. Deleting the two pruning lines leaves the AI playing identically but slower, which is the clearest way to feel what pruning actually does.
What you will learn
- Implement minimax: choose the move that maximises your score, assuming the opponent minimises it
- Understand why a depth term makes an AI win promptly and lose slowly
- Add alpha-beta pruning and measure how many positions it removes
- Search with make-and-undo on one board instead of copying at every node
- Model difficulty as how often a perfect player makes a mistake, rather than as a worse evaluation
Before you start
- JavaScript basics — arrays, functions, and what recursion is
- A browser and a text editor. Nothing to install.
New to JavaScript? The free JavaScript course covers everything this project assumes.
Academic integrity notice
These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.
Features
- Full minimax search with alpha-beta pruning
- Depth-weighted scoring, so it wins in the fewest moves and survives the longest when losing
- Make-and-undo on a single board rather than copying at every node
- Three difficulties, modelled as how often a perfect player plays at random
- Winning line highlighted, running scoreboard
- Cells are real buttons, so the whole game is keyboard-operable without extra code
Technology stack
Track: Web Development · Level: Mini project
What is included
- index.html, style.css and script.js — commented throughout
- A README explaining minimax, the depth term and alpha-beta pruning
- Five suggested extensions, including scaling to a 4x4 board
Delivered as a 7 KB ZIP. SHA-256 published under Changelog so you can verify it.
System requirements
- Operating system
- Any
- Runtime
- Any modern browser. No install, no build step.
Licence
Learn from it, change it, and use it in your own coursework and personal projects. Do not resell it or pass it off as your own original work.
- Personal and educational use
- Modifying the code however you like
- Using it as the basis of your own coursework, with honest acknowledgement
- Putting your modified version in a portfolio or on your own GitHub
- Reselling or redistributing the project, modified or not
- Presenting it as your own original work
- Commercial deployment — see the Commercial licence
Support window: 6 months from download.
Frequently asked
Can the AI really not be beaten?
Not on Unbeatable, no. Tic-tac-toe is a solved game and perfect play by both sides is always a draw. The best you can achieve is a draw, and the AI will punish any mistake. Easy and Medium play at random some of the time, so those are winnable.
What is alpha-beta pruning actually doing?
If one reply already proves a move is worse than something the AI can guarantee elsewhere, the remaining replies cannot change the decision, so the search stops looking at them. It typically explores under a tenth as many positions with an identical result. Delete the two break statements and the AI plays exactly the same, just slower.
Is this good enough for a college mini project?
It is the right scope for one, and minimax is a genuinely examinable topic. Understand the depth term and the pruning well enough to explain them, extend it — a 4x4 board is the natural next step — and write your own report.
Changelog
- First release
SHA-256cb4f410bd044e2c77557ebe6c726bb61094d7294e74fcb7c5b34f7aef9d8093e