Implement a Connect Four game class with move validation, win detection and draw detection
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement the logic for a Connect Four game. Connect Four is a two-player connection board game in which players take turns dropping colored discs into a vertically suspended grid. The objective is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs.
The game is played on a grid with a given number of `rows` and `cols` (default `6` rows by `7` columns). Row `0` is the top of the grid and row `rows - 1` is the bottom. Players alternate turns, starting with player 1. A disc dropped into a column falls to the lowest available row in that column.
The game ends when a player connects four discs in a row (horizontally, vertically, or diagonally) or when the board is completely full, resulting in a draw.
Implement a class `ConnectFour(rows, cols)` with the following methods:
- `makeMove(col: int) -> int`: Drop a disc into the specified column for the current player. Returns `0` if the game continues, `1` if player 1 wins, `2` if player 2 wins, `3` if the game is a draw, or `-1` if the move is invalid (column full, out of range, or game already over).
- `getBoard() -> List[List[int]]`: Return the current state of the board as a 2D list. `0` represents empty, `1` represents player 1, and `2` represents player 2.
- `getCurrentPlayer() -> int`: Return the current player's number (`1` or `2`). Returns `0` if the game is already over.
- `getGameState() -> int`: Return the game state: `0` if ongoing, `1` if player 1 wins, `2` if player 2 wins, or `3` if the game is a draw.
Example 1, a vertical win:
```text
Input:
["ConnectFour", "makeMove", "makeMove", "makeMove", "makeMove", "makeMove", "makeMove", "makeMove", "getGameState"]
[[6, 7], [0], [1], [0], [1], [0], [1], [0], []]
Output:
[null, 0, 0, 0, 0, 0, 0, 1, 1]
game = ConnectFour(6, 7) # 6x7 board, player 1 starts
game.makeMove(0) # 0 P1 lands in col 0, row 5
game.makeMove(1) # 0 P2 lands in col 1, row 5
game.makeMove(0) # 0 P1 lands in col 0, row 4
game.makeMove(1) # 0 P2 lands in col 1, row 4
game.makeMove(0) # 0 P1 lands in col 0, row 3
game.makeMove(1) # 0 P2 lands in col 1, row 3
game.makeMove(0) # 1 P1 lands in col 0, row 2: four in column 0
game.getGameState() # 1 player 1 has won
```
Example 2, a horizontal win:
```text
Input:
["ConnectFour", "makeMove", "makeMove", "makeMove", "makeMove", "makeMove", "makeMove", "makeMove", "getCurrentPlayer", "getGameState"]
[[6, 7], [0], [0], [1], [1], [2], [2], [3], [], []]
Output:
[null, 0, 0, 0, 0, 0, 0, 1, 0, 1]
game = ConnectFour(6, 7) # 6x7 board, player 1 starts
game.makeMove(0) # 0 P1 lands in col 0, row 5
game.makeMove(0) # 0 P2 lands in col 0, row 4
game.makeMove(1) # 0 P1 lands in col 1, row 5
game.makeMove(1) # 0 P2 lands in col 1, row 4
game.makeMove(2) # 0 P1 lands in col 2, row 5
game.makeMove(2) # 0 P2 lands in col 2, row 4
game.makeMove(3) # 1 P1 lands in col 3, row 5: four in row 5
game.getCurrentPlayer() # 0 the game is over
game.getGameState() # 1 player 1 has won
```
```hint Finding the landing row
Keep one number per column so that a drop never has to scan the column for its first empty cell.
```
```hint Checking for a win
Only lines that pass through the disc just placed can have changed. Count how many directions such a line can run in, and remember the new disc may land in the middle of a line.
```
### Constraints & Assumptions
- `1 <= rows <= 20`
- `1 <= cols <= 20`
- Column numbers are 0-indexed (valid range: `0` to `cols - 1`).
- Player 1 always moves first.
- Dropping a disc into a full or out-of-range column is an invalid move.
- At most `rows * cols + 100` calls will be made to `makeMove`.
### Clarifying Questions to Ask
- Does an invalid move cost the current player their turn, or do they move again?
- If the last empty cell also completes four in a line, is the result a win or a draw?
- Should `getBoard` return a copy, or may callers hold a live reference to the internal grid?
- On a board with fewer than four rows or columns, should the game still run normally and end in a draw when it fills up?
### What a Strong Answer Covers
- The state the class keeps, and how a drop finds its landing row without scanning the column.
- How the win check after a move limits itself to the new disc, and covers all four line directions on both sides of it.
- The order of steps inside `makeMove` (validation, placement, win, draw, turn switch) and why that order matters.
- Invalid moves and calls after the game has ended, handled with no change to the state.
- Time and space cost of each method, including the cost of returning the board.
### Follow-up Questions
- How would you generalize the class to `k` discs in a row and more than two players?
- How would you add an `undo()` method that also works right after a winning move?
- How could a bitboard representation make the win check a few bitwise operations?
- What would a computer opponent need from this class to search ahead efficiently?
Overview: Implement a Connect Four game class that drops discs into columns, rejects invalid moves, and reports wins and draws through fixed return codes. It tests object-oriented state design, win detection in all four line directions, and careful handling of edge cases such as a full board or moves after the game ends.