Java || Simple Tic Tac Toe Game Sample Code GUI

Print Friendly, PDF & Email

The following is another homework assignment which was presented in an intro to Java class. This program was used to provide more practice creating a graphical user interface.


How To Play Tic-Tac-Toe
Special Fonts For This Game - Click Here To Download
Executable .Jar File - Click Here To Download & Play

This program features a graphical user interface (GUI) which has nine buttons arranged in a square. A user plays the game by clicking the mouse in a available button. The game then places a symbol on the clicked button alternating between the letters “X” and an “O.”

The game begins by clicking on the “New Game” button. This action will clear away any text on the 9 game buttons. The 9 game buttons are still disabled, but the radio button is enabled. This means someone must select who plays first: “X” or “O.” After selecting the starting player then the 9 game buttons become enabled.

When a player clicks on one of the 9 available game buttons, the text on that button will change into an “X” or an “O.” The game stops when one of the two players has 3 identical symbols in a row, a column, or a diagonal. When there is such a winner, the symbol of the winner is displayed to the screen. Sometimes all 9 game buttons have been clicked on but there is still no winner. In that case the text “Tie” is displayed to the screen.

The two buttons “New Game” and “Quit” remain enabled at all times. Clicking on “Quit” will close the window.

This program was implemented into 3 different files, so the code for this program will be broken up into 3 sections. The three-file solution always has one file (the driver) which contains the “main” function. Another file contains the implementation of the graphical user interface (GUI), and the last file contains logic, which in this case is the game algorithms.

==== File #1 – ====

==== File #2 – ====

This is the GUI (graphical user interface) class for a three-file solution which implements the game of tic tac toe. The sole purpose of this source code is to define the GUI and call methods in the BusinessLogic class when needed.

==== File #3 – ====

This is the BusinessLogic class for a three-file solution which implements the game of tic tac toe. The is the class containing algorithms.

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

This program uses custom fonts. To obtain those fonts, click here!

Click here to download & play the executable .jar file.

(click to enlarge)

Was this article helpful?
👍 YesNo

Leave a Reply