Last modified: March 09, 2023
If you have not done Lab A4.6 Creature AI - Direct Path and Creature AI - Scouting Line of Movement, you should go do those first as this lab will build upon those labs.
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 previous versions, if there was no direct path to any treat, then the creature just stood still, but in this version the creature will wander around by moving horizontally or vertically, while avoiding walls.
Notice in the picture above, the creature cannot move directly to any of the treats. In this lab, you will make your creature wander around until a target treat can be found, at which point it will go straight to it.
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.
When the creature is wandering, you should keep track of which direction the creature is currently moving.
In order to choose a valid direction to wander in, while avoiding walls, it will be helpful if you could easily find out which directions are not blocked by walls. To do this, you will make four helper methods that return whether north is blocked, south is blocked, east is blocked or west is blocked. You may also want other methods such as a method that returns whether front is blocked (the direction the creature is moving). Any of these methods could be reversed (i.e. front is clear, north is clear...etc). The point is to be able to tell which directions are open and which are blocked.
List is an interface, so you cannot make instances of a List, instead you need to make an instance of a class that implements List, such as ArrayList.
ArrayList<String>
.Here is an example:
// create an ArrayList that holds Integer values
ArrayList<Integer> numList = new ArrayList<Integer>();
// Add an 8 to the list. This is equivalent to numList.add(new Integer(8))
numList.add(8);
numList.add(7);
numList.add(2);
System.out.println(numList); // prints [8, 7, 2]
int firstNum = numList.get(0);
System.out.println(firstNum); // prints 8
Make a method that sets the direction to a random open direction:
Use the helper methods you wrote plus any other tools you may need to update the creature's AI to have the following behaviors:
If you implemented all the creature behaviors correctly, your program should behave like this demo.
While this version works pretty well, there can still be scenarios where the creature has a hard time finding all the treats. See if you can think of a way to improve the AI. Observe the AI as it moves around and try to identify scenarios where it could make smarter choices.
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