#33Connect Four
Overview
Build a Connect Four game where players take turns dropping discs into a 6x7 grid until one achieves four in a row.
Requirements
- Implement the
useApphook to manage the game state. - Allow players to take turns dropping discs into the columns of the 6x7 grid.
- Ensure discs "fall" to the lowest available row in the chosen column.
- Implement the
checkForWinfunction to detect 4 discs of the same color in a row (horizontally, vertically, or diagonally). - Update the "Turn" indicator after each move.
- Update the "Winner" indicator when a player wins and stop further moves.
- Implement the "Reset Game" button to clear the board and reset the turn to red.
- Maintain the
Celltype andcreateEmptyBoardfunction signature as provided. - The only valid disc colors are
"white","red", and"yellow". Tests will check these exact color values. - All game logic should be tested using the provided
data-testidattributes.
Notes
- The challenge is split across multiple files (
App.tsx,useApp.ts,lib.ts) to simulate a real project structure. App.tsxcontains the main UI structure and placeholders for the game board, turn indicator, and winner indicator.useApp.tsis where the game logic (state management, handling moves, and resetting the board) should be implemented.lib.tscontains utility functions and types (Cell,createEmptyBoard,checkForWin) that must be respected and can be used in your implementation.- Each cell in the board is a
<button>with adata-testidin the formatcell-{row}-{col}to facilitate testing. - The "Turn" indicator uses
data-testid="turn-indicator"and the winner display usesdata-testid="winner-indicator". - Make sure the discs "fall" to the lowest available row in the chosen column.
- After a win, no further moves should be allowed until the board is reset.
- All styling and layout use Tailwind CSS classes; focus on the game logic rather than UI enhancements.
Tests
- renders the app title
- renders 6x7 grid of cells
- displays initial turn as red
- allows placing a red disc in the first column
- switches turn after placing a disc
- resets the board correctly
- declares a winner when 4 in a row
- detects horizontal win
- detects vertical win
- detects diagonal win ↘
- detects diagonal win ↗