CS 160, Spring 2013
Programming Assignment P3
Help Dory to find Nemo!
Programming due Monday, Feb. 18 at noon; late deadline Feb. 18 at 10 p.m.
A Program that Controls a Fish in a Maze
This lab has the goal of teaching you how to:
- Instantiate an object and call its methods.
- Use loops and conditional statements.
- Control an object in a visual environment.
Description
The purpose of this program is to 1) display the maze with Dory and Nemo and
other hazards, 2) use a precisely specified algorithm to move Dory from her
initial position to Nemo, while 3) avoiding sharks and jellyfish. A picture
of the easy maze we provide is shown below:
Instructions
You must complete Recitation 3 in the lab before working on this assignment,
since you will create the program P3.java in the lab as a starting
point. Some additional files are needed, and you will copy these into your
workspace during the recitation, including
Maze.java,
easyMaze.txt,
hardMaze.txt,
and the image files
Nemo.jpg,
Dory.jpg,
Both.jpg,
Shark.jpg, and
Jelly.jpg.
The .jpg image files, easyMaze.txt and hardMaze.txt must be copied to
~/workspace/P3, and the Maze.java source file must be copied to
~/workspace/P3/src. NOTE: Do not modify any files other than your
own P3.java!
Leave in the code that prompts for the filename and instantiates the maze object and
prints the maze width and height. Remove the maze.moveUp() and maze.moveRight() commands
you put in during R3. Next implement a command processing loop similar to the one in
the ATM program you wrote in R3. Here are detailed instructions:
- Declare a string variable to hold the command from the user.
- Declare a character variable to hold the first letter of the command.
- Declare a boolean variable to hold return values from move commands.
- Initialize the boolean variable to false.
- Inside a while (true) loop do the following.
- Print "Command: " as a prompt to the console.
- Read a string from the user with the scanner and store it.
- Extract the first character of the string using the charAt method and store it.
- Write switch statement for the character variable with the following cases: 'l','r','u','d','s','q'.
- Your program should print "Invalid command" if any other command is entered.
- Each case should have a break after the code that processes the command.
- The 'l' command should call the moveLeft() method on the maze.
- The 'r' command should call the moveRight() method on the maze.
- The 'u' command should call the moveUp() method on the maze.
- The 'd' command should call the moveDown() method on the maze.
- For all of the above commands, store the return value in the boolean declared above.
- The 's' command should implement a for loop to move Dory, see the details below.
- The 'q' command should exit immediately by calling System.exit(0).
- After the switch statement, print "Successful" or "Failed" based on the boolean.
- No code is required after the while loop for command processing.
To implement this progam, use the move functions provided by the maze object.
These methods have no parameter and return a boolean. The return
value is true if Dory can move and false if she cannot. The reasons for failure
of a move command are that Dory either hit the edge of the maze, or there is
a shark or jellyfish where you are trying to move.
NOTE: Dory is programmed to wait 0.5 seconds each time you move it,
so you can issue move calls back to back, and Dory will move at a
reasonable speed that allows you to see what is happening.
You can get 90/100 on this assignment by implementing the 's' command as a
simple loop that moves Dory from the left column of the maze to the right column.
The easyMaze.txt data file tests this simple algorithm by not putting
any obstacles in the way. To get 100/100 on this assignment you need to
implement a more complex algorithm as follows. When Dory encounters an obstacle,
you must move up and right one square, then continue in the loop that moves
from left to right. The hardMaze.txt data file tests this algorithm
by putting single obstacles in the way of Dory. Think about what would be
required to pass more complex mazes.
Testing
You should test your code by selecting the 's' command with the maze provided
in easyMaze.txt. If you implement the more complex algorithm, also test with
the maze provided in hardMaze.txt. In both cases, the Maze.java code should
print "Dory found Nemo!" if your algorithm is correct.
You can also make other mazes, but be aware that even the more complex algorithm will
not always find Nemo. We will test with other unpublished mazes on which we have
made sure the simple and more complex algorithm work, so do not hard code the size
of the maze in your loops.
Specifications
Your program must meet the following specifications:
- Dory must move exactly as specified.
- Work on your own.
- The name of the source code file must be exactly P3.java
- Name it exactly: upper/lower case letters are important!
- Comments at the top with your name, e-Name, date and course.
- The output from your program is checked, so make sure your program
does not print anything else when you turn it in.
- We expect programming assignments to be implemented in Eclipse
using Java 1.5 or 1.6 or 1.7 (1.4 or less will NOT work).
- We will be testing the code on the machines in the CS computer lab,
so make sure your code runs on those machines.
- We will be checking programs for plagiarism, so please don't copy
from anyone else.
Grading Criteria
NOTE: We will test your program with a different maze than the one provided!
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- -5 points for each move command ('l', 'r', 'u', 'd') that does not work.
- -10 points if the 's' command fails on easyMaze.txt.
- -10 points if the 's' command fails on hardMaze.txt.
- -10 points if the 's' command does not work on a different size maze.
Submit your program to RamCT as you were shown in the recitation, and read the syllabus for the late
policy (if necessary).
© 2012 CS160 Colorado State University. All Rights Reserved.