# CS 1713 Week 13: Two-Dimensional Arrays and Tic Tac Toe

#### Objectives:

• Introduce two-dimensional arrays.
• Use two-dimensional arrays in a class.

#### Activities:

• Graphics Case Study 9: Tic Tac Toe

Two dimensional arrays :

• Are arrays of arrays.
• Represent tables and matrices.
• Syntax uses [row][column].
• Rows can have different number of items.
• Number of rows in x is x.length.
• Number of columns in the i-th row is x[i].length .

Example 3: Declare a variable called ticTac that holds a two-dimensional array of char. In a separate statement create a 3x3 array for ticTac.

`     char [][] ticTac;     ticTac = new char;`

Example 4: Set the entry of ticTac in the second row, third column to an 'x'.

`     ticTac = 'x';`
Array index values start at 0, even for 2-D arrays.

Exercise 1: Create a two-dimensional array called aMatrix with the following values in it:
```   6.0 4.5 3.0 1.0
7.8 5.3 3.2 1.8
6.2 3.7 7.8 9.9
```
Ans: The following code does explicit initialization of the array:
```   double [][] aMatrix = {{6.0, 4.5, 3.0, 1.0},
{7.8, 5.3, 3.2, 1.8},
{6.2, 3.7, 7.8, 9.9}};
```
Draw a picture of the array aMatrix.

Example 5: Add up all of the entries in aMatrix

```   double sum = 0;
for (int i = 0 i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
sum += aMatrix[i][j];
}
}
System.out.println("The sum is " + sum);
```

Example 6: Create a two dimensional array whose i-th row contains the integers from 0 to i. Assume the number of rows is n.

```     int [][] triangle = new int [n][];
for (int i = 0; i < n; i++) {
triangle[i] = new int [i+1];
for (int j = 0; j < triangle[i].length; j++)
triangle[i][j] = j;
}
```
Exercise 2: Rewrite Example 6 as a static method called createTriangular that takes a single integer parameter n and returns the triangular matrix.
Ans:
```   public static double [][] createTriangular (int n) {
int [][] triangle = new int [n][];
for (int i = 0; i < n; i++) {
triangle[i] = new int [i+1];
for (int j = 0; j < triangle[i].length; j++)
triangle[i][j] = j;
}
return triangle;
}
```

Exercise 3: Write a static method called printArray that outputs the entries in a two-dimensional array a of Object. Output each row on a separate line.
Ans:

```   public static void printArray(Object [][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
```

### Tic Tac Toe Design

Tic Tac Toe will have two main parts, the player and the game.
The responsibilities of the game include:
• Keeping track of the board
• Keeping track of the players and knowing whose turn it is
• Determiining when the game is over hand who won
The responsibilities of a player are:
• Displaying the board
• Determining the next move
The behavior of the game includes:
• Adding a user to the game
• Making a move
The behavoir of a player includes:
• Setting X or O.
• Setting the board
• Prompting for a move
• Displaying informational messages

Here is a formal description of the behavoirs of the game and player:

```public interface PlayerInterface {
public void setID(char id);
public void boardChanged(char[][] board);
public void makeAMove();
public void inform(String s);
}

public interface GameInterface {
public void move(int row, int col, PlayerInterface player);
public void joinGame(PlayerInterface player);
}
```
We will need to implement these interfaces as well as a driver to start things off.

We will use two versions of the player interface, one with a GUI for use by a human player, and one for a computer.

### Graphics Case Study 8: Using a two-dimensional array: Tic Tac Toe

Start by writing the ComputerPlayer.
Choose the first available move and have the computer play against itself.
Then try making a random move (among the available moves).

Then fill in some of the Game including joinGame and move. For now, accept all moves and play with one player.

Have the constructor of the ComputerPlayer take an extra integer parameter representing the level of play, e.g. 0=first available move. 1=random, 2=?
Try to think up some better algorithms.

Next finish the Game including a determineation of who won.

Next write the GuiPlayer.
Have it create a frame that looks something like: 