COMP 110-001 - Introduction to Programming (Spring 2009)

Program 3: MouseBrain

50 points

Assigned: Friday, February 20th

Due: Monday, March 2nd by 5pm


Problem Description

In a horrible scientific experiment gone wrong, the brain of the once ordinary mouse Alfred has been replaced with a miniature computer. You are plagued with the unfortunate task of reprogramming Alfred to scrounge for cheese so that he may survive in the fast-paced world of laboratory mazes. To make matters worse, Alfred has been blindfolded to ensure that his sense of smell has been unaffected by the brain surgery.

Your job is to program an algorithm into Alfred's brain that will allow him to find the cheese using only the power of scent. Your goal is to move him to where the scent gets stronger until he is reveling in a feast of cheese.


Part 1: Download the files

On the course webpage, you will find MouseMaze.java, MouseBody.java, Cheese.java, and MouseBrain.java. Download these files into the COMP110 directory you use with jGrasp. Modify the Program Header in *only* MouseBrain.java to reflect your name.


Part 2: Create the interface to MouseBrain.java

You will be adding code exclusively to the MouseBrain.java file. As it stands, you are unable to compile MouseBrain.java. You will first need to add two instance variables and five methods, as given below. The body of the accessor and mutator methods (the first four methods below) should be straightforward to code. Once you have created the first four methods, create a skeleton for the findCheese() method (a skeleton is method with an empty body).

MouseBrain
VisibilityVariable NameTypeDescription
-mouseBodyMouseBodyReference to the mouse's body
-nameStringStores the mouse's name
VisibilityMethod NameReturn TypeParameter(s)Description
+getMouseBodyMouseBodynoneGets the value of the variable mouse
+getNameStringnoneGets the mouse's name
+setMouseBodyvoidMouseBodySets the value of the variable mouse
+setNamevoidStringSets the mouse's name
+findCheesevoidnoneNavigates the mouse through the maze to find the cheese


Part 3: Compile and run

At this point, you should be able to compile the code and run it. Alfred will remain in one place even after clicking the "Get the Cheese!" button, because he has not been programmed to move, causing him to fail the maze:



In order to move towards the cheese, you will need to inform his body that you want it to move. Movement methods are available to you by interfacing with the mouse instance variable you have defined at the top. For example, inserting the following code into the findCheese() method:

for (int x=0;x<200;x++)
    mouseBody.moveRight();


will cause the Alfred to move right 200 pixels (or until he reaches the edge of the maze). Alfred will still fail the maze (unless you are incredibly lucky!)

Since you will want to move Alfred in all four directions, as well as check for the scent of cheese, the following methods are available to you to interact with the mouse body:
MouseBody
VisibilityMethod NameReturn TypeParameter(s)Description
+moveLeftvoidnoneMoves the mouse left one pixel
+moveRightvoidnoneMoves the mouse right one pixel
+moveUpvoidnoneMoves the mouse up one pixel
+moveDownvoidnoneMoves the mouse down one pixel
+sniffForCheesedoublenone Gets the power of the cheese scent, in a range from
0.0 to 1.0 (inclusive), where 0.0 is faint and 1.0 is very strong.

Part 4: Create a cheese-finding algorithm

You will now need to come up with an algorithm (pseudocode is highly recommended) that will allow you to move Alfred to the exact center of the cheese, using only the power of scent. As an example, you may have Alfred test the scent to find an odor strength of 0.75. You then move him left and find that the odor has decreased to 0.73. Therefore, you conclude that the cheese must be towards the right. Your final solution will likely incorporate one or more loops, and should be inserted into the findCheese() method.

Alfred must be positioned within one pixel of the center of the cheese horizontally and vertically for the maze to be a success. This is indicated by the phrase Close Enough! being displayed after Alfred stops searching for cheese. In other words, merely touching the cheese is not good enough.

A Bullseye! (moving to the exact center) will give you extra credit. This can be determined by making sure the value returned from sniffForCheese() is equal to 1.0. You can use the == or != operator for doubles to test this scenario, rather than an approximation using epsilon.

In the event of success, Alfred will eat the cheese:




Notes, Hints, and Troubleshooting

You must NOT modify any of the code in MouseMaze.java, MouseBody.java, or Cheese.java, minus the troubleshooting exception noted above. You are welcome to rename your mouse from "Alfred" in the main method of MouseBrain.java (only if you would like).

However, feel free to modify any code *after* you have successfully completed and submitted the lab. (Particularly if you know how to draw a better blindfolded mouse, perhaps one that doesn't look like Geordi).

Submission

For this assignment you will need to create a Jar file that contains the source code for MouseBrain.java that you edited as part of this assignment. Follow these instructions for creating a Jar file.
The Jar file you submit must be named yourlastname_pro3.jar, where yourlastname is your last name.

Submit yourlastname_pro3.jar via the UNC Blackboard System before the deadline.

Grading