Tic-Tac-Toe Game in Java: Two-Player Console-Based Project
Introduction to the Java Tic-Tac-Toe Game:
Tic-Tac-Toe Game:
Create a simple two-player console-based game.
A console-based version of the classic two-player game. Players take turns marking spaces on a 3x3 grid, and the first player to get three marks in a row (horizontally, vertically, or diagonally) wins.
Input: Player move (row and column) on a 3x3 grid.
Output: Updated game board and game status (win/draw/continue).
Example:
- Input: Player 1 selects row 1, column 2
- Output: Updated grid, "Player 1 wins" or "Continue playing"
Solution 1: Tic-Tac-Toe with Simple Array and Loops
Code:
Output:
1 2 3 1 | | ----- 2 | | ----- 3 | | Player X's turn. Enter row and column (1-3): 1 1 1 2 3 1 X| | ----- 2 | | ----- 3 | | Player O's turn. Enter row and column (1-3): 1 2 1 2 3 1 X|O| ----- 2 | | ----- 3 | | Player X's turn. Enter row and column (1-3): 3 3 1 2 3 1 X|O| ----- 2 | | ----- 3 | |X Player O's turn. Enter row and column (1-3): 3 2 1 2 3 1 X|O| ----- 2 | | ----- 3 |O|X Player X's turn. Enter row and column (1-3): 2 2 1 2 3 1 X|O| ----- 2 |X| ----- 3 |O|X Player X wins!
Explanation :
- Game Board: The board is represented by a 2D array, initialized with empty spaces.
- Player Input: Players input the row and column to place their mark.
- Win/Draw Conditions: The game checks for a win by rows, columns, or diagonals. It also checks if all spaces are filled (indicating a draw).
- Switching Players: The game alternates between 'X' and 'O'.
- Loop: The game loop runs until either a player wins or the game ends in a draw.
Solution 2: Using Object-Oriented Approach
Key points:
- Game Board Initialization: The board is initialized as a 3x3 grid, filled with empty spaces.
- Player Moves: The user inputs row and column values, and the game updates the board accordingly if the input is valid.
- Win Condition: After every move, the game checks if the current player has completed a row, column, or diagonal.
- Draw Condition: If the board is full and no one wins, the game is a draw.
- Switching Players: After every valid move, the current player switches between 'X' and 'O'.
- Game Restart: After a game ends, players can choose to restart the game with an empty board or end the session.
- Statistics: The game keeps track of how many games each player has won and the number of draws.
- Input Validation: Invalid inputs (e.g., selecting already occupied spots or entering out-of-bound values) are handled, and the player is prompted to re-enter their move.
Code:
TicTacToe.java
Explanation:
- Class Definition:
- The TicTacToe class manages the game's logic, including the board, player turns, win checks, and statistics.
- Fields:
- board: A 3x3 char array representing the game board.
- currentPlayer: Tracks the current player ('X' or 'O').
- playerXWins, playerOWins, draws: Integers to track the number of wins for each player and the number of draws.
- Constructor:
- Initializes the board, sets the starting player to 'X', and resets the win/draw counters.
- initializeBoard():
- Fills the board with empty spaces (' ') to prepare it for gameplay.
- printBoard():
- Displays the current state of the board in a 3x3 grid format with row and column numbers for easy reference.
- switchPlayer():
- Switches the current player between 'X' and 'O' after each turn.
- makeMove(int row, int col):
- Places the current player's mark on the specified row and column, if the spot is valid and unoccupied.
- Returns true if the move is valid, otherwise false.
- checkWin():
- Checks all possible winning conditions (rows, columns, and diagonals) to determine if the current player has won.
- Returns true if there is a win, otherwise false.
- checkDraw():
- Checks if the board is full and no moves are possible, indicating a draw.
- Returns true if the game is a draw, otherwise false.
- getCurrentPlayer():
- Returns the current player ('X' or 'O').
- updateWinCount():
- Increments the win count for the current player (either playerXWins or playerOWins).
- updateDrawCount():
- Increments the draw count (draws).
- displayStatistics():
- Displays the current statistics: number of wins for Player X, Player O, and the number of draws.
- resetBoard():
- Resets the board for a new game, clearing all spaces and setting the starting player to 'X'.
TicTacToeOOP.java
Explanation:
- Imports:
- java.util.Scanner: Used to capture user input from the console.
- main() Method:
- The entry point of the program where the game logic is executed.
- Game Initialization:
- Creates a TicTacToe object named game.
- Initializes a Scanner object to read user input.
- Main Game Loop (while (playAgain)):
- Controls whether the players want to play another game.
- Runs continuously until the players choose not to play again.
- Single Game Loop:
- Repeats until either a player wins or the game ends in a draw.
- Calls game.printBoard() to display the current state of the board.
- Prompts the current player to enter row and column coordinates for their move.
- Input Handling:
- Player enters row and column (1-3), which are converted to 0-indexed values.
- Checks if the move is valid using game.makeMove().
- Move Validation:
- If the move is valid:
- Calls game.checkWin() to see if the current player has won.
- If no one has won, calls game.checkDraw() to see if the game is a draw.
- If the game is ongoing, switches the player using game.switchPlayer().
- Winning Condition:
- If a player wins, the board is displayed, and the current player is declared the winner.
- Increments the win count for the winner using game.updateWinCount().
- Draw Condition:
- If the game is a draw, displays a message and increments the draw count with game.updateDrawCount().
- Display Statistics:
- After each game, game.displayStatistics() shows the win and draw counts.
- Play Again Prompt:
- Asks the players if they want to play another game.
- If they answer "yes", the board is reset using game.resetBoard().
- If "no", the loop ends, and the game concludes.
- Game Conclusion:
- Displays a thank-you message and closes the Scanner object.
Output:
1 2 3 1 | | ----- 2 | | ----- 3 | | Player X's turn. Enter row and column (1-3): 1 1 1 2 3 1 X| | ----- 2 | | ----- 3 | | Player O's turn. Enter row and column (1-3): 1 3 1 2 3 1 X| |O ----- 2 | | ----- 3 | | Player X's turn. Enter row and column (1-3): 2 1 1 2 3 1 X| |O ----- 2 X| | ----- 3 | | Player O's turn. Enter row and column (1-3): 3 1 1 2 3 1 X| |O ----- 2 X| | ----- 3 O| | Player X's turn. Enter row and column (1-3): 2 2 1 2 3 1 X| |O ----- 2 X|X| ----- 3 O| | Player O's turn. Enter row and column (1-3): 2 3 1 2 3 1 X| |O ----- 2 X|X|O ----- 3 O| | Player X's turn. Enter row and column (1-3): 1 2 1 2 3 1 X|X|O ----- 2 X|X|O ----- 3 O| | Player O's turn. Enter row and column (1-3): 3 3 1 2 3 1 X|X|O ----- 2 X|X|O ----- 3 O| |O Player O wins! Player X Wins: 0 Player O Wins: 1 Draws: 0 Do you want to play again? (yes/no): no Thanks for playing!
Java Code Editor:
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics