This project implements two versions of the classic game Tic-Tac-Toe:
- TicTacToeClassic: A traditional two-player version where users take turns marking X's and O's.
- TicTacToeAI: A single-player version where the user plays against an AI opponent powered by the Minimax algorithm with alpha-beta pruning. (Which is impossible to defeat, the best you can do is a draw)
- Graphical User Interface (GUI): Uses Java Swing to create an interactive and visually appealing game board.
- AI Opponent (TicTacToeAI): Implements the Minimax algorithm with alpha-beta pruning for intelligent decision-making. The AI is designed to be unbeatable.
- Win/Loss/Draw Detection: Automatically determines the outcome of the game and displays a message accordingly.
- Scorekeeping: Keeps track of wins for both the player and the AI (in
TicTacToeClassic). - Clear Board Functionality: Allows resetting the game to start a new round.
JButtonArray: Uses a 2D array ofJButtoncomponents to represent the game board.ActionListener: Each button has anActionListenerto handle player moves, update the board, and check for wins/draws.checkWinMethod: Iterates through rows, columns, and diagonals to check for winning combinations.winLogicMethod: Handles the logic for displaying the winner and updating the score.clearBoardMethod: Resets the board and clears the game state.
MinimaxClass: Implements the Minimax algorithm with alpha-beta pruning in a separate class.evaluateMethod: Recursively evaluates the game tree to determine the best move for the AI.checkWinandcheckDrawMethods: Helper functions to determine the game state.AIMoveMethod: Calls theMinimaxclass to calculate the optimal move and updates the board.
- Here's a web representation of the GAME.
- Clone the Repository:
git clone https://github.com/loftyyyy/TicTacSwing- Compile and Run:
javac org/example/*.java
java org.example.TicTacToeClassic // For two-player mode
java org.example.TicTacToeAI // For AI modeTicTacToeClassic: Main class for the two-player game.TicTacToeAI: Main class for the AI version.Minimax: Companion class forTicTacToeAIthat implements the Minimax algorithm.BtnActionListener(inTicTacToeAI): ActionListener for button clicks.showMessage(inTicTacToeAI): Runnable to display the game result.ButtonActionListener(inTicTacToeClassic): ActionListener for button clicks.showWinDialog(inTicTacToeClassic): Runnable to display the game result.
- Minimax Algorithm Explanation: Algorithms Explained – minimax and alpha-beta pruning (Sebastian Lague)
- Improved UI: Consider using more modern-looking UI elements and enhancing the overall visual appeal of the game.
- Difficulty Levels: Implement different difficulty levels for the AI opponent by adjusting the depth of the Minimax search.
- User Interface: Consider adding a menu for choosing game modes (two-player or AI) and potentially customizing game settings.
- SFX: Implement different sound effects when an event happens.
- I have learned how to implement, think outside the box, and search for resources online .
- Algorithms made me think of other ways to create a simple mechanism. It makes you think intuitively. For example, my logic for checking if a game state is in a draw.