2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00
2026-03-06 09:49:23 +01:00

Connect Four

Connect Four is a two player game played on a 7 by 6 grid. Each player has a color: one player is red, and the other player is yellow. The first player starts Connect Four by dropping one of their yellow discs into the (center) column of an empty game board. The two players then alternate turns dropping one of their discs at a time into an unfilled column, until the second player, with red discs, achieves a diagonal four in a row, and wins the game. If the board fills up before either player achieves four in a row, then the game is a draw.

Technical specifications

The game board is an 8 x 8 NeoPixel grid. To mark the limits of the board, the top row is off, in the row below the pixels are blue, and in the rightmost column's pixels are blue from the row below the empty row until the bottom row. The game consists of a rotary encoder, using the Encoder.h library connected to pins:

#define ENCODER_A 2
#define ENCODER_B 3
#define ENCODER_SW 4

The grid is managed using the FastLED.h library and is connected to:

#define LED_PIN 6
#define LED_WIDTH 8
#define LED_HEIGHT 8
#define NUM_LEDS (LED_WIDTH * LED_HEIGHT)
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

Players play by turning the rotary encoder to choose a column by rotating the encoder. When they press the button, the disc is dropped and falls to the lowest free position in the grid. If a column is full and a player tries to drop a disc, the disc at the top (column selection row) blinks, indicating that no disc can be dropped. If the player rotates the encoder to a non-full column, a disc can be played.

A win is when one of the players achieves four connecting discs of the same color: horizontal, vertical, or diagonal. If a player wins, all the discs on the board a dimmed to 15% intensity, and the winning four discs are blinking at high intensity.

If the board is full and none of the players win, a draw, then all the discs on the board are dimmed, and a blinking animation indicates a draw.

The game has four states:

  1. Menu: Here the players choose the type of game they want to play, 1 or 2 player. This is indicated on the board by a single yellow vertical bar in the center column (single player versus computer) or two vertical bars: two player game. Rotating the encoder switches between game modes, and pressing the button selects the game mode.
  2. Game play: Players are playing the game until one of the players connects four or they achieve a draw.
  3. Game over: One of the players connects four, or they achieve a draw. If the rotary encoder button is pressed, this state switches to the Menu state.
  4. Demo mode: The computer plays against itself. This mode is automatically triggered if there is no input for 60 seconds. If the rotary encoder is turned or pushed, the demo mode exits and the game returns to the menu state.

The program initializes the SerialPrint output (baud 115200) and outputs useful (debugging) information regarding the game state and selections.

S
Description
AN ESP32 project that plays connect-four on a NeoPixel 8x8
Readme 264 KiB
Languages
Python 54%
JavaScript 25.1%
C++ 20.2%
HTML 0.7%