Practice/Databricks/Tic-Tac-Toe Game (M x N Board)
Tic-Tac-Toe Game (M x N Board)
CodingMust
Problem Overview
Design and implement a Tic-Tac-Toe game class for an M x N board (not necessarily square) with a configurable win condition requiring K consecutive marks in a row, column, or diagonal.
This is a generalization of the classic 3x3 Tic-Tac-Toe game where:
- The board can be any size (M rows, N columns)
- Players need K consecutive marks to win (not necessarily 3)
- Two players alternate turns using marks X (Player 1) and O (Player 2)
- Win detection checks all four directions: horizontal, vertical, and both diagonals
Example
`
game = TicTacToe(3, 4, 3) # 3x4 board, need 3 in a row to win
game.move(0, 0) # Player 1 places X at (0,0)
Board:
| X | | | |
| | | | |
| | | | |
game.move(0, 1) # Player 2 places O at (0,1)
Board:
| X | O | | |
| | | | |
| | | | |
game.move(1, 0) # Player 1
game.move(1, 1) # Player 2
game.move(2, 0) # Player 1 wins with column!
Board:
| X | O | | |
| X | O | | |
| X | | | |
Player 1 won
Returns: 1
`
Requirements
Board Configuration:
- M x N board (not necessarily square)
- K consecutive marks needed to win (K is configurable)
- 2 <= M, N <= 100
- 2 <= K <= max(M, N)
Core Functionality:
-
TicTacToe(M, N, K): Constructor that initializes the board
-
move(i, j): Places a mark for the current player at position (i, j)
- Automatically alternates between players
- Returns 0 if no winner, 1 if Player 1 wins, 2 if Player 2 wins
- All moves are guaranteed to be valid (no error handling needed)
Win Conditions:
A player wins by getting K consecutive marks in:
- Any row (horizontal)
- Any column (vertical)
- Main diagonal (top-left to bottom-right)
- Anti-diagonal (bottom-left to top-right)