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 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 -
Method Summary
Modifier and TypeMethodDescriptionprivate 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
playGame()
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.
-
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.
-