Connect 4

Play the classic game of Connect 4, also known as Four in a Line and the Captain's Mistress. The goal is to be the first to make four in a row of your color by dropping pieces into one of seven columns. Of course, your opponent is trying to do the same.

In this version, red always goes first, but red is assigned to Player 1 or Player 2 randomly. This implementation allows two human players to play or a human to play against an artificial intelligence with selectable difficulty.

Human vs. Human
Human vs. Computer


The computer AI player uses the negamax variant of the minimax search algorithm to decide on its moves. The difficulty levels you can choose from correspond to the number of moves ahead the AI will search. A difficulty of 1 will always choose a winning move if there is one. A difficulty of 2 will always block your winning move if there is one (although it may not block any winning moves if there are more than one). A difficulty of 9 looks nine moves ahead and offers a challenging opponent.

Note that the computation needed to look ahead grows exponentially, even with alpha-beta pruning. The AI runs in JavaScript on your computer in your browser. Some combinations of computers and browsers may play slowly or even crash at high difficulty levels. Mozilla Firefox seems particularly prone to problems.

At low levels of difficulty, the algorithm cannot see far into the future. If it thinks that several different moves are equally valuable, it will select one of them at random. To make it a little smarter at low levels of play, it prefers getting three in row on a move to getting two in a row and getting two in a row to nothing.