Implement the findWinnerTicTacToeRow method that returns the winner found in any complete row of a tic-tac-toe board.

The input contains a Tic-Tac-Toe board board and its size n. The board contains player marks such as X and O, and empty cells are represented by E.

Your task is to return the player mark that has completed a full row, full column, or full diagonal. A player wins when all cells in one of these lines contain the same non-empty mark.

For example, in [[X,X,X],[O,O,E],[E,E,E]], the first row contains only X, so the result is X.

Example 1

Input
board (char[][]) = [[X,X,X],[O,O,E],[E,E,E]]
n (int) = 3
Returns (char) X

Example 2

Input
board (char[][]) = [[X,O,X],[O,X,O],[O,X,X]]
n (int) = 3
Returns (char) X

Example 3

Input
board (char[][]) = [[O,O,O],[X,X,E],[E,E,E]]
n (int) = 3
Returns (char) O

Check every possible winning line on the board.

First check each row, then each column. For a line to be a winning line, its first cell must not be E, and every other cell in that line must be equal to it.

Finally, check both diagonals using the same rule. Return the winning mark as soon as it is found. If no winning line exists, return E as the no-winner value.

Pseudocode:

function findWinnerTicTacToeRow(board, n):
    for each row r:
        if board[r][0] != E and every cell in row r equals board[r][0]:
            return board[r][0]
    for each column c:
        if board[0][c] != E and every cell in column c equals board[0][c]:
            return board[0][c]
    if board[0][0] != E and every main diagonal cell is equal:
        return board[0][0]
    if board[0][n - 1] != E and every opposite diagonal cell is equal:
        return board[0][n - 1]
    return E
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.