-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleGreep.java
162 lines (147 loc) · 4.59 KB
/
SimpleGreep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
/**
* A Greep is an alien creature that likes to collect tomatoes.
*
* This is a simple greep that makes use of most of the functionality in Greep,
* and it is the default opponent that you will have when competing in the
* Greeps competition.
*
* This greep uses the memory to remember where it has seen tomatoes, and will
* go back to get more. Also, if they hit water, they turn immediately and try
* a different direction.
*
* If the greep is waiting alone at a pile of tomatoes it will block so opponents
* can't get to the pile.
*
* Finally, if the greep can see multiple opponent greeps, it will let
* off a stink bomb.
*
* This greep does not communicate with other greeps or the ship.
*
* @author Davin McCall
* @author Poul Henriksen
* @version 1.0
*/
public class SimpleGreep extends Greep
{
// Remember: you cannot extend the Greep's memory. So:
// no additional fields (other than final fields) allowed in this class!
private static final int TOMATO_LOCATION_KNOWN = 1;
/**
* Default constructor. Do not remove.
*/
public SimpleGreep(Ship ship)
{
super(ship);
}
/**
* Do what a greep's gotta do.
*/
public void act()
{
super.act(); // do not delete! leave as first statement in act().
// Before moving, lets check for food.
checkFood();
if (carryingTomato()) {
bringTomatoHome();
}
else if(getTomatoes() != null) {
TomatoPile tomatoes = getTomatoes();
if(!blockAtPile(tomatoes)) {
// Not blocking so lets go towards the centre of the pile
turnTowards(tomatoes.getX(), tomatoes.getY());
move();
}
}
else if (getMemory(0) == TOMATO_LOCATION_KNOWN) {
// Hmm. We know where there are some tomatoes...
turnTowards(getMemory(1), getMemory(2));
move();
}
else if (numberOfOpponents(false) > 3) {
// Can we see four or more opponents?
kablam();
}
else {
randomWalk();
}
// Avoid obstacles
if (atWater() || moveWasBlocked()) {
// If we were blocked, try to move somewhere else
int r = getRotation();
setRotation (r + Greenfoot.getRandomNumber(2) * 180 - 90);
move();
}
}
/**
* Is there any food here where we are? If so, try to load some!
*/
public void checkFood()
{
TomatoPile tomatoes = getTomatoes();
if(tomatoes != null) {
loadTomato();
// Note: this attempts to load a tomato onto *another* Greep. It won't
// do anything if we are alone here.
setMemory(0, TOMATO_LOCATION_KNOWN);
setMemory(1, tomatoes.getX());
setMemory(2, tomatoes.getY());
}
}
/**
* Move forward, with a slight chance of turning randomly
*/
private void randomWalk()
{
// there's a 3% chance that we randomly turn a little off course
if (randomChance(3)) {
turn((Greenfoot.getRandomNumber(3) - 1) * 100);
}
move();
}
/**
* Bring a tomato to our ship. Drop it if we are at the ship.
*/
private void bringTomatoHome()
{
if(atShip()) {
dropTomato();
}
else {
turnHome();
move();
}
}
/**
* If we are at a tomato pile and none of our friends are blocking, we will block.
*
* @return True if we are blocking, false if not.
*/
private boolean blockAtPile(TomatoPile tomatoes)
{
// Are we at the centre of the pile of tomatoes?
boolean atPileCentre = tomatoes != null && distanceTo(tomatoes.getX(), tomatoes.getY()) < 4;
if(atPileCentre && getFriend() == null ) {
// No friends at this pile, so we might as well block
block();
return true;
}
else {
return false;
}
}
private int distanceTo(int x, int y)
{
int deltaX = getX() - x;
int deltaY = getY() - y;
return (int) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
}
/**
* This method specifies the name of the author (for display on the result board).
*/
public String getName()
{
return "Simple";
}
}