← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Coding - Tic-Tac-Toe Game (M x N Board with Configurable Win Condition)
Design and implement a Tic-Tac-Toe game class with the following specifications:
Board Configuration:
Class Structure:
Input:
TicTacToe game = new TicTacToe(3, 3, 3);game.move(0, 0, 1); (Player 1 places an X at (0, 0))game.move(0, 2, 2); (Player 2 places an O at (0, 2))game.move(2, 2, 1); (Player 1 places an X at (2, 2))game.move(1, 1, 2); (Player 2 places an O at (1, 1))game.move(2, 0, 1); (Player 1 places an X at (2, 0))game.move(1, 0, 2); (Player 2 places an O at (1, 0))game.move(2, 1, 1); (Player 1 places an X at (2, 1))Output:
game.getWinner() should return 1 (Player 1 wins)1 <= M, N <= 201 <= K <= min(M, N)K <= M * N0 <= row, col, player <= 0row and col are within the board size (M x N)player is either 1 or 2Board Representation:
Move Validation:
Winning Condition:
Game State:
class TicTacToe:
def __init__(self, n: int, m: int, k: int):
self.board = [[0] * m for _ in range(n)]
self.n = n
self.m = m
self.k = k
def move(self, row: int, col: int, player: int) -> bool:
if self.board[row][col] != 0 or not (0 <= row < self.n) or not (0 <= col < self.m):
return False
self.board[row][col] = player
return self.check_win(player)
def check_win(self, player: int) -> bool:
for i in range(self.n):
for j in range(self.m - self.k + 1):
if all(self.board[i][j + x] == player for x in range(self.k)):
return True
if all(self.board[j + x][i] == player for x in range(self.k)):
return True
for i in range(self.n - self.k + 1):
for j in range(self.m - self.k + 1):
if all(self.board[i + x][j + x] == player for x in range(self.k)):
return True
if all(self.board[i + x][j + self.k - 1 - x] == player for x in range(self.k)):
return True
return False
def getWinner(self) -> int:
for i in range(self.n):
for j in range(self.m):
if self.board[i][j] == 0:
return 0
for i in range(self.n):
for j in range(self.m - self.k + 1):
if all(self.board[i][j + x] == self.board[i][j] for x in range(1, self.k)):
return self.board[i