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 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.
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.
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.
* 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).
You’ll find the solution to this challenge on the Downloads tab of the Java All-in-One For Dummies, 4th Edition product page.