### Homework 9: 2-dimensional arrays

• Introduction

• This homework is intended to reinforce the following concept:

 2-dimensional arrays

• Preparation

• Make directory and copy files:

 ``` mkdir ~/cs170/hw9 cp ~cs170002/share/hw9/*.java ~/cs170/hw9 cd ~/cs170/hw9 ```

Files:

 ``` MyCanvas.java = program to draw a picture stored inside a 2-dim. array (do not change this program) PlotSin.java = you need to write statements in this program to draw a sin plot TestTTT.java = program to test the TicTacToe.java program (do not change this program) TicTacToe.java = you need to write the "checkBoard" method inside this program to verify if a Tic Tac Toe game has exactly one winning combination ```

• Part 1: make a sin-plot

• We have seen in class how to use the MyCanvas.java methods to draw a picture contains in a 400x300 2-dimensional array.

If you need to review the material, here is the webpage: click here

• Sin plot assignment:

• Draw the following sin-plot (plot of the sin function) in a 400x300 canvas:

• Hints:

• The sin function is in the Java library class: Java.lang.Math

This will compute the sin of a number x: Math.sin(x)

• The constant π (= 3.1415926353...) is in the Java library class: Java.lang.Math

The name of the π constant is: Math.PI

• To compute sin(x), you must use x values in the range [0 .. 2*π]

BUT.... in order to plot the sin-curve on a 400x300 canvas, you must use column values in the range of values [0 .. 399]

Therefore, you must map the range of values [0 .. 2*π] to the range of values [0 .. 399]

Use a multiplication factor to scale.

The best way to do this is:

 use an col variable to run through the range of values [0 .. 399], for each column value, convert it to an appropriate value between [0 .. 2*π].

• The height of the sin-plot must be 100 pixels

The range of the sin function is [-1 .. 1].

In order to plot on a 400x300 canvas, you must also scale the range of values [-1 .. 1] to the range of values [-100 .. 100]

Use a multiplication factor to scale.

After scaling, you must transpose (= move down) the plot by 150 pixels to place it in the center of the canvas.

• Debugging hint

• You will most likely get array bound out of range errors.

So, to give you an idea why your program get this error, you should add a print statement before you perform any assignment to the 2-dimensional array variable a

• What I mean is this:

 ``` System.out.println( "col = " + col + ", row = " + row ); a[col][row] = ....' ```

When you see a col index that is smaller than 0 or larger than 399, you will get an array index out of bound error.

When you see a row index that is smaller than 0 or larger than 299, you will also get an array index out of bound error.

The printing out of the col and row variables will help you figure out why you have a logic error.

• NOTE:

• The sin plot that you are making:

is in fact a flipped sin plot

• That is because when the sine value increases, we will draw the point in a row with a larger row index, which will go down wards (instead of "upwards" when a value increases).

• Part 2: checking out a Tic Tac Toe result

• We will use a 2 dimensional array of char to respresent the outcome of a Tic Tac Toe game

Example:

 ``` Tic Tac Toe game board: 2-dim array representation: +---+---+---+ char[] a = new char[3][3]; | X | O | X | +---+---+---+ a[0][0] = 'X'; a[0][1] = 'O'; a[0][2] = 'X'; | O | O | X | a[1][0] = 'O'; a[1][1] = 'O'; a[1][2] = 'X'; +---+---+---+ a[2][0] = 'O'; a[2][1] = 'X'; a[2][2] = 'X'; | O | X | X | +---+---+---+ ```

• Also, we generalize the Tic Tac Toe game so that we can have arbitrary large Tic Tac Toe game boards.

Example: a 4x4 Tic Tac Toe game board

 ``` Tic Tac Toe game board: 2-dim array representation: +---+---+---+---+ char[] a = new char[4][4]; | X | O | X | O | +---+---+---+---+ a[0][0] = 'X'; a[0][1] = 'O'; a[0][2] = 'X'; a[0][3] = 'O'; | O | O | X | O | a[1][0] = 'O'; a[1][1] = 'O'; a[1][2] = 'X'; a[1][3] = 'O'; +---+---+---+---+ a[2][0] = 'X'; a[2][1] = 'X'; a[2][2] = 'O'; a[2][3] = 'O'; | X | X | O | O | a[3][0] = 'X'; a[3][1] = 'X'; a[3][2] = 'X'; a[3][3] = 'O'; +---+---+---+---+ | X | X | X | O | +---+---+---+---+ ```

• 2-dim. array assignment

• Write a checkBoard method inside the program class file TicTacToe.java that:

 receives an 2 dimensional array as input parameter (that represents the outcome of a Tic Tac Toe game) returns as output the value true if the Tic Tac Toe game has a winner (does not matter if 'O' or 'X' wins) returns as output the value false if the Tic Tac Toe game does not have a winner or it has multiple winning combinations

• Testing:

• I have provided a test program named TestTTT.java that test a number of winning and non-winning Tic Tac Toe outcomes.

• To compile and run the test, do:

 ``` javac TestTTT.java java TestTTT ```

The correct result is as follows:

 ``` Test 1: column win O X O X X O O X X Winning Tic Tac Toe board: true --- correct Test 2: row win O X X O X X X O O O O O O X X X Winning Tic Tac Toe board: true --- correct Test 3: diagonal 1 win O X O X O X O X X Winning Tic Tac Toe board: true --- correct Test 4: diagonal 2 win X O O O O X X O X O X O O X X X Winning Tic Tac Toe board: true --- correct Test 5: no win X O O O X O X O O X X O O X X X Winning Tic Tac Toe board: false --- correct Test 6: multiple row wins X O X O O O O O X X X X O X X O Winning Tic Tac Toe board: false --- correct ```

• Hints:

 You will need a nested loop to find winning combinations in rows (and one in columns). You need a non-nested loop to find winning combinations in a diagonal (one loop for each diagonal). To find out if a row (or column or diagonal) contains the same symbol, compare symbols in the other position against the symbol in the first position They must all be equal... Count the number of winning combinations in the Tic Tac Toe board If the number of winning combinations is equal to 1, you have a winning result. Anything else, is not a winning result.

• Turnin

• Turn in your PlotSin.java and TicTacToe.java program file as follows:

 ``` cd ~/cs170/hw9 /home/cs170002/turnin-hw PlotSin.java hw9 /home/cs170002/turnin-hw TicTacToe.java hw9a ```