1 / 40

Interlude 2 - The Greeps Competition

Interlude 2 - The Greeps Competition. 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). */

nelson
Download Presentation

Interlude 2 - The Greeps Competition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interlude 2 - The Greeps Competition

  2. The Greeps Competition

  3. 12.1 How to Get Started

  4. How to Get Started (continued)

  5. 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! }

  6. 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.

  7. Competition Rules

  8. 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

  9. 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; /** The creature's home ship */ private Ship ship; private boolean moved = false; private boolean atWater = false; private int timeToSpit = 0; /** General purpose memory */ private int memory; private boolean[] flags; • Rule 2: No additional fields. You cannot extend the Greeps’ memory. That is: You are not allowed to add fields to the class (except final fields). You can use the one byte memory that is provided.

  10. 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; }

  11. 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]; }

  12. 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; }

  13. getMemory /** * Retrieve a previously stored value. */ public int getMemory() { return memory; }

  14. 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()) { dropTomato(); } else { turnHome(); move(); } } else { move(); checkFood(); } } • Rule 3: You cannot move more than once per ‘act’ round.

  15. 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.)

  16. 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.

  17. Rule 6 • Rule 6: No creation of objects. You are not allowed to create any scenario objects (instances of user-defined classes, such as Greep or Paint). Greeps have no magic powers - they cannot create things out of nothing.

  18. Rule 7 • Rule 7: No tele-porting. Methods from Actor that cheat normal movement (such as setLocation) may not be used.

  19. Greeps Spitting Paint

  20. 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

  21. Creature Class Methods

  22. Creature Class Methods (continued)

  23. Creature Class Methods (continued)

  24. Creature Class Methods (continued)

  25. Creature Class Methods (continued)

  26. Map 1

  27. Map 2

  28. Map 3

  29. Map 4

  30. Map 5

  31. Map 6

  32. Map 7

  33. Map 8

  34. Map 9

  35. Map 10

  36. Tomatoes

  37. Final Score (First Run)

  38. Final Score (Second Run) Use the total scores to determine if you Greeps logic is improving

  39. 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.

  40. 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.

More Related