Package com.project.game
Class TicTacToe
java.lang.Object
com.project.game.TicTacToe
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
FieldsModifier and TypeFieldDescriptionprivate final String[][]3x3 game board represented as a 2D array.private StringThe symbol of the player whose turn it is (either "X" or "O").private booleanFlag to indicate if the game is over.private final StringSymbol representing Player O.private final StringSymbol representing Player X.private final ScannerScanner object for reading user input.private intCounter to track the number of turns taken. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidChecks for a winner or a tie by evaluating board conditions.private voidDisplays the initial board layout with cell numbers for reference.private voidplayGame()Controls the main game loop.private voidAllows the user to select the first player (X or O).private voidDeclares the current player as the winner and ends the game.private voidupdateBoard(int row, int col) Updates the board with the current player's move and redraws the board.
-
Field Details
-
isGameOver
private boolean isGameOverFlag to indicate if the game is over. -
turns
private int turnsCounter to track the number of turns taken.Maximum value is 9, as the board has 9 cells.
-
playerX
Symbol representing Player X.- See Also:
-
playerO
Symbol representing Player O.- See Also:
-
currentPlayer
The symbol of the player whose turn it is (either "X" or "O"). -
board
3x3 game board represented as a 2D array. Each cell contains either "X", "O", or an empty string. -
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.
-