Interlude_2_-_The_Greeps_Competition

Download Report

Transcript Interlude_2_-_The_Greeps_Competition

Interlude 2 - The Greeps
Competition
Bruce Chittenden
The Greeps Competition
12.1 How to Get Started
How to Get Started (continued)
Add Your Name to Greeps Class
Put your name here
/**
* This method specifies the name of the author (for display on the result board).
*/
public static String getAuthorName()
{
return "Anonymous"; // write your name here!
}
12.2 Programming Your Greeps
To program your Greeps to collect as many tomatoes as
possible, you should improve their behavior. The Greep
class, which is included in the scenario, already includes
some behavior (albeit not very clever) that you can look
at to get started.
We can see that Greep is a subclass of Creature. Class
Creature provides a number of very useful methods that
we can use.
Competition Rules
Rule 1
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A Greep is an alien creature that likes to collect tomatoes.
*
* @author (your name here)
* @version 0.1
*/
public class Greep extends Creature
{
// Remember: you cannot extend the Greep's memory. So:
// no additional fields (other than final fields) allowed in this class!
/**
* Default constructor for testing purposes.
*/
public Greep()
{
this(null);
}
/**
* Create a Greep with its home space ship.
*/
public Greep(Ship ship)
{
super(ship);
}
Rule 1: Only change the class
‘Greep’. No other classes may be
modified or created
Rule 2
public abstract class Creature extends Actor
{
private static final double WALKING_SPEED = 5.0;
private static final int TIME_TO_SPIT = 10;
/** Indicate whether we have a tomato with us */
private boolean carryingTomato = false;
Rule 2: No additional fields. You
/** The creature's home ship */
cannot extend the Greeps’ memory.
private Ship ship;
That is: You are not allowed to add
fields to the class (except final
private boolean moved = false;
fields). You can use the one byte
private boolean atWater = false;
memory that is provided.
private int timeToSpit = 0;
/** General purpose memory */
private int memory;
private boolean[] flags;
setFlag
/**
* Store a user defined boolean value (a "flag"). Two flags are available,
* i.e. 'flagNo' may be 1 or 2.
*/
public void setFlag(int flagNo, boolean val)
{
if(flagNo < 1 || flagNo > 2)
throw new IllegalArgumentException("flag number must be either 1 or 2");
else
flags[flagNo-1] = val;
}
getFlag
/**
* Retrieve the value of a flag. 'flagNo' can be 1 or 2.
*/
public boolean getFlag(int flagNo)
{
if(flagNo < 1 || flagNo > 2)
throw new IllegalArgumentException("flag number must be either 1 or 2");
else
return flags[flagNo-1];
}
setMemory
/**
* Store a user defined value. Attention: even though the parameter type is int,
* only byte size values (0 <= val <= 255) are accepted.
*/
public void setMemory(int val)
{
if(val < 0 || val > 255)
throw new IllegalArgumentException("memory value must be in range [0..255]")
else
memory = val;
}
getMemory
/**
* Retrieve a previously stored value.
*/
public int getMemory()
{
return memory;
}
Rule 3
/**
* Do what a greep's gotta do.
*/
public void act()
{
super.act(); // do not delete! leave as first statement in act().
if (carryingTomato()) {
if(atShip()) {
Rule 3: You cannot move more than
dropTomato();
}
once per ‘act’ round.
else {
turnHome();
move();
}
}
else {
move();
checkFood();
}
}
Rule 4
Rule 4: You cannot communicate
directly with other Greeps. That is:
no field accesses or method calls to
other Greep objects are allowed.
(Greeps can communicate indirectly
via the paint spots on the ground.)
Rule 5
/**
* Is there any food here where we are? If so, try to load some!
*/
public void checkFood()
{
// check whether there's a tomato pile here
TomatoPile tomatoes = (TomatoPile) getOneIntersectingObject(TomatoPile.class);
if(tomatoes != null) {
loadTomato();
// Note: this attempts to load a tomato onto *another* Greep. It won't
// do anything if we are alone here.
}
Rule 5: No long vision. You are
}
allowed to look at the world only at
the immediate location of the
Greep. Greeps are almost blind, and
cannot look any further.
Rule 6
Rule 6: No creation of objects. You
are not allowed to create any
scenario objects (instances of userdefined classes, such as Greep or
Paint). Greeps have no magic
powers - they cannot create things
out of nothing.
Rule 7
Rule 7: No tele-porting. Methods
from Actor that cheat normal
movement (such as setLocation)
may not be used.
Greeps Spitting Paint
Strategy from Michael Kolling
In the handout version, Greeps just run more or less straight, and when they
hit an obstacle (water or the screen edge) they are stuck and stay there.
The first thing I suggest to my students to do is to turn when they hit water, so
they don’t get stuck. Then turn if you hit the screen edge.
The next thing is that Greeps can only pick up tomatoes when two of them are
at the tomato pile together. So I suggest that they wait at the pile when they
find one.
That is usually enough to get them started and to generate more ideas.
You can then (that will be a day or two in) also have discussion where they
describe their ideas of how to improve it.
Michael
Creature Class Methods
Creature Class Methods (continued)
Creature Class Methods (continued)
Creature Class Methods (continued)
Creature Class Methods (continued)
Map 1
Map 2
Map 3
Map 4
Map 5
Map 6
Map 7
Map 8
Map 9
Map 10
Tomatoes
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
40
10
10
40
30
20
6
50
50
20
12
40
30
40
30
20
20
30
30
12
40
40
50
50
30
40
40
50
30
20
40
10
40
16
50
30
40
30
30
40
30
40
40
20
20
20
7
Total
20
178
190
130
130
150
150
146
130
110
132
1446
Final Score (First Run)
Final Score (Second Run)
Use the total scores to determine if
you Greeps logic is improving
12.3 Running the Competition
To make the competition interesting, there should be two
versions of the Greeps scenario. One gets handed out to
all contestants. (This is the one included in the book
scenarios.) This scenario includes three different maps.
The Greeps land and forage on each of the three maps in
turn. (So the challenge for contestants is to develop
movement algorithms that are flexible enough to work on
different maps, not just a known one.) We recommend
running the competition with 10 different maps.
12.4 Technicalities
For submissions of an entry to the judge, the easiest
mechanism is that contestants submit only the
Greeps.java file. The judge then copies that file into his
full (10-map) scenario, recompiles and runs it.