Tino Intro To Java

Last modified: March 25, 2024

Lab A4.6 Creature AI - Direct Path

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

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.

Understanding The Starting Scenario

The starter code sets up the environment, including:

  1. Classes that show menus, load levels and continue to the next level when a level is completed.
  2. An class named PX_LastName_FirstName_Creature where you will write all your code
  3. A main menu with buttons you can click once you press the run button.
    • The Run All Levels option runs the levels in order, going to the next level each time your creature has collected all the food or stop if the last level is completed.
    • The Load Level option lets you load a specific level to run. It will only run that level and will stop when all food is collected.
    • The New Level option lets you create your own custom levels.

Class Name and Header

A 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.
    }
}
  1. Change the name of the class to reflect your name and period.
  2. In the comment above the class declaration, write a short message that says the class represents a creature that moves toward treats and eats them.
  3. Fill in your name for the author and put the date for the version. Your comment and class declaration should look something like this (but with YOUR name and date):
/**
 * 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 {
...

Mission

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:

  1. The creature can never end a frame even partially inside a wall. No going through walls.
  2. The creature can never end a frame more than 2 pixels from where they started (i.e. no moving faster than 2).
  3. You cannot remove or change any properties of any walls.
  4. You cannot move any treats or change the properties of any treats.
  5. You cannot remove any treats the creature is not touching.
  6. You cannot change the size or image of the creature.

Lists

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.
  1. You need to import java.util.List in order to use Lists.
  2. Save the return value into a List.
  3. Use < > 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).

Creature Behaviors

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:

  1. The creature has a speed of 2.
  2. The creature keeps a reference to a target treat that it will move toward each frame if it can.
  3. Every frame:
    • if there is no current target treat, get a list of the treats in the world and, as long as there is at least one, set the target to the first treat.
    • if there is a target, move toward the target by speed; however, if moving toward the target puts the creature in a wall, it should step back to where it was before it went into the wall.
    • Keep the rotation of the creature fixed, so the user should never see it rotate
    • If the creature touches a treat, remove the treat. If the treat is the target treat, set the target back to no target (null)
    • Note that this strategy will work great in level 1 because there are no walls in the middle, however on other levels, it will get stuck on walls. That is ok for now as we will handle that case in a later HW.

Demo

If you implemented all the creature behaviors correctly, your program should behave like this demo.

Optional: Better Wall Strategy

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?

Submission

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

Dark Mode

Outline