Java Programming Challenge: A Simple Tic-Tac-Toe Game
Java Programming and Loop Iterations
10 Useful Eclipse Tricks

Java Programming Challenge: Adding a GUI to the Tic-Tac-Toe Program

In Java Programming Challenge: A Simple Tic-Tac-Toe Game you were challenged to write a console-based program that plays the simple game of Tic-Tac-Toe. The Java programming challenge here is to use Swing to add a Graphical User Interface (GUI) to the program. The GUI should resemble the one shown here.

The user interface for a Tic-Tac-Toe program.
The user interface for a Tic-Tac-Toe program.

The game play is simple. The human player plays first by clicking any of the squares. The square clicked by the human displays a large X. After the human plays, the program determines if the human has won or forced a draw. If so, a message is displayed, the board is reset, and a new game begins. If not, the computer chooses a move and marks its square with a large O.

The program then determines if the computer has won the game. If so, the program displays a message, resets the board, and starts a new game. If not, the human player plays again. This continues until one player wins or all of the squares are filled.

Your program should use the same TicTacToeBoard class you create for Java Programming Challenge: Adding Class to the Simple Tic-Tac-Toe Program. In other words, it must implement the exact same methods. For your convenience, these methods are repeated in the following table.

The TicTacToeBoard Class
TicTacToeBoard Creates a new TicTacToeBoard with all squares empty.
void reset() Resets the status of each square to empty.
void playAt(String square, int player) Marks the specified square (A1, A2, A3, B1, B2, B3, C1, C2, or C3) for the specified player (1 for X, 2 for O). Throws IllegalArgumentException if square is not one of the allowable values, player is not 1 or 2, or the specified square is not empty.
int isGameOver() Determines whether the game is over. Returns 0 if the game is not over, 1 if X has won the game, 2 if O has won the game, and 3 if the game is a draw. The game ending conditions are as follows:
1: If any row, column, or diagonal contains all X's.
2: If any row, column, or diagonal contains all O's.
3: If there are no empty squares and neither X nor O has won.
int getNextMove() Returns an integer representing the next move for the computer opponent. This method should make a rudimentary effort to select a good move, according to the following strategy:
* If the center (square B2) is empty, play the center square.
* If the center is not empty but any of the four corners (squares A1, A3, C1, or C3) are empty, play one of the corners (it doesn't matter which).
* If the center is not empty and no corners are empty, play one of the edges (squares A2, B1, B3, or C2).
String toString() Returns a string that represents the current status of the board. The string includes new-line characters to display the rows as well as separator lines on separate console lines, as in this example:
O | | O
| X |
| X |

You'll find the solution to this challenge on the Downloads tab of the Java All-in-One For Dummies, 4th Edition product page.

Good luck!

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Java All-in-One For Dummies Cheat Sheet
Java Programming Challenge: Creating a Simple Turing Machine
Java Programming Challenge: Doing Tic-Tac-Toe as an Applet
Java Programming Challenge: Saving Tic-Tac-Toe
Java Programming Challenge: Adding Arrays to the Simple Tic-Tac-Toe Program