Last modified: March 25, 2024
In this lab, you will create a simulation where a creature will collect treats by going straight to each treat until all treats are gone.
Download the starting scenario to get started. Make sure you extract the zip file after downloading. If that version does not work on your computer, you can use this version that will work for older versions of greenfoot.
The starter code sets up the environment, including:
PX_LastName_FirstName_Creature
where you will write all your codeA PX_LastName_FirstName_Creature
class has already been created for you that looks like this:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class PX_LastName_FirstName_Creature here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PX_LastName_FirstName_Creature extends Actor {
public void act() {
// Add your action code here.
}
}
/**
* Represents a creature that moves toward treats and eats them.
*
* @author Ted McLeod
* @version 3/2/2023
*/
public class P2_McLeod_Ted_Creature extends Actor {
...
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:
Some Actor methods return a List. Here are some examples:
Return type | Method Name and Parameters | Description | |
---|---|---|---|
<A> java.util.List<A> | getIntersectingObjects(java.lang.Class<A> cls) | Get all objects of the given class that intersect this actor | |
<A> java.util.List<A> | getObjectsInRange(int radius, java.lang.Class<A> cls) | Get all the objects of a the given class within the given radius |
There are also some methods that return a List in the World class:
Return type | Method Name and Parameters | Description |
---|---|---|
<A> java.util.List<A> | getObjects(java.lang.Class<A> cls) | Get all the objects in the world of the given class. |
<A> java.util.List<A> | getObjectsAt(int x, int y, java.lang.Class<A> cls) | Return all objects of the given class at a given cell. |
< >
brackets to indicate the element type.List<Flower> flowers = getIntersectingObjects(Flower.class);
Then you can call methods on the list. See the Java List API. Some methods that are useful:
get(int index) //returns the object at position index in the list
size() //returns the size of the list
For example, if you wanted to get the first object in the list of intersecting Flower objects, you could say:
List<Flower> flowers = getIntersectingObjects(Flower.class);
Flower firstFlower = flowers.get(0);
Note that this would throw an IndexOutOfBoundsException if there were no Flower objects intersecting the actor running this code. To avoid that, you should always check the size of the list to make sure the index you are accessing exists, similar to how you would check the length of a String before calling str.charAt(i)
.
In this first assignment, you will implement a simple strategy that makes the creature pick a treat to go to and go straight to it, repeating the process as long as there are still treats. The creature will still not move through walls though, so this strategy will make the creature get stuck on a wall if there is a wall between the creature and the treat. If it goes through the wall, then you did it wrong because you are not following the rules of the mission. In the PXLastNameFirstName_Creature class, you should write code that implements the following behaviors:
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 walls so the creature gets stuck less often. What should the creature do if it hits a wall?
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