Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Programming Contest: The Greeps Are Coming!

Yolande Poirier-OracleDec 8 2015 — edited Dec 10 2015

by Michael Kölling

A fun competition that teaches Java programming and computational problem-solving

I wrote a series of articles for Java Magazine that introduced Java programming with Greenfoot, a programming environment for novices that lets you create simulations and games. In those articles, I discussed aspects of Greenfoot, as well as some aspects of learning to program in Java. In this article, we will apply what was discussed in those articles to have some fun: creating a programming contest involving alien bugs.

If you have been reading Java Magazine, you are likely not a novice programmer. But many of us are in situations where we want to show others how to program, show people the excitement and enjoyment that comes with getting your code running, and teach them how to write Java. All of this works best with interesting, exciting, fun examples. This article provides an example you can use. It does not matter whether you are running a coding club, helping out with programming in your kid's school, teaching your own son or daughter to write Java—or even wanting to learn Java yourself. Whenever you have one or more kids that you want to motivate to really get into writing some code, you can use the Greeps contest. (I use the term kids in the widest possible sense: I mean kids of any age! This probably includes most of us…)

The programming experience needed to tackle this example is quite modest: you need an understanding of variables, method calls with parameters, and if statements. That's about it. However, don't let the simplicity of the constructs fool you: the task itself, while it can be started easily using some basic ideas, can be improved considerably with some intelligent strategy. It is not all simplistic!

This exercise not only teaches Java—it gives people practice in computational problem-solving. The age range for which this example can be used is from about 13 years upwards. However, it works equally well with older age groups. (And, be warned: the competition is highly addictive! Even if you are an experienced programmer, you might have some fun attempting this task. And once you start, you know how it is…)

The Story

Greeps are alien creatures, and they've come to Earth! One of the important things to know about greeps is that they like tomatoes. They have landed with their spaceship and are swarming out to find and collect tomatoes (Figure 1).

Figure1.png

Figure 1. What greeps look like.

The challenge in this programming competition will be to program your greeps so that they find and collect tomatoes as quickly as possible. You will have limited time, and every tomato you manage to bring back to the spaceship scores a point.

The Practicalities

You can use this exercise in a competition against a friend who programs his or her own greeps. Or you can use it with a group of people in a coding club. If you are on your own, you could post your entry to the Greenfoot website to see how you compare to others who have posted there. (However, if you post to the Greenfoot site, please do not include source code. Instead, post an executable version that others can see and run to compare their score and their code's behavior. We want to keep this as a programming challenge for others and don't want to make it too easy to find solutions.)

Getting Started

To start, download the Greeps scenario file, greeps.zip, and open it in Greenfoot. Then run it.

When you run the scenario, you will see that a spaceship lands in an area with sand and water. The greeps will come out and start swarming around in search of tomatoes. By a magnificent stroke of luck, there are some piles of tomatoes in the area.

Greeps are land animals; they cannot swim. You will see that they will not go into water. If there is a lake in the way, they have to walk around it.

When you try out the program as it is, you will quickly notice that the greeps are not very smart: they swarm out of the spaceship in random directions, but when they reach the edge of the water (or the edge of the screen), they stop. They cannot go forward, so they don't know what to do.

Your task in this competition will be to program the greeps to give them more-intelligent behavior, so that they are able to walk around, explore, find tomatoes, and bring tomatoes back to the ship.

There are a few things about greeps that you should know:

  • There are 20 greeps in the spaceship. They will come out after landing to start their work. You cannot get any more of them.
  • Greeps can carry a tomato on their back, but they cannot load tomatoes onto their own back. They can only load a tomato onto another greep's back! This means that two of them have to be at the tomato pile at the same time to pick up a tomato.
  • Greeps cannot talk or communicate verbally in any way. They can, however, spit paint onto the ground. And they can spit in three different colors (see Figure 2)! There are rumors that there once was a tribe of greeps who used this ability to convey information to each other.
  • Greeps are very short-sighted. They can see only the ground at their immediate location and they cannot look any further.
  • Greeps have a good memory—they never forget what they know. However, unfortunately, their memory is very limited. They can remember only a few things at a time.

Figure2.png

Figure 2. Greeps can spit three colors of paint.

With this background knowledge, you are practically a greep expert and can get to work programming your own greep behavior.

The Rules

To modify the behavior of the greeps, you improve the code in the Greep class. There is already some (very simple) behavior present that you can look at to get started.

When you look at the scenario, you can see that Greep is a subclass of Creature. The Creature class provides some very useful methods that you can use; have a look at what is there.

There are some rules that you must follow:

  • Rule 1: Only change the class Greep. No other classes may be modified or created.
  • Rule 2: You cannot extend the greeps' memory. That is, you are not allowed to add fields (other than final fields) to the class. Some general-purpose memory (one int and two booleans) is provided.
  • Rule 3: You cannot move more than once per "act" round.
  • Rule 4: Greeps do not communicate directly. They do not call each other's methods or access each other's fields.
  • Rule 5: There is no long vision. You are allowed to look at the world only in the immediate location of a greep. Greeps are almost blind and cannot look any further.
  • Rule 6: There is 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.
  • Rule 7: There is no teleporting. Methods from Actor that cheat normal movement (such as setLocation) may not be used.

It is important to follow these rules. It is technically easy to break them, but we consider that cheating. Where's the fun in that?

To program the greeps, you will work mostly in the greeps' act method (and any other private methods you choose to create).

Some Tips

  • Read the documentation of class Creature. (The best way to do this is to open the class in the editor and switch to the Documentation view.) These are some of the most useful methods for your work. Know what is there.
  • Work in small steps. Start making small improvements and see how it goes.
  • Some first improvements could be as follows: turn around when you are at water, wait if you find a tomato pile (and try to load tomatoes), turn if you are at the edge of the world, and so on.

You will soon figure out many more improvements you could implement. It gets especially interesting once you start using the paint drops on the ground to make marks for other greeps to find.

Running the Greeps Project as a Competition

The Greeps project is great fun if you run it as a competition. To do this, it is best if there is a judge who can coordinate things. In a school, this can be the teacher. If you do this with friends, the judge can be one of your friends (but this person should then not take part in 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 that was downloaded earlier.) 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 on a known map.) The judge should have a different scenario that includes more maps. I recommend running the competition with 10 different maps. Contestants do not get access to the last seven maps—they can test their code only on the first three. Then they hand in their greeps for scoring, and the judge then runs the contestants' greeps on all 10 maps (maybe on a large display screen) to reach the official score.

The competition is best run over several days (or maybe a week or two), with repeated chances for contestants to submit their work for scoring, so that they can slowly improve.

Technicalities

For submission 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 or her full (10-map) scenario, recompiles the code, and runs it. This ensures that no other classes are modified in the process.

Some artwork (to make flyers or posters for the competition) is available at http://www.greenfoot.org/competition/greeps/.

Instructors can also find instructions there for obtaining a version of the Greeps scenario with 10 maps. Alternatively, instructors can make more maps themselves fairly easily. An image of an empty map is provided in the images folder of the Greeps scenario. Water can just be painted onto the map, and map data (for example, the location of tomato piles and so on) can be specified in the Earth class.

Conclusion

A competition—whether run against yourself or against others—is a great way to have fun when programming. This particular one will let you use some fairly simple Java constructs and hone your problem-solving skills. It's great fun; you will see when you start.

The Greeps competition is part of the Greenfoot book and is discussed in more detail there (as are many other programming projects).

See Also

About the Author

Michael Kölling is a professor at the School of Computing, University of Kent, in Canterbury, England. His research interests are in the areas of object-oriented systems, software tools, programming languages, computing education, and HCI. He is the author of two Java textbooks and is the lead developer of BlueJ and Greenfoot.

Join the Conversation

Join the Java community conversation on Facebook, Twitter, and the Oracle Java Blog!

Comments

843793
Well, this was a shot in the dark, but I tested it and it seems to be the problem: remove the parens around i.next()!

After doing so, running the code in your post instead provides a "NoSuchElementException" from the iterator (completely expected). I'm not sure if this a bug in the compiler, or if it has something to do with the way the code gets expanded. Maybe if someone thinks about it long enough they'll be able to come up with a reason.
843793
Why would you expect to use those parens like that anyway?
843793
Why would you expect to use those parens like that
anyway?
I can see that the parens are redundant - and as the other responder pointed out, the error goes away when they are removed...however, I'm still curious...

Firstly, is it actually an error to use the parentheses in that way?

Secondly, although the offending line is executed in the example I gave, the code that I pulled the example from is a bit stranger - when I try to instantiate a class (call it B), that contains a method with the redundant parentheses, I get the same error - even though the offending line isn't executed (I can post the code tonight if anyone is interested).

Gareth
843793
Here's the code:

import java.util.*;
class A {
public static void main (String argv[]) {
B bb = new B();
}
}

class B {

void methodC() {
ArrayList<Boolean> B = new ArrayList<Boolean>(10);
Iterator<Boolean> i = B.iterator();
boolean b = (i.next()).booleanValue();
}
}

843793
This is definitely a bug in the compiler. The bytecode generated for B.methodC() is:

Codeview "bytecode" for void B.methodC():
#3/Test.java:12 - new class java.util.ArrayList
#4/Test.java:12 - dup
#5/Test.java:12 - bipush (byte)10
#6/Test.java:12 - invokespecial public java.util.ArrayList(int)
#7/Test.java:12 - astore_1 lv_1
#8/Test.java:13 - aload_1 lv_1
#9/Test.java:13 - invokevirtual public java.util.Iterator java.util.AbstractList.iterator()
#10/Test.java:13 - astore_2 lv_2
#11/Test.java:14 - aload_2 lv_2
#12/Test.java:14 - invokeinterface public abstract java.lang.Object java.util.Iterator.next()
#13/Test.java:14 - invokevirtual public final boolean java.lang.Boolean.booleanValue()
#14/Test.java:14 - istore_3 lv_3
#15/Test.java:15 - return

and it's clear that between instruction #12 and instruction #14 a typecast is missing.

I'll add this to my list of known prototype compiler bugs at
http://cag.lcs.mit.edu/~cananian/Projects/GJ/
1 - 5

Post Details

Added on Dec 8 2015
10 comments
6,246 views