CSU Banner

CS 163/164, Spring 2018

Programming Assignment - P6

Maze Program

Due Monday, Feb. 26th at 6:00 pm

Late Tuesday, Feb. 27th at 8:00 am

Objectives of this Assignment

This lab has the goal of teaching you how to:
  1. Instantiate a Maze object and call its methods,
  2. see your code controlling a graphical user interface, and
  3. use control loops to manage movement in the Maze.


This assignment features Chihiro from the Miyazaki film Spirited Away, the best animated film ever made. Chihiro is having trouble in the spirit world, and needs the help of Haku. While looking for Haku in the maze, Chihiro needs to stay away from the witch Yubaba. The goal of this program is to move Chihiro around the maze according to a precise set of rules. If you follow the rules, Chihiro will find Haku, and will never meet Yubaba. Note: You must follow the exact path we specify to receive full credit on this program, finding Haku is not enough!

Getting Started

The setup for this assignment is complex enough that you may not want to do it on your own, so we have dedicated some time during Lab 9 for our TAs to help you to get started. P6 requires an additional Java file for the graphical user interface, in addition to several image and maze files. As usual, use Eclipse to create a P6 project and associated P6 class in P6.java. Copy the image files and maze files into workspace/P6/, and the Maze.java source file to workspace/P6/src/.

A tree view of the P6 directory should look like this:
├── Chihiro.png
├── Haku.png
├── Yubaba.png
├── Success.png
├── Maze1.txt
├── Maze2.txt
├── Maze3.txt
├── Maze4.txt
├── Maze5.txt
├── Maze6.txt
├── HardMaze.txt
├── bin/
└── src/
         └── Maze.java

Copy the following code into P6.java:

public class P6 {

    // Class variables
    public static Maze maze;
    public static int mazeWidth;
    public static int mazeHeight;

    public static void main(String[] args) {

        // Create maze
        String fileName = args[0];
        System.err.println("Maze name: " + fileName);

        // Get dimensions
        maze = new Maze(fileName);
        mazeWidth = maze.getWidth();
        mazeHeight = maze.getHeight();
        System.err.println("Maze width: " + mazeWidth);
        System.err.println("Maze height: " + mazeHeight);

        // Add code to move around maze

Once all files are in place, follow these steps:
  1. Modify the run configuration to pass Maze1.txt to the program.
  2. Test your program by calling maze.moveRight() to make the student move right one square.
  3. Exercise 1: The maze object has similar methods to move down, left, and up, try calling each of them.
  4. Exercise 2: Try writing a for or while loop to move Chihiro from left to right across the top row.
  5. Exercise 3: Run Chihiro around the outside of the Maze, in a clockwise direction.
  6. Exercise 4: Run Maze5.txt and see what happens when you run into Yubaba.
    Note: In order to detect Yubaba, you must check the return value from the move methods.


In Lab 9 you should have started on P6.java, if not please follow the directions above. Leave in the code you wrote that instantiates the Maze object and retrieves the dimensions. Remove any code from the recitation that moves Chihiro 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 width and height of maze
public int getWidth();
public int getHeight();

// Get location of Chihiro
public int getColumn();
public int getRow();

// Commands to move Chihiro
public boolean moveRight();
public boolean moveLeft();
public boolean moveDown();
public boolean moveUp();


The Maze is programmed to wait 1.0 seconds each time you move Chihiro, so you can issue move calls back to back, and Chihiro will move at a reasonable speed that allows you to see the moves. You can modify this time in the Maze.java file. Any other changes to the graphical user interface in Maze.java will void your warranty! In addition to checking visually, the move methods will print the row and column of Chihiro, and you can use this to debug your code. This is also used to test your code. For example, here is the output of Chihiro moving right along the top row then down to the second row.

Maze name: Maze5.txt
Maze width: 8
Maze height: 5
Moved to row 0, column 1
Moved to row 0, column 2
Moved to row 1, column 2
Moved to row 1, column 3
Moved to row 1, column 4
Moved to row 0, column 4
Moved to row 0, column 5
Moved to row 0, column 6
Moved to row 0, column 7
Chihiro found Haku, congratulations!


You should test your code with the six 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 maze width, followed by an integer with the maze height. These values are followed by one line for each row of the maze, with one character per column. The value 'C' is Chihiro, 'H' is Haku, 'Y' is Yubaba, and '-' is empty.


Your program must meet the following specifications:

Grading Criteria

Submit P6.java to Checkin.

CS Banner
CS Building