Tino Intro To Java

Last modified: March 09, 2023

Lab A4.8 Creature AI - Wander

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.

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.

Wandering

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

Class Name and Header

Update the date in the header of your creature class to today's date.

Review Lists

To review useful methods that return a List in Greenfoot, you should review Lab A4.6 Creature AI - Direct Path.

Keep Track Of Wandering Direction

When the creature is wandering, you should keep track of which direction the creature is currently moving.

  1. The direction the creature is moving should always be either north, south, east or west.
  2. The creature should not move diagonally when wandering.
  3. None of this applies if the creature has a target, because in that case it will just go straight to the treat.

Gather Information

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.

ArrayList API

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.

  1. When you make an ArrayList, you need to say what type of data is stored in the list as in ArrayList<String>.
  2. ArrayList cannot store primitives directly, however there are wrapper classes (Integer, Character, Boolean, Double) that can be used to store the corresponding primitives in an object.
  3. Because of a feature called autoboxing, you can add a primitive to an ArrayList holding the corresponding wrapper class type and it will automatically convert it to the correct type. You can also do the same thing when you get objects out of the list.

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

Set A Direction

Make a method that sets the direction to a random open direction:

  1. Create a list and populate it with the directions that are not blocked.
  2. Choose a random valid index in the list and set the direction to that direction.

Wander

Use the helper methods you wrote plus any other tools you may need to update the creature's AI to have the following behaviors:

  1. When a creature has a target treat, it just moves straight toward the treat.
  2. When a creature does not have a target treat, the creature tries to find the closest treat with a direct path.
  3. When the creature cannot find a treat with a direct path, it moves in its current wander direction (N, S, E or W).
  4. When wandering, it always moves only 1 cell at a time either north, south, east or west.
  5. When a creature encounters a wall, it sets a new direction randomly
  6. When the creature is wandering, there is a small percentage chance (like 2%) it will change direction randomly even when not at a wall.

Demo

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

Better wandering

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.

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