gekro
GitHub LinkedIn

Four in a Row

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

  • You: violet disc with a dot
  • The computer: amber disc with a ring

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.

How the computer plays

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.

© 2026 Rohit Burani · MIT · Built at gekro.com · View source ↗

Guide

How to play

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.

  • Mouse or touch: press a column button above the board, or tap the board itself. The column buttons say how many spaces are free and switch off when a column is full.
  • 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 computer’s reply, so it is your turn again. With two players it takes back one move. Once a game is over, undo is off. A game in which you used Undo against the computer is not counted: it leaves the session tally, your win streak and your saved best as they were.
  • Copy result copies a short summary of your session tally and the page address.

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.

How it works

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.

LevelLooks aheadNotes
Easy1 movePlays a random column about one move in four
Medium4 movesNever plays at random
Hard7 movesNever 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:

  • all 69 lines are detected for both players, and a line cannot wrap from the end of one row to the start of the next;
  • discs stack from the bottom, a full column rejects a seventh disc, and a finished game accepts no more moves;
  • the computer takes a win and blocks a loss at every search depth, from either side and in mirror image;
  • turning pruning off never changes a search value, so pruning only saves work;
  • hundreds of full games of random and computer play keep the board consistent, with the right disc counts and no move after a win;
  • the same random seed gives the same computer move.

Privacy and storage

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.

Limits

  • Hard looks seven moves ahead, not to the end of the game, so a long plan can still beat it. It is a good opponent, not a perfect one.
  • Easy is meant to be beatable: it looks one move ahead and sometimes plays a random column.
  • A win streak counts only finished games against the computer in which you did not use Undo. A draw leaves it unchanged, a loss resets it, and a game you abandon is not counted.
  • With two players there is no clock, no names and no saved games.
  • The tally and the current game are not saved if you reload the page.

Everything runs locally in your browser.

All Fun tools →

For informational purposes only. Not financial, medical, or legal advice. You are solely responsible for how you use these tools.