Play Four in a Row against a computer at three levels, or against a friend on the same screen. Drop discs, line up four, keep a session tally.
Turn Your turn (violet dot)
Discs 0
This session -
Win streak 0
Best streak -
Tap a column button or the board to drop a disc. On a keyboard, left and right arrows move between columns, Enter or Space drops a disc, and the keys 1 to 7 drop straight into a column. Undo takes back your last move and the reply to it; a game that used Undo is not counted.
The computer searches the moves ahead with negamax and alpha-beta pruning, trying the centre columns first. When it cannot see the end of the game, it scores each of the 69 possible lines of four: open threes and open twos are worth points for the side that holds them, and the centre column gets a small bonus. At every level it takes a win when it has one and blocks yours when you are one move from winning. Easy looks one move ahead and plays a random column about one move in four. Medium looks four moves ahead and Hard looks seven. Equal moves are chosen at random, so games differ.
As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.
Take turns dropping a disc into one of seven columns. The disc falls to the lowest free space. The first player to line up four of their discs wins, in a row, a column or a diagonal. If the board fills with no four, it is a draw.
Pick an Opponent: the computer, or Two players on the same screen. Against the computer, choose a Difficulty and whether you go first (violet discs with a dot) or go second (amber discs with a ring). The first player is always violet. Changing any of these starts a new game.
Each disc carries a mark as well as a colour, a solid dot for the first player and a ring for the second, so the two sides can be told apart without relying on colour. The board is a flat, rounded tray in a soft violet-tinted dark neutral, and the discs are violet and amber with a light shading. The winning four is outlined with a solid frame and glows briefly (not at all if your device asks for reduced motion), and the result is also written out in words below the board.
The board is 42 cells, and a move is a column. Everything that decides a game lives in a small pure module: dropping a disc, checking the 69 possible lines of four (24 across, 21 up and 24 diagonal), and refusing any move once someone has won.
The computer uses negamax, the compact form of minimax, with alpha-beta pruning, which skips branches that cannot change the answer. It tries the centre columns first, since they sit in the most lines, so good moves come early and more of the tree is pruned. Instead of re-reading the whole board at every step, the search keeps a running count of each side’s discs in every line of four, and updates only the lines through the cell that just changed.
When the search stops looking ahead, it scores the position. For each line that only one side can still use, two discs are worth a few points and three discs more, and discs in the centre column earn a small bonus. A win found sooner scores higher than one found later.
| Level | Looks ahead | Notes |
|---|---|---|
| Easy | 1 move | Plays a random column about one move in four |
| Medium | 4 moves | Never plays at random |
| Hard | 7 moves | Never plays at random |
On Medium and Hard the computer first checks for a move that wins on the spot, then for a move it must make to stop you winning on the spot. Easy does the same except that about one move in four it plays a random move instead. Among equally good moves it chooses at random, so the same position does not always get the same reply.
The search runs on the page itself, after a short pause that lets “The computer is thinking” appear first. No background thread is used. The automated test only requires a Hard move to finish in well under five seconds. The test suite also checks:
Nothing is uploaded and there is no account. Your best win streak for each level is saved in your browser’s local storage, as small whole numbers inside a small JSON object, under one key. The session tally and the current game are kept in memory only, so they reset when you leave the page. The address records only your opponent, level and seat. Clearing site data removes the saved streaks.
Everything runs locally in your browser.
For informational purposes only. Not financial, medical, or legal advice. You are solely responsible for how you use these tools.