Lab 3

due: Monday March 3 at 11:59pm (submit on Gradescope here).

Goals:

Before getting started with the lab, please join our Campuswire discussion board (if you haven't already joined): https://campuswire.com/p/G822FE933. We also suggest updating your notification settings (Settings -> Notification preferences) to enable "New Question Asked" so that you receive an update when a question is posted on the discussion board.

In this lab, we will implement a game in which a bunny (the Player) is roaming around someone's garden trying to gather food items. There is also a dog in the garden who is trying to catch up to the bunny (in a pretty random way).

If the bunny gets to the exact location of a piece of food, it picks it up and stores it. But, if the dog gets too close to the bunny, the bunny loses the first item in its storage. For example if the bunny picks up a pumpkin and then a strawberry and then a tomato, and then gets too close to the dog, then it will lose the pumpkin first. The pumpkin will then be dropped at whatever position the bunny was at when it got too close to the dog.

The bunny can be moved around with the arrow keys. Every time one of these arrow keys are pressed and released, the bunny moves, which also causes the dog to move (randomly). All of the high-level game mechanics are implemented for you, but you'll need to implement some methods to (1) gather food items when the bunny lands on them and (2) lose food items when the bunny gets too close to the dog. You'll also implement a method to calculate the current score. Here's what the game looks like:

Note that the bunny and dog can sometimes overlap which is okay for our game (assume the game continues).

There are 5 files needed for this assignment (download here), but you only need to modify GamePiece.java and Player.java and then submit these two files to Gradescope.


Part 1: Implement the GamePiece squaredDistance method.

First, notice that the Player (the bunny), Dog and Food classes all inherit from the GamePiece class. This GamePiece stores the x (column) and y (row) location of each game piece. Note that x goes from left-to-right and y goes from top-to-bottom, so the top-left corner has coordinates (0, 0).

Our game relies on being able to measure distances between the game pieces, like when the bunny is exactly at a piece of food (zero distance) or "too close" to the dog. Instead of directly using the distance between the game pieces, we'll use the squared distance. For two points $(x_1, y_1)$ and $(x_2, y_2)$, the squared distance is:

$$ \mbox{squared distance} = (x_2 - x_1)^2 + (y_2 - y_1)^2. $$

Your first task is to implement the squaredDistance method of the GamePiece class. See the documentation in GamePiece.java for how to do this. You can add a PSVM at the bottom of the GamePiece class to test your implementation.

Part 2: Complete the Player constructor.

The Player.java file will not compile right now because something is missing in the Player constructor. Please fix this, and recall that Player extends GamePiece. Also, initialize the gatheredFood list to a new ArrayList in the Player constructor.

Once this step is complete, you should be able to run Game.java and move the bunny around with the arrow keys. To quit the game, you can press the q key.

Part 3: Implement the Player checkFood method.

Next, complete the checkFood method defined for the Player class. This will iterate through all the Food items stored in the availableFood ArrayList and, if any Food item is exactly at the location of the Player, then add that Food item to the Player object's gatheredFood list. When this happens, you also need to remove the gathered Food item from the availableFood list (which is the food still available in the Game).

Hint: check if the squaredDistance to each Food item is equal to 0.

Note: You can use Game.createFood to create a list of food items to test your method within a PSVM of the Player class.

Part 4: Implement the Player checkDog method.

Now, please complete the checkDog method, again defined for the Player class which checks the proximity to the dog and updates the game state. You should use the squaredDistance method again but, this time, if the squared distance is less than 16 (i.e. the distance is less than 4), it means the dog is too close and the Player (the bunny) will drop a single piece of food. When a Food item is dropped, it should be left at the exact same location the bunny was just at. The dropped food should be added back into the availableFood list.

Part 5: Calculate the current score.

Finally, please complete the getScore method of the Player class. The score will be calculated as the sum of the number of characters in the name of each gathered food item. For example, if the Player object gathers a pumpkin (7 characters), apple (5 characters) and tomato (6 characters), the score will be 18. Add a test for this example in the PSVM of your Player class.

Submission

Once you have completed the parts above, please upload GamePiece.java and Player.java to Gradescope. Before submitting, we recommend testing the methods you implemented within a PSVM of the classes you want to test.

Please try to respect the style guidelines we introduced in slide 3 of this lecture. We will soon enforce these style rules for homework submissions.