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:
  1. Instantiate an object and call its methods.
  2. Use loops and conditional statements.
  3. 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:
  1. Declare a string variable to hold the command from the user.
  2. Declare a character variable to hold the first letter of the command.
  3. Declare a boolean variable to hold return values from move commands.
  4. Initialize the boolean variable to false.
  5. Inside a while (true) loop do the following.
  6. Print "Command: " as a prompt to the console.
  7. Read a string from the user with the scanner and store it.
  8. Extract the first character of the string using the charAt method and store it.
  9. Write switch statement for the character variable with the following cases: 'l','r','u','d','s','q'.
  10. Your program should print "Invalid command" if any other command is entered.
  11. Each case should have a break after the code that processes the command.
  12. The 'l' command should call the moveLeft() method on the maze.
  13. The 'r' command should call the moveRight() method on the maze.
  14. The 'u' command should call the moveUp() method on the maze.
  15. The 'd' command should call the moveDown() method on the maze.
  16. For all of the above commands, store the return value in the boolean declared above.
  17. The 's' command should implement a for loop to move Dory, see the details below.
  18. The 'q' command should exit immediately by calling System.exit(0).
  19. After the switch statement, print "Successful" or "Failed" based on the boolean.
  20. 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:

Grading Criteria

NOTE: We will test your program with a different maze than the one provided!
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.