Last modified: March 09, 2023
If you have not done Lab A4.6 Creature AI - Direct Path, you should go do that first as this lab will build upon that foundation.
You are writing the AI for a creature that will wander the world finding and collecting food while avoiding walls. The creature must follow these rules while collecting treats:
In this version we will begin to address the situation where there is no direct path between treats because walls are in the way. In particular, you will be writing a method that finds the nearest treat that can be reached directly without encountering a wall.
Notice in the picture above, the creature might be able to go directly to some of the treats, but others are blocked by walls. In this lab, you will make your creature target the closest treat that can be reached by moving directly toward it, rather than just choosing the first treat or a random treat from the list.
If you need to review the set up, you should read about it in Lab A4.6 Creature AI - Direct Path
Update the date in the header of your creature class to today's date.
To review useful methods that return a List in Greenfoot, you should review Lab A4.6 Creature AI - Direct Path.
In order to find the closest treat the creature can reach, first we need a way to determine the distance to each treat. Since we will also need to know if the treat can even be reached, we can find out the answer to both questions with a single helper method. Here is a description of what this method should do:
In the first assignment, you implemented a simple strategy that made the creature pick a treat and go straight to it. That version works fine when all the treats are in an open world like in level 1, but the creature gets stuck as soon as there is a wall between the creature and the target treat. In this version, we will change the strategy so that each time it chooses a new target treat, it will choose the closest reachable treat. This could still fail if the creature is ever in a position where there are no reachable treats, but it will do much better than the last version. For this version it is ok if your creature just stops when there are no reachable treats.
If you implemented all the creature behaviors correctly, your program should behave like this demo.
Once you have the basic lab working, see if you can come up with a strategy for handling the case where there are no reachable treats so the creature will still have a chance of eventually finding all the treats. It doesn't have to be perfect. What would be a better reaction than stopping?
Submit only your PX_LastName_FirstName_Creature.java
file. For example if you were in 3rd period and named Michael Wang, then you would submit P3_Wang_Michael_Creature.java
.
You must Sign In to submit to this assignment