Adam Kareen | Software Engineer

TicTacToe Java GUI

Development of a GUI-Based Tic Tac Toe Game in Java

Client / build for
JetBrains Academy
Desktop App Development
Main Technology
Live Preview

The purpose of this project was to create a graphical user interface (GUI) based Tic Tac Toe game in Java. The game includes three levels of difficulty, with artificial intelligence (AI) utilized at the 'Hard' level using the Minimax algorithm. The 'Medium' level simulates human-like gameplay, while the 'Easy' level makes random moves.

Software and Tools

The entire project was developed using Java, with the Swing library used to create the graphical user interface. The IntelliJ IDEA integrated development environment (IDE) was used for coding, debugging, and testing.

Game Design

The game was designed to have a simple and intuitive interface, with buttons for each cell on the 3x3 grid. A selection menu allows the user to choose the difficulty level before starting a game. The game keeps track of the player's and AI's scores, and it resets for a new game once a round is completed.

AI Implementation

Three AI difficulty levels were implemented:

1. Easy: The AI makes random moves. A list of available cells is created, and one is chosen randomly.

2. Medium: The AI behaves similarly to a human player. It uses a set of pre-determined strategies such as blocking the player from winning, or making a winning move if available. If no such moves are available, it makes a random move.

3. Hard: The AI uses the Minimax algorithm to determine its moves. This algorithm simulates all possible game scenarios and chooses the move that minimizes the maximum possible loss (i.e., it assumes optimal play from the opponent). By using this algorithm, the AI is unbeatable when playing optimally.

GUI Implementation

The GUI was implemented using Java Swing. A JFrame was used as the main window, with a JPanel containing a 3x3 GridLayout for the game board. Each cell on the board is a button, and clicking on a button places a move in the corresponding cell.

To update the game state, an ActionListener was attached to each button. When a button is clicked, the ActionListener is triggered, updating the game board and checking if the game has been won. The AI's move is then computed and executed, and the game state is updated again.

Challenges and Solutions

One of the main challenges was implementing the Minimax algorithm efficiently. Tic Tac Toe has a relatively small game tree, but for more complex games, the Minimax algorithm can be computationally expensive. To overcome this, alpha-beta pruning was used to eliminate unnecessary branches of the game tree, improving the algorithm's efficiency.

Another challenge was making the game interface responsive while the AI is making its move. This was achieved by performing the AI computations on a separate thread, preventing the GUI from becoming unresponsive.


This project provided a practical demonstration of AI in games, showcasing how the Minimax algorithm can be used to create an unbeatable AI opponent. Furthermore, it demonstrated the power and versatility of Java for creating a user-friendly graphical interface, offering users a fun and engaging experience while playing the game. Future improvements could include implementing a learning algorithm that adapts to the user's play style, providing an even greater challenge.