Welcome to the TicTacToe game! This is a simple yet engaging graphical version of the classic TicTacToe game implemented in Java using Swing. The game allows you to play against the computer, with adjustable settings for line thickness and player colors.
- Interactive Gameplay: Click on the grid to make your move.
- Adjustable Line Thickness: Use the slider to adjust the thickness of the grid lines.
- Customizable Colors: Choose different colors for 'O' and 'X' using the color picker.
- Game Statistics: Keep track of your wins, losses, and draws.
- Computer Opponent: Play against an AI that tries to block your moves and win the game.
- Java Development Kit (JDK) installed on your computer.
- Save the code to a file named TicTacToe.java.
- Open a terminal or command prompt.
- Navigate to the directory where TicTacToe.java is saved.
- Compile the Java file using the command:
Copy code - javac TicTacToe.java
- Run the compiled Java program using the command:
Copy code - java TicTacToe
- Start the Game: Launch the game to see the TicTacToe grid.
- Make a Move: Click on an empty cell to place your 'O'.
- Computer's Turn: After you place 'O', the computer will automatically make its move as 'X'.
- Win, Lose, or Draw: The game checks for a win, loss, or draw after each move. If the game ends, a dialog box will show the result and ask if you want to play again.
- Adjust Settings:
- Use the Line Thickness slider to change the thickness of the grid lines.
- Click on the O Color button to choose a new color for 'O'.
- Click on the X Color button to choose a new color for 'X'.
- TicTacToe(): The constructor initializes the game window, adding the slider, color buttons, and the game board.
- stateChanged(ChangeEvent e): Adjusts the line thickness based on the slider's value.
- actionPerformed(ActionEvent e): Opens a color chooser dialog to change the color of 'O' or 'X'.
- Board(): Adds a mouse listener to handle user clicks on the game board.
- paintComponent(Graphics g): Draws the TicTacToe grid and the 'O' and 'X' marks.
- mouseClicked(MouseEvent e): Handles user clicks to place 'O' and triggers the computer's move.
- putX(): Manages the computer's moves and checks for game over conditions.
- won(char player): Checks if the specified player has won the game.
- testRow(char player, int a, int b): Tests if a player has completed a row.
- nextMove(): Determines the best move for the computer.
- findRow(char player): Finds a row where the specified player can win or block.
- find1Row(char player, int a, int b): Finds a blank spot in a row where the player can win or block.
- isDraw(): Checks if the game is a draw.
- newGame(char winner): Resets the board and starts a new game, showing the result of the previous game.
You can easily customize the game by modifying the following parts of the code:
- Initial Line Thickness: Change the initial value of lineThickness in the TicTacToe constructor.
- Initial Colors: Change the initial values of oColor and xColor in the TicTacToe constructor.
- Board Size: Adjust the size of the game window by changing the dimensions in the setSize() method call.
Enjoy playing this graphical TicTacToe game against the computer. Customize the game settings to your liking and see how many times you can win against the AI!