Teacher’s Notes:
=======================================
This is a multi-part assignment that is broken into chunks. In the first part, the student will make the basic parts of a tic tac toe game. In the next part (coming), they will make the a very basic computer opponent.
The wikipedia link in the assignment links to an explanation of the game from wikipedia.
It will look better if you format the document below by setting the sample outputs to the font called "Courier New". Also, the function/method header at the bottom should be in the Courier New.
Assignment:
=======================================
Tic Tac Toe Part 1:
In this project we are going to create a simple Tic Tac Toe game. In the game of Tic Tac Toe, one player is assigned the letter X and the other gets the letter O. The players play on a 3x3 grid and take turns placing their symbol into one space of the grid. A symbol may not be placed on a spot where another symbol is already placed. Whenever a player places their symbol, the board is checked to see if their is a row, column, or diagonal of their symbols on the grid. If there is, that player has won the game. If not, the game continues. When the board is filled with symbols and no winner declared, the game is considered to be a tie. In the image below, the player with the O symbol has won.
https://en.wikipedia.org/wiki/Tic-tac-toe
To get started, you will:
- Write a program that creates a 3 x 3 array of chars.
- Populate the array with spaces in all nine elements using a nested loop.
- Write a method that will display the array in a neat board like configuration. The empty spots of the board will be displayed with a number indicating their position as shown below. If the spot has been assigned an X or an O, it will display that symbol instead. The following is an example of an empty board:
1 | 2 | 3
—--------
4 | 5 | 6
—--------
7 | 8 | 9
The following is an example of a board that has been some spots assigned:
X | 2 | 3
—--------
4 | O | O
—--------
7 | 8 | X
- Complete the following method. In this method, char[][]board is the 2D array for the board. char symbol is the X or the O that is to be placed on the board. This method will prompt the user to choose a spot on the board to make their move (1-9 as shown above). If the spot is already occupied, it will prompt the player to make another choice (and repeat this until they choose an empty spot). If the user chooses an invalid spot, the program will prompt again. Once an empty spot is chosen, the element corresponding to the number will be assigned to be symbol.
public static void makeMove (char[][] board, char symbol) {
}
Solution (JAVA):
=======================================
import java.util.Scanner;
public class TicTacToe {
public static void initialize(char[][] board){
for(int x = 0; x < 3; x++)
for(int y = 0; y < 3; y++)
board[x][y] = ' ';
}
public static void display(char[][] board){
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
if(board[x][y] == ' ')
System.out.print(" " + (3*x+y%3+1) +" ");
else
System.out.print(" " + board[x][y] +" ");
if(y != 2)
System.out.print("|");
}
System.out.println();
if(x != 2)
System.out.println("----------");
}
}
public static void makeMove(char[][]board, char symbol) {
int spot = 0;
boolean symbolPlaced = true;
do {
symbolPlaced = true;
do
{
System.out.print("Where would you like to place your " + symbol + ":");
spot = input.nextInt();
}while(spot < 1 || spot > 9);
int x = (spot-1)/3;
int y = (spot-1)%3;
if(board[x][y] == ' ')
board[x][y] = symbol;
else
symbolPlaced = false;
}while(!symbolPlaced);
}
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
char[][] board = new char[3][3];
initialize(board);
display(board);
makeMove(board,'X');
display(board);
makeMove(board,'O');
display(board);
}
}