# teaching machines

## CS 148: Lab 10 – Arrays

Welcome to lab 10!

If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.

In this lab you will work with data that has been collected into arrays. Arrays let us number our data, making it easy to loop through the collection or associate data with the natural numbers.

### Checkpoint 1

Person A types.

Suppose you have an array of 10 String names. You want to select 5 of them for a drug test. Repeat subjects are not allowed. Write a method pick5 that accepts an array containing the 10 names as a parameter and prints 5 unique names drawn at random. Do not use ArrayList.

### Checkpoint 2

Person B types.

Watch this clip of Link’s Awakening, in which the player solves a puzzle by filling in the floor. Also, watch a few moments of this clip of Machinarium, a beautifully illustrated point-and-click puzzle game. Write a program that lets the user trace out a space-filling path a bit like you see in the clips. Our path will be more like Machinariums—we’ll only let the player change direction when the current segment comes to a halt.

Let’s break this down into a few steps:

• Create a main method in class Fill that defines a 2D char array. Mark unvisited, empty tiles with a . and obstacles with an x. You might want to start with no obstacles as you develop your code.
• Define the player’s starting coordinates using two ints that are static. In main, mark the player’s starting tile on the board as visited by assigning it *.
• Write method show, which accepts a 2D char array as its sole parameter. It prints the board to the console mostly as is, except the player’s position within the board is marked with a ?.
• Write method isFilled, which accepts a 2D char array as its sole parameter. It returns true if no empty tiles remain and false otherwise.
• Back in main, repeatedly show the board and ask the player for a direction command—one of n, e, s, or w. Advance the player along the given direction using the helper method you write in the next step. Keep asking and advancing until the board is filled.
• Write method advance, which accepts as parameters a 2D char array, an int x delta, and an int y delta. The delta values specify which direction to head. Each will be one of [-1, 0, 1]. The method walks the player along the direction specified by the delta values as long as there are empty, unvisited tiles. Follow this pseudocode:
while the proposed next tile is on the board and is a '.'
move the player to the proposed next tile
mark the player's tile with '*'
Back in main, call this method with delta values appropriate for the direction the user typed in. For example, suppose the player typed e for east. What is the y delta? The x delta?

Add some obstacles into your board and invite a classmate to have a go! But be careful. Not all board configurations are solvable.