Class TicTacToe

java.lang.Object
com.project.game.TicTacToe

public class TicTacToe extends Object
The TicTacToe class represents the core logic of the Tic-Tac-Toe game.

It handles board initialization, player selection, game play, and winner determination.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final String[][]
    3x3 game board represented as a 2D array.
    private String
    The symbol of the player whose turn it is (either "X" or "O").
    private boolean
    Flag to indicate if the game is over.
    private final String
    Symbol representing Player O.
    private final String
    Symbol representing Player X.
    private final Scanner
    Scanner object for reading user input.
    private int
    Counter to track the number of turns taken.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new TicTacToe game.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Checks for a winner or a tie by evaluating board conditions.
    private void
    Displays the initial board layout with cell numbers for reference.
    private void
    Controls the main game loop.
    private void
    Allows the user to select the first player (X or O).
    private void
    Declares the current player as the winner and ends the game.
    private void
    updateBoard(int row, int col)
    Updates the board with the current player's move and redraws the board.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • isGameOver

      private boolean isGameOver
      Flag to indicate if the game is over.
    • turns

      private int turns
      Counter to track the number of turns taken.

      Maximum value is 9, as the board has 9 cells.

    • playerX

      private final String playerX
      Symbol representing Player X.
      See Also:
    • playerO

      private final String playerO
      Symbol representing Player O.
      See Also:
    • currentPlayer

      private String currentPlayer
      The symbol of the player whose turn it is (either "X" or "O").
    • board

      private final String[][] board
      3x3 game board represented as a 2D array. Each cell contains either "X", "O", or an empty string.
    • sc

      private final Scanner sc
      Scanner object for reading user input.
  • Constructor Details

    • TicTacToe

      TicTacToe()
      Constructs a new TicTacToe game.

      Initializes the board, prompts the user to select the first player, and starts the game loop.

  • Method Details

    • selectPlayer

      private void selectPlayer()
      Allows the user to select the first player (X or O).

      Ensures that the input is valid and assigns the current player accordingly.

    • initializeBoard

      private void initializeBoard()
      Displays the initial board layout with cell numbers for reference.

      The board is printed with cell numbers 1-9, arranged in a 3x3 grid format.

    • playGame

      private void playGame()
      Controls the main game loop.

      Handles player turns, board updates, and checks for a winner or a tie. Switches players after every valid move.

    • updateBoard

      private void updateBoard(int row, int col)
      Updates the board with the current player's move and redraws the board.
      Parameters:
      row - The row index of the board (0-2).
      col - The column index of the board (0-2).
    • checkWinner

      private void checkWinner()
      Checks for a winner or a tie by evaluating board conditions.

      - Checks for winning combinations horizontally, vertically, and diagonally.
      - Declares the winner or tie and terminates the game if the conditions are met.

    • setWinner

      private void setWinner()
      Declares the current player as the winner and ends the game.