Skip to main content

Command Palette

Search for a command to run...

Find Winner on a Tic Tac Toe Game

Published
3 min read
Find Winner on a Tic Tac Toe Game
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:

  • Players take turns placing characters into empty squares ' '.

  • The first player A always places 'X' characters, while the second player B always places 'O' characters.

  • 'X' and 'O' characters are always placed into empty squares, never on filled ones.

  • The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.

  • The game also ends if all squares are non-empty.

  • No more moves can be played if the game is over.

Given a 2D integer array moves where moves[i] = [row<sub>i</sub>, col<sub>i</sub>] indicates that the i<sup>th</sup> move will be played on grid[row<sub>i</sub>][col<sub>i</sub>]. return the winner of the game if it exists (A or B). In case the game ends in a draw return "Draw". If there are still movements to play return "Pending".

You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.

LeetCode Problem - 1275

class Solution {
    public String tictactoe(int[][] moves) {
        // Initialize a 3x3 Tic-Tac-Toe board
        char[][] board = new char[3][3];

        // Loop through each move in the game
        for (int i = 0; i < moves.length; i++) {
            int row = moves[i][0]; // Row index of the current move
            int col = moves[i][1]; // Column index of the current move

            // Assign 'X' to player A's move (even index) and 'O' to player B's move (odd index)
            board[row][col] = (i % 2 == 0) ? 'X' : 'O';

            // Check if the current move results in a win
            if (checkIn(board, row, col)) {
                // If player A (X) wins, return "A", else if player B (O) wins, return "B"
                return (i % 2 == 0) ? "A" : "B";
            }
        }

        // If all moves are made and no winner, check if it's a draw or pending
        return (moves.length == 9) ? "Draw" : "Pending";
    }

    // Function to check if the current player has won
    public boolean checkIn(char[][] board, int row, int col) {
        char player = board[row][col]; // Current player's symbol ('X' or 'O')

        // Check if the player has a complete row
        if (board[row][0] == player && board[row][1] == player && board[row][2] == player) {
            return true;
        }

        // Check if the player has a complete column
        if (board[0][col] == player && board[1][col] == player && board[2][col] == player) {
            return true;
        }

        // Check if the player has a diagonal (top-left to bottom-right)
        if (row == col && board[0][0] == player && board[1][1] == player && board[2][2] == player) {
            return true;
        }

        // Check if the player has a diagonal (top-right to bottom-left)
        if (row + col == 2 && board[0][2] == player && board[1][1] == player && board[2][0] == player) {
            return true;
        }

        // No win condition met
        return false;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.