Connect Four
UI Coding
Hard
50 min
games
use-reducer
algorithms
accessibility
keyboard
Build Connect Four for two players on one device. The board has 7 columns × 6 rows. Players take turns dropping a disc into a column. The disc falls to the lowest empty cell in that column. The first player to line up four discs horizontally, vertically or diagonally wins. Red moves first, then Yellow.
The starter renders an empty board and a row of column buttons.
Requirements
- Clicking a column button, or any cell in that column, drops the current player's disc into that column.
- A full column can't take more discs. Its button is named
Column 3, fulland is marked unavailable. - Detect a win after every move by checking only around the disc just placed. Highlight the winning discs. When the board fills with no winner, it's a draw.
- A status line (
role="status") shows whose turn it is and the last move, for exampleYellow dropped in column 4. Red's turn., orRed wins!/It's a draw.After the game ends, drops are ignored. - A New game button resets the board.
- Accessibility:
- Column buttons are named
Drop in column Nand form a single tab stop. <kbd>←</kbd>/<kbd>→</kbd> move between columns, <kbd>Home</kbd>/<kbd>End</kbd> jump to the first and last column, and <kbd>Enter</kbd>/<kbd>Space</kbd> drop. - Board cells expose their contents:
Row 6, column 1, red. - Disc colours aren't the only cue (for example, add a pattern or a letter for Yellow) so colour-blind players can tell them apart.
- Column buttons are named
- Animate the disc falling, and respect
prefers-reduced-motion.
Follow-up
- Add an AI opponent. How deep can minimax go in the browser, and where would you run it?
- Make rows, columns and the line length configurable.
- How would you sync a game between two browsers?
Spotted a mistake or a rights concern? Report an issue