CS 160, Fall 2013
Programming Assignment P4
Moving Chihiro in a Maze

Programming due Monday, Sep. 23 at noon; late deadline Sep. 23 at 10 p.m.


Moving in a Maze

This lab has the goal of teaching you how to:
  1. Read input from the user and perform error checking.
  2. Instantiate a Maze object and call its methods.
  3. Use control statements to manage movement in the Maze.
  4. See your code run in a visual environment.

Description

This assignment features Chihiro from Spirited Away, one of the finest animated films ever made. The purpose of this program is to 1) display Chihiro in the maze, in a location specified by the user, 2) determine whether the Chihiro is on the edge of the maze, 3) move Chihiro to the top edge if she did not start on an edge, and 4) move Chihiro in a counterclockwise direction around the edge of the maze, making sure that Chihiro traverses every edge location exactly once.

Instructions

In Recitation 5 you should have started on P4.java. If not, create a new project and class called P4. Copy the file Maze.java into the P4 source directory. Copy the file Chihiro.jpg into the P4 directory. If you were in R5, leave in the code you wrote in R5 that inputs and checks the initial location of Chihiro, and instantiates the Maze object. Otherwise you must copy the code from R5, which is found here. Remove any code that moves Chihiro around in the maze. Add the following code:
  1. Determine if Chihiro is on the edge of the maze and store the result in a boolean called onEdge.
  2. If Chihiro is not on the edge of the maze, use moveTo calls to move Chihiro directly to the top edge, one square at a time.
  3. Save the location of Chihiro now that she is on the edge, this is the ending position.
  4. Move Chihiro counterclockwise around the edge of the maze, one square at a time, until she returns to the ending position.
Chihiro is programmed to wait 0.5 seconds each time you move her, so you can issue moveTo calls back to back, and Chihiro should move at a reasonable speed that allows you to see where she moves. In addition to checking visually, the moveTo method will print where Chihiro moves, and you can use this to debug your code. For example, if the Maze has 4 rows and 5 columns, and starts in the third row and fourth column, you should see the following output:

Number of rows: 4
Number of columns: 5
Starting row: 2
Starting column: 3
Chihiro moved to 1,3
Chihiro moved to 0,3
Chihiro moved to 0,2
Chihiro moved to 0,1
Chihiro moved to 0,0
Chihiro moved to 1,0
Chihiro moved to 2,0
Chihiro moved to 3,0
Chihiro moved to 3,1
Chihiro moved to 3,2
Chihiro moved to 3,3
Chihiro moved to 3,4
Chihiro moved to 2,4
Chihiro moved to 1,4
Chihiro moved to 0,4
Chihiro moved to 0,3
NOTE: Row and column numbers are zero based, so the first column/row is index 0, the second column/row is index 1, the third column/row is index 2, and so on!

HINT: Try to implement the code so that there is only a single loop to move Chihiro around. Our solution requires less than 40 lines for all of the code that follows the Maze instantiation.

Testing

You should test your code with the following 9 test cases: In the case that Chihiro starts in the middle of the maze, make sure she moves to the top edge before traversing the maze. The location where she first reaches the top of the maze is the point to which she should return. In the case that Chihiro starts on the edge, make sure she returns to that same point. In all cases make sure Chihiro does exactly one loop around the edge, in the counterclockwise direction. After all test cases are working you should test your code again with a different size of maze. The Maze object constrains the number of rows and columns as follows:

4 <= number of rows, number of columns <= 8

Specifications

Your program must meet the following specifications:

Grading Criteria

Submit your program to the Checkin tab on the course website, as you were shown in the recitation, and read the syllabus for the late policy (if necessary).

© 2013 CS160 Colorado State University. All Rights Reserved.