People keep asking me for the source code. The Greeps scenario is intended to be a competition and making my code available online would ruin the fun for a lot of people participating in the contest. Instead I will try to give you some hints to get you started.
If you hit the play button on the unedited "out of the box" version of the Greep class you will find your Greeps doing a very bad job. They fail at their very first task because they get stuck as soon as they hit the water or world edge. Your first task is therefore to make them keep walking. You will insert something like this into your code:
If you run your code now you will notice that your Greeps indeed keep walking. They don't get stuck anymore because they turn by 90 degrees once they hit the water or world edge. Well, at least as long as they don't find a tomato by accident. Unfortunately walking around is pretty much all they do at this point. The next step will be to make the Greeps wait at the tomato piles they find. To accomplish this task you might find the following method usefull:
If you run your Greeps again you will find that they actually start collecting tomatoes. The problem is that they often get stuck on their way back to the ship. This is task number 3 of the basic Greep routine we have to address.
We have already fixed the movement issues for the non-tomato-carrying Greeps. Why not do the exact same thing for the Greeps carrying a tomato? Just insert code Snipet 1 in the right place (where greeps are carrying a tomato but are not at the ship). This is great except for the fact that it didn't help much... The greeps still get stuck.
The reason is that your turn(90) command is in conflict with the turnHome() command in the same code section. Assume you are stuck at the water. The code snipet you just inserted makes your Greep turn 90 degree to the right. The following turnHome() command nullifies the effect of your command by turning the greep back facing the ship. He is still stuck. Turning the order of the commands around doesn't help much either.There are various ways to overcome this problem.
I will show you one possible solution using random numbers: Instead of turning strictly 90 degrees we turn randomly between 0 and 270 degrees. This can be achieved easily by replacing "90" with the expression "Greenfoot.getRandomNumber(270)". The second thing we have to change is the execution order. Make sure that you turn randomly first, then move (so your turning has an effect on your movement) and at the end turn back towards your ship. Your code should look like this:
If you run your Greeps now you can see that they are not stuck anymore. They keep walking and try to overcome the obstacle that blocks the way back to the ship. Please note that this solution to the problem is far from optimal. I'm sure that you can come up with a much better solution quite easily.
Once the basic Greep routine has been established you can start improving it. There are many ways in which you can do so. If you watch your basic greeps, you will notice that they don't take advantage of the fact that they found a tomato pile. Once they return the tomato to the ship they are back at square one and start searching again. A simple but effective way to make the greep go back to get another tomato from the same pile is to send them back the way they came.
Just turn them around 180 degrees once they drop the tomato at the ship and the greep will return to the tomato pile. Well at least he is heading in that general direction which is not too bad. Once you have accomplished this, you might want the lucky greeps who found tomatoes to communicate the location to other greeps. This is where the paint comes in. By leaving a paint trail one greep can communicate the location (or general direction) of a tomato pile to other greeps.
This is where you will decide what to do with the 10 bit of memory every greep has available. There are a lot of possibilities. During the process of improving your code you will come up with more and more ideas on how you could use that memory to improve the greeps behaviour. Explore them carefully. Run a lot of tests to find the most effective application for it.
-> Use randomness as often as possible. In many cases you can solve tasks using random Chances instead of using valuable memory.
-> Experiment with different angles to turn at water and world edges. Maybe add some randomness too. Small changes can have huge impact on performance!
-> Take small steps. Don't implement too much new stuff at once. Save your work after every step in a separate file, so you can go back to an earlier version if necessary.
-> Run the maps A LOT. Observe your greeps closely. Watch out for situations where they lose a lot of time. Try to improve their behaviour in those situations.
-> Design your own maps to see how your greeps perform in difficult situations.
-> Use the 10 bit of memory they give wisely! Don't waste it on things you could have done without it.
-> They give you 3 out of the 10 maps in the competition. Use the fact that you know the first 3 maps to improve your greeps performance specifically on those maps. Be carefull though that you don't hurt your performance on other maps. (*)
(*) IMPORTANT: Some teachers and professors try to introduce additional rules to prevent students from doing this kind of map specific optimizations. Enforcing such rules is very difficult and requires an in depth analysis of the source code. A much easier way to prevent students from doing this is to simply exclude the first 3 maps from the competition. The best way to keep the remaining maps secret is to design them yourself. Empty maps can be downloaded from the competition website.
People keep asking me for the source code. The Greeps scenario is intended to be a competition and making my code available online would ruin the fun for a lot of people participating in the contest. Instead I will try to give you some hints to get you started.
JoshTheSerious 9 months ago
1 The Basics
Greeps are very simple creatures. They follow a basic routine as soon as they land on a planet:
1. Walk around until you find a tomato pile.
2. Wait at tomato pile until a fellow Greep helps you load a tomato on your back.
3. Carry the tomato back to the ship.
4. Goto 1.
JoshTheSerious 9 months ago
If you hit the play button on the unedited "out of the box" version of the Greep class you will find your Greeps doing a very bad job. They fail at their very first task because they get stuck as soon as they hit the water or world edge. Your first task is therefore to make them keep walking. You will insert something like this into your code:
JoshTheSerious 9 months ago
if (atWater()) { turn(90); //positive values will turn your greeps clockwise; negative values counter-clockwise.
}
if (atWorldEdge()) { turn(90);
}
JoshTheSerious 9 months ago
If you run your code now you will notice that your Greeps indeed keep walking. They don't get stuck anymore because they turn by 90 degrees once they hit the water or world edge. Well, at least as long as they don't find a tomato by accident. Unfortunately walking around is pretty much all they do at this point. The next step will be to make the Greeps wait at the tomato piles they find. To accomplish this task you might find the following method usefull:
JoshTheSerious 9 months ago
public boolean seeFood() { //this method checks wether or not there is a tomato pile at the Greeps location.
if (getOneIntersectingObject(TomatoPile.class) != null) { return true;
} return false;
}
You can then replace the single move() instruction by the follwing statement:
JoshTheSerious 9 months ago
if (seeFood()) { /*wait for fellow greep*/
} else move(); //nothing to see here...
JoshTheSerious 9 months ago
If you run your Greeps again you will find that they actually start collecting tomatoes. The problem is that they often get stuck on their way back to the ship. This is task number 3 of the basic Greep routine we have to address.
JoshTheSerious 9 months ago
We have already fixed the movement issues for the non-tomato-carrying Greeps. Why not do the exact same thing for the Greeps carrying a tomato? Just insert code Snipet 1 in the right place (where greeps are carrying a tomato but are not at the ship). This is great except for the fact that it didn't help much... The greeps still get stuck.
JoshTheSerious 9 months ago
The reason is that your turn(90) command is in conflict with the turnHome() command in the same code section. Assume you are stuck at the water. The code snipet you just inserted makes your Greep turn 90 degree to the right. The following turnHome() command nullifies the effect of your command by turning the greep back facing the ship. He is still stuck. Turning the order of the commands around doesn't help much either.There are various ways to overcome this problem.
JoshTheSerious 9 months ago
I will show you one possible solution using random numbers: Instead of turning strictly 90 degrees we turn randomly between 0 and 270 degrees. This can be achieved easily by replacing "90" with the expression "Greenfoot.getRandomNumber(270)". The second thing we have to change is the execution order. Make sure that you turn randomly first, then move (so your turning has an effect on your movement) and at the end turn back towards your ship. Your code should look like this:
JoshTheSerious 9 months ago
if (atWater()) { turn(Greenfoot.getRandomNumber(270)); //positive values will turn your greeps clockwise; negative values counter-clockwise.
}
if (atWorldEdge()) { turn(Greenfoot.getRandomNumber(270));
}
move();
turnHome();
JoshTheSerious 9 months ago
If you run your Greeps now you can see that they are not stuck anymore. They keep walking and try to overcome the obstacle that blocks the way back to the ship. Please note that this solution to the problem is far from optimal. I'm sure that you can come up with a much better solution quite easily.
That's it. Your greeps are collecting tomatoes.
JoshTheSerious 9 months ago
2 Next steps
Once the basic Greep routine has been established you can start improving it. There are many ways in which you can do so. If you watch your basic greeps, you will notice that they don't take advantage of the fact that they found a tomato pile. Once they return the tomato to the ship they are back at square one and start searching again. A simple but effective way to make the greep go back to get another tomato from the same pile is to send them back the way they came.
JoshTheSerious 9 months ago
Just turn them around 180 degrees once they drop the tomato at the ship and the greep will return to the tomato pile. Well at least he is heading in that general direction which is not too bad. Once you have accomplished this, you might want the lucky greeps who found tomatoes to communicate the location to other greeps. This is where the paint comes in. By leaving a paint trail one greep can communicate the location (or general direction) of a tomato pile to other greeps.
JoshTheSerious 9 months ago
3 Advanced strategies
This is where you will decide what to do with the 10 bit of memory every greep has available. There are a lot of possibilities. During the process of improving your code you will come up with more and more ideas on how you could use that memory to improve the greeps behaviour. Explore them carefully. Run a lot of tests to find the most effective application for it.
JoshTheSerious 9 months ago
4 Hints:
-> Use randomness as often as possible. In many cases you can solve tasks using random Chances instead of using valuable memory.
-> Experiment with different angles to turn at water and world edges. Maybe add some randomness too. Small changes can have huge impact on performance!
JoshTheSerious 9 months ago
-> Take small steps. Don't implement too much new stuff at once. Save your work after every step in a separate file, so you can go back to an earlier version if necessary.
-> Run the maps A LOT. Observe your greeps closely. Watch out for situations where they lose a lot of time. Try to improve their behaviour in those situations.
JoshTheSerious 9 months ago
-> Design your own maps to see how your greeps perform in difficult situations.
-> Use the 10 bit of memory they give wisely! Don't waste it on things you could have done without it.
-> They give you 3 out of the 10 maps in the competition. Use the fact that you know the first 3 maps to improve your greeps performance specifically on those maps. Be carefull though that you don't hurt your performance on other maps. (*)
JoshTheSerious 9 months ago
(*) IMPORTANT: Some teachers and professors try to introduce additional rules to prevent students from doing this kind of map specific optimizations. Enforcing such rules is very difficult and requires an in depth analysis of the source code. A much easier way to prevent students from doing this is to simply exclude the first 3 maps from the competition. The best way to keep the remaining maps secret is to design them yourself. Empty maps can be downloaded from the competition website.
JoshTheSerious 9 months ago