CS 160, Spring 2014
Programming Assignment P4
Searching for Java

Programming due Monday, Feb. 17 at noon; late deadline Feb. 17 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. See your code control user interface.
  4. Use control loops to manage movement in the Maze.

Description

This assignment features you, the student, as the star of the show. The goal of this program is to move yourself around the maze according to a precise set of rules. If you follow the rules, you will find the Java logo . Note: You must follow the exact path we specify to receive full credit on this program, finding the Java logo is not enough!

Instructions

In Recitation 5 you should have started on P4.java. If not, follow the directions here. Leave in the code you wrote that instantiates the Maze object and retrieves the dimensions. Remove any code from the recitation that moves the student around in the maze. Then add code to implement the algorithm shown below. This will require multiple control loops, which can be either while or for statements. Here is a complete specification of the Maze methods you can call:
// Constructor, parameter is name of maze file
public Maze(String fileName)

// Get height of maze, in rows
public int getHeight()

// Get width of maze, in columns
public int getWidth()

// Move commands, returns true if move successful, false is blocked
public boolean moveRight()
public boolean moveLeft()
public boolean moveUp()
public boolean moveDown()

// Returns true when the student finds the java logo, false otherwise
public boolean isDone()

Algorithm

  1. The student always starts at the top left corner, that is row and column zero.
  2. The Java logo can be anywhere in the maze, which also contains obstacles shown as "Wrong Way" signs.
  3. You must traverse every row in the maze from top to bottom according to the rules below, until you find the Java logo.
  4. Row and column numbers are zero based, so the first row and column is index 0. the second row and column is index 1, and so on. The number zero is even.
  5. On even rows, you must move left to right using maze.moveRight(), on odd rows, you must move right to left using maze.moveLeft().
  6. After completing each row, use maze.moveDown() to proceed to the next row, until you reach the last row or find the Java logo.
  7. You can detect that you have encountered an obstacle by checking the return value from move methods in the Maze object, true means no obstacle, false means obstacle.
  8. If you run into an obstacle when when moving left to right:
  9. If you run into an obstacle when when moving right to left:
  10. Every time you move left or right, not including when avoiding an obstacle, you must call maze.isDone() to see if you have found the Java logo.
  11. When you find the Java logo, you must immediately break out of all loops, and exit the program.
  12. There are mazes that cannot be solved using the algorithm, but we will not test your program with any of them.
The Maze is programmed to wait 0.5 seconds each time you move the student, so you can issue move calls back to back, and the student will move at a reasonable speed that allows you to see the moves. In addition to checking visually, the move methods will print the row and column of the student, and you can use this to debug your code. For example, here is the output of moving the student from the top left corner right three spaces.

Maze name: Maze1.txt
Maze width: 10
Maze height: 5
Moved to row 0, column 1
Moved to row 0, column 2
Moved to row 0, column 3

Testing

You should test your code with the five Mazes provided, and you can also make your own mazes. The format of a maze file is shown below. The first line is an integer specifying the height (number of rows), the second line is an integer specifying the width (number of columns), followed by one line for each row of the maze, with one character per column. The value 'S' is the student, 'J' is the Java logo, 'D' is a wrong way sign, and '-' is empty.
5
10
S--------J
--D---D---
--D----D--
----D-----
--D----D--

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).

© 2014 CS160 Colorado State University. All Rights Reserved.