Connect Four (Agent vs. Human)
Play Connect Four against an agent that evolves a fresh set of board-evaluation weights with a genetic algorithm before every move it makes.
Go to the live page Open the Connect Four demoThe Agent vs. Human demo is a game of Connect Four between you and an agent. The board is seven columns wide and six rows tall, and the first player to line up four pieces across, down or diagonally wins. Yellow is player 1 and red is player 2. On every one of its turns, the agent runs a short genetic algorithm to decide where to play, and it shows you its reasoning.
How the Agent Thinks¶
The agent judges a board with six hand-picked features: how many of its pieces sit in the centre column, how many open lines of two and three it has, how many open lines of two and three you have, and how many of your three-in-a-row lines it has already blocked. Each feature gets a weight, and a board's score is the weighted sum.
The genetic algorithm evolves those six weights. Each candidate weight set looks at every legal move, scores the board each move would produce, and is rated by how good its favourite move is. When time or generations run out, the best weight set picks the move.
Two checks sit around that search. If the agent can win right now, it plays the winning move without evolving anything. And any move that would let you win on your very next turn is scored as a loss, so the agent avoids handing you an immediate win whenever it has a choice.
How a Game Looks¶
Click any empty cell in a column to drop your piece there. Your piece appears straight away, then the agent thinks, usually for a couple of seconds on the default settings, and drops its own. The Agent's Reasoning panel shows its top three candidate columns with their scores, the evolved weight for each feature, and how long the search took. A winning move is marked as winning and a move that loses on the spot is marked as losing. When someone connects four or the board fills up, a dialog announces the result.
Use Human first or Agent first to choose who opens, then New game for a fresh board.
Settings to Try¶
Open Settings and pick a difficulty preset, or set the numbers yourself. Apply & Reset starts a new game with the new settings.
- Easy is fast and weak: a small population, few generations and half a second per move.
- Medium is the default: a population of 60, 60 generations and up to 2.5 seconds per move.
- Hard is slow and strong: a population of 120, 150 generations and up to 6 seconds per move.
You can also edit each field directly, which switches the preset to Custom: population size (10 to 200), generations (10 to 300), time budget per move (0.5 to 10 seconds), mutation percent, parents mating (never more than the population) and elitism. The current settings are shown in a line under the toolbar.
What the Achievement Rewards¶
Signed-in users unlock the Four in a row achievement the first time they beat the agent.
For more on how the demos catalog works, see What the Demos Do.