Can a computer program play tic-tac-toe without ever losing? You write a script in MATLAB that first detects wins, ties, and losses on the game board. Then you build a random player that places marks in random spots.
Next you program a "smart player" using roughly 300 if-then statements. Each statement counters a move that could lead to a loss. The smart player always goes second.
Against the random player the program won 80 percent of 10,000 games and tied the rest. Against three human players it never lost. Humans tied 86 percent of their games and lost 14 percent.
Hypothesis
The hypothesis is that it is possible to create a computer game that never loses, even when it goes second.
When you encode decision rules precisely, a machine follows them perfectly every time without forgetting or hesitating. Here, roughly 300 if-then statements define a smart player — each statement countering a move that could lead to a loss. Tested against three human players, the program never lost, showing that a complete set of rules can produce unbeatable play.
An algorithm is a set of step-by-step rules a computer follows to solve a problem. In this experiment, roughly 300 if-then statements form the core logic of a smart player written in MATLAB. Each statement counters a specific move that could lead to a loss — so when the random player places a mark, the smart player already has a response. Against 10,000 random-player games, the smart player won 80 percent and tied the rest. It never lost.
Method & Materials
You will create a script that detects ties, wins, and losses. You will create a random player and have the two random players play against each other. You will then analyze strategies and program counter-attacks against moves that would result in a loss. You will then test the efficiency of the "smart player" against random players and humans.
You will need a laptop, the program MATLAB, and a basic understanding of coding.
MEL Math — hands-on math experiment kits delivered monthly — makes abstract concepts tangible. (Affiliate link)
The results of this project showed that the computer program was successful in that it was able to never lose, as seen when it played three human players. The computer program only tied 19.97% of the games against the random player, and the rest of the games the computer program won. The three human subjects, played against the computer program and never won, tied 86% of the games and lost 14%.
Why do this project?
This science project is unique because it shows how to create a computer program that can never lose a game of tic-tac-toe.
Also Consider
Experiment variations to consider include testing the computer program against more human players and testing the computer program against different strategies.
Full project details
Additional information and source material for this project are available below.