Connect Four OOP Game
Written by a community member · · 1 attempt · 0% pass
Design an object-oriented Connect Four game with a ConnectFour class.
The constructor is:
ConnectFour(rows: int, cols: int)
Implement:
play_move(column: int) -> list[list[int]]
display_board() -> list[list[int]]
Player 1 moves first, and players alternate automatically. Represent player 1 with 1, player 2 with 2, and empty cells with 0. The board is returned from top row to bottom row. A move places the current player's piece in the selected zero-based column according to standard Connect Four rules. The game ends when a player gets four consecutive pieces horizontally, vertically, or diagonally; moves after the game ends should have no effect. Invalid moves should also have no effect. Display the board after each move, and have display_board display and return the current board.
For this exercise, the board has at least four rows and four columns. Use encapsulated instance state rather than global variables. The class should be implementable with equivalent behavior in Python, JavaScript, TypeScript, C++, Java, C#, Go, Kotlin, Swift, and Rust.
Example: on a 6-by-7 board, the move sequence [0, 1, 0, 1, 0, 1, 0] gives player 1 a vertical four-in-a-row in column 0 and ends the game.