AlgoViz

Design four in a row

medium 75 min

Model a game in which balls fall into tubes and four in a line win.

Watch lesson4:35

Where it shows up

The same pattern appears across everyday software.

  • OpenSpiel: connect_four

    DeepMind's collection lets the number of rows, columns and of pieces in a row be parameters, with 6, 7 and 4 as defaults. The three arguments of our constructor.

  • PettingZoo: connect_four

    The learning environment has seven actions, one for each column. Nobody chooses a height, and a full column may not be played.

  • Pascal Pons: a perfect solver

    An open tutorial builds a solver step by step: negamax, alpha-beta, middle first, a bitmap with one extra bit for each column, a table of positions, and deepening. The order of our two pages.

  • Kaggle: ConnectX

    A competition in which programs play this game with configurable sizes. Each move has a time limit, which is why deepening until the bell matters.

  • Chess programs: bitboards

    One 64 bit word holds one kind of piece on 64 squares. Shifts and ands answer questions about the whole board at once, as our twelve operations do.

  • Chess programs: Zobrist keys

    A random number for each piece and square, combined by exclusive-or. A move changes the key with a few operations, and the key finds the position in a table.

  • HTTP: If-Match

    RFC 9110 lets a client make a change only if the resource is still the version it knows; otherwise the answer is 412. The number of a drop is the same guard.

  • Java: Long.bitCount

    Counting the ones in a 64 bit number is one method in the standard library, and one instruction on common processors. Estimates over bitboards use it.