Java Programming Challenge: A Simple Tic-Tac-Toe Game
Your mission, should you decide to accept it, is to create a Java program that can play a game of Tic-Tac-Toe with the user.
As you probably know, Tic-Tac-Toe is a simple game usually played with paper and pencil. First, you make a simple 3 x 3 grid on the paper. Then two players alternate turns by marking Xs and Os in empty spaces on the grid. The first player who makes three of his or her marks in a horizontal, vertical, or diagonal row wins. If all of the spaces in the grid are filled before any player marks three in a row, the game is a draw.
Here are the rules and instructions for this challenge:
The computer plays against the human. The human moves first and is X. The computer is O.
The program should begin by displaying a short welcome message, and then should prompt the player for his or her first move. For example:
Welcome to Tic-Tac-Toe. Please enter your first move:
To designate the squares of the grid, use the letters A, B, and C for the columns and the numerals 1, 2, and 3 for the rows, like this:
A1 B1 C1 A2 B2 C2 A3 B3 C3
Thus, to place an X in the top-left square, the human player would enter the text A1 when the program prompts the player for a move.
After the human enters a move, the program should display the current status of the board on the console.
Use X to mark the human’s plays and O to mark the computer’s plays. Use vertical bar characters (found on the keyboard with the backslash character, just above the Enter key) and hyphens to draw the board in a simple grid.
For example, if the user has entered A1 as his or her first move, the program would display the following:
X | | ---|---|--- | | ---|---|--- | |
After the human’s move, the program determines its move, announces it to the user, displays an updated board, and then prompts for the user’s move.
For example, you might see this:
I will play A2: X | O | ---|---|--- | | ---|---|--- | | Please enter your next move:
Play continues until one player has scored three in a row or all squares have been filled with no winner. Your program must be able to determine whether either player has scored three in a row and won the game. (This is the most difficult part of this programming challenge.)
When the game is over, the program displays a message indicating the result of the game: “You beat me!” if the human player wins, “I beat you!” if the computer player wins, or “It’s a draw!” if the game ends in a draw.
The human and computer players can play in only those squares that are not already occupied by either player.
The program ends when the game is won by either player or the game is a draw. If you want to play again, you must run the program again.
Note that you are free to use any method you wish to determine how the computer should make its moves. I suggest you have the computer always play in the first empty square. This is obviously not the best way to play Tic-Tac-Toe, and you will have no trouble beating the computer every time you play. But choosing this simple playing strategy allows you to focus on other aspects of the programming, such as how to internally represent the grid and how to determine when a player has won the game or when the game has ended in a draw.
(In “Java Programming Challenge: Adding Arrays to the Simple Tic-Tac-Toe Program” you’re asked to come up with a better strategy for the program to determine its plays.)
Here is an example of the console display for a complete game:
Welcome to Tic-Tac-Toe. Please enter your first move: A1 X | | ---|---|--- | | ---|---|--- | | I will play at A2: X | O | ---|---|--- | | ---|---|--- | | Please enter your next move: B1 X | O | ---|---|--- X | | ---|---|--- | | I will play at A3: X | O | O ---|---|--- X | | ---|---|--- | | Please enter your next move: C1 X | O | O ---|---|--- X | | ---|---|--- X | | You beat me!
Here are a few hints to get you started:
The best way to represent the grid is with an array. For now, use nine variables, named A1, A2, A3, B1, B2, B3, C1, C2, and C3.
If you use the int type for the grid variables, you can then use 1 to indicate that a square contains an X and 2 to indicate that a square contains an O.
Eight possible rows can lead to a win. A player wins if any of the following combinations of squares are either 1 (for X) or 2 (for O):
A1 – A2 – A3 A1 – B1 – C1 A1 – B2 – C3 B1 – B2 – B3 A2 – B2 – C2 A3 – B2 – C1 C1 – C2 – C3 A3 – B3 – C3
There are two ways to determine whether the game is a draw. The first is to check all nine squares: If none of the squares is empty and neither player has won, the game is a draw. The second is to count the moves: If nine moves have been made and neither player has won, the game is a draw.
You can find a solution to this programming challenge on the Downloads tab of the Java All-in-One For Dummies, 4th Edition product page.