# teaching machines

## CS 145 Lecture 29 – Arrays in Two Dimensions

November 16, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

We’ve seen that we can create an array like this:

type[] id = new type[#];


That type can be anything, like int, String, double, boolean, BufferedImage, and so on. It can also be another array:

type[][] id = new type[#][];


Just as 1D arrays let us use one integer to map into a line of data, an 2D lets us use two integers to map into an area of data. We can do things like this:

String[][] teams = new String[nteams][3];

// Team A
teams[0][0] = "Jimmy Chonga";
teams[0][1] = "Jen Sing";
teams[0][2] = "Bill Dollar";

// Team B
teams[1][0] = "Bill Ding";
teams[1][1] = "Paula Ticks";
teams[1][2] = "Ann Ticks";


The beauty of 2D arrays is that we can organize data by two schema. Often these schema are spatial axes, but that is not a requirement.

We will explore 2D arrays by writing a simplified version of Battleship. But let’s ditch the war theme and trademark. We will call it Scatterpillar. It’s our job to find all the caterpillars scattered across a 2D Cartesian plane.

See you next class!

Sincerely,

P.S. Here’s the code we wrote together…

#### Teams.java

package lecture1116;

import java.util.Random;

public class Teams {
public static void main(String[] args) {
String[][] teams = new String[2][3];

teams[0][0] = "Bill Ding";
teams[0][1] = "Paula Ticks";
teams[0][2] = "Will Powers";

teams[1][0] = "Bill Dollar";
teams[1][1] = "Jimmy Chonga";
teams[1][2] = "Justin Thyme";

faceOff(teams);
}

private static void faceOff(String[][] teams) {
// pick a player from team 0
// pick a player from team 1

int nteams = teams.length;

Random generator = new Random();
int i = generator.nextInt(teams[0].length);
String player0 = teams[0][i];

i = generator.nextInt(teams[1].length);
String player1 = teams[1][i];

System.out.println(player0 + " vs. " + player1);
}
}


#### Scatterpillar.java

package lecture1116;

import java.util.Random;
import java.util.Scanner;

public class Scatterpillar {
public static void main(String[] args) {
char[][] board = getNewBoard(5, 4);
placeCaterpillar(board, 4);
printBoard(board);

Scanner in = new Scanner(System.in);

while (hasUnhit(board)) {
System.out.println();
System.out.println("Where's that caterpillar? ");
int c = in.nextInt();
int r = in.nextInt();

if (board[r][c] == 'o') {
board[r][c] = '!';
System.out.println("You find!!!!");
} else {
board[r][c] = 'x';
System.out.println("Not yet. But you'll get it. Just try harder.");
}
System.out.println();

printBoard(board);
}

System.out.println();
System.out.println("Find all done!");
}

private static boolean hasUnhit(char[][] board) {
for (int r = 0; r < board.length; ++r) {
for (int c = 0; c < board[r].length; ++c) {
if (board[r][c] == 'o') {
return true;
}
}
}
return false;
}

private static void placeCaterpillar(char[][] board, int length) {
Random gen = new Random();
if (gen.nextBoolean()) { // vertical
int c = gen.nextInt(board[0].length);
int r = gen.nextInt(board.length - length + 1); // if height is 10 and
// length is 3, then range
// is [0, 7]
for (int i = 0; i < length; ++i) {
board[r + i][c] = 'o';
}
} else { // horizontal
int r = gen.nextInt(board.length);
int c = gen.nextInt(board[0].length - length + 1); // if width is 10 and
// length is 3, then
// range is [0, 7]
for (int i = 0; i < length; ++i) {
board[r][c + i] = 'o';
}
}
}

public static char[][] getNewBoard(int width, int height) {
char[][] board = new char[height][width];

for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
board[r][c] = '.';
}
}

return board;
}

public static void printBoard(char[][] board) {

System.out.print(' ');
for (int c = 0; c < board[0].length; ++c) {
System.out.print(c);
}
System.out.println();

for (int r = 0; r < board.length; ++r) {
System.out.print(r);
for (int c = 0; c < board[r].length; ++c) {
if (board[r][c] == 'o') {
System.out.print('.');
} else {
System.out.print(board[r][c]);
}
}
System.out.println();
}
}
}