Programming Assignment 6: P6 CS 163/164, Spring 2019 Programming Assignment - P6 Maze Program Due Monday, Mar. 4th at 6:00 pm Late Tuesday, Mar. 5th at 8:00 am Objectives of this Assignment This lab has the goal of teaching you how to: Instantiate a Maze object and call its methods, see your code controlling a graphical user interface, and use control loops to manage movement in the Maze. Description This assignment features Firefly and Mal from the space western Firefly, the best TV series ever made. Firefly is constantly having to save Mal from the gorram Reavers. The goal of this program is to move Firefly around the maze according to a precise set of rules. If you follow the rules, Firefly will find Mal, and will never meet the Reavers. Note: You must follow the exact path we specify to receive full credit on this program, finding Mal 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/. Firefly.png Mal.png Reavers.png Success.png Maze1.txt Maze2.txt Maze3.txt Maze4.txt Maze5.txt Maze6.txt HardMaze.txt Maze.java A tree view of the P6 directory should look like this: P6/ ├── Firefly.png ├── Mal.png ├── Reavers.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: Modify the run configuration to pass Maze1.txt to the program. Test your program by calling maze.moveRight() to make Firefly move right one square. Exercise 1: The maze object has similar methods to move down, left, and up, try calling each of them. Exercise 2: Try writing a for or while loop to move Firefly from left to right across the top row. Exercise 3: Run Firefly around the outside of Maze6.txt, in a clockwise direction. Exercise 4: Run Maze5.txt and see what happens when you run into the Reavers. Note: In order to detect the Reavers, you must check the return value from the move methods. Instructions 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 Firefly 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 Firefly
public int getColumn();
public int getRow();
// Commands to move Firefly
public boolean moveRight();
public boolean moveLeft();
public boolean moveDown();
public boolean moveUp();
Algorithm The mazes can be of any size. Firefly always starts in the top left corner. Mal can be anywhere, as specified by the maze file. Reavers can be anywhere, as specified by the maze file. If you follow the rules, Firefly will never meet the Reavers, and will find Mal. Row and column numbers are zero based, so the first row and column is index 0. Here is the exact algorithm for finding Mal: On even rows, Firefly moves from left to right. On odd rows, Firefly moves from right to left. When Firefly reaches the right side on an even row, it moves down. When Firefly reaches the left side on an odd row, it moves down. When Firefly successfully moves, the Maze move methods return true. When Firefly would run into the Reavers, the Maze move methods return false. When the move method returns false, you must pull a "Crazy Ivan" to make Firefly evade the Reavers. "Crazy Ivan" when moving right means move down, right, right, then up. "Crasy Ivan" when moving left means move down, left, left, then up. Hint: You can use an outer loop to run through all rows. Hint: You can use an inner loop to move from left to right. Hint: You can use a separate inner loop to move from right to left. Hint: You can decide between the loops by checking the row modulo 2. Hint: Always move down after completing a loop. If Firefly goes outside the maze, the maze will print an error and exit. Firefly cannot run into the Reavers, the maze will not allow it. When Firefly finds Mal, the maze will print congratulations and exit. 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 1.0 seconds each time you move Firefly, so you can issue move calls back to back, and Firefly 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 Firefly, and you can use this to debug your code. This is also used to test your code. For example, here is the output of Firefly 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
Firefly found Mal, congratulations!
Testing 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 'F' is Firefly, 'M' is Mal, 'R' is Reavers, and '-' is empty.
5
6
F----M
--R---
----R-
-R----
---R--
Specifications Your program must meet the following specifications: Firefly must follow the exact route specified. Work on your own, as always. The name of the source code file must be exactly P6.java. Name the file exactly - upper and lower case matters! Assignments should be implemented using Eclipse. Assignments should be implemented using Java 1.8. Make sure your code runs on machines in the COMCS 120 lab. Submit your program to the Checkin tab as you were shown in the recitation. Read the syllabus for the late policy. We will be checking programs for plagiarism, so please don't copy from anyone else. Grading Criteria 100 points for perfect submission. 0 points for no submission, will not compile, submitted class file, etc. Preliminary Tests: compileTest: checks that P6.java program compiles with Maze.java. (0 points) test1: Firefly correctly solves Maze1.txt (10 points) test2: Firefly correctly solves Maze2.txt (10 points) test3: Firefly correctly solves Maze3.txt (10 points) test4: Firefly correctly solves Maze4.txt (10 points) test5: Firefly correctly solves Maze5.txt (10 points) test6: Firefly correctly solves Maze6.txt (10 points) Preliminary tests use mazes of different sizes, so do not hard code width and height. Final Tests: HardMaze: We will test with a large and devious maze called HardMaze.txt, which is provided to you! (40 points) Final grading includes the preliminary tests. Submit P6.java to Checkin.