-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyGreep.java
153 lines (141 loc) · 4.45 KB
/
MyGreep.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
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A Greep is an alien creature that likes to collect tomatoes.
*
* Rules:
*
* Rule 1
* Only change the class 'MyGreep'. 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 is
* provided. (The ship can also store data.)
*
* Rule 3
* You can call any method defined in the "Greep" superclass, except act().
*
* Rule 4
* Greeps have natural GPS sensitivity. You can call getX()/getY() on any object
* and get/setRotation() on yourself any time. Friendly greeps can communicate.
* You can call getMemory() and getFlag() on another greep to ask what they know.
*
* Rule 5
* No creation of objects. You are not allowed to create any scenario objects
* (instances of user-defined classes, such as MyGreep). Greeps have no magic
* powers - they cannot create things out of nothing.
*
* Rule 6
* You are not allowed to call any methods (other than those listed in Rule 4)
* of any other class in this scenario (including Actor and World).
*
* If you change the name of this class you should also change it in
* Ship.createGreep().
*
* Please do not publish your solution anywhere. We might want to run this
* competition again, or it might be used by teachers to run in a class, and
* that would be ruined if solutions were available.
*
*
* @author (your name here)
* @version 0.1
*/
public class MyGreep extends Greep
{
// Remember: you cannot extend the Greep's memory. So:
// no additional fields (other than final fields) allowed in this class!
/**
* Default constructor. Do not remove.
*/
public MyGreep(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().
checkFood();
if (carryingTomato()) {
if(atShip()) {
dropTomato();
}
else {
turnHome();
move();
}
}
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 (numberOfOpponents(false) > 3) {
// Can we see four or more opponents?
kablam();
}
else {
randomWalk();
}
if (atWater() || moveWasBlocked()) {
turn(Greenfoot.getRandomNumber(300));
move(20);
}
}
/**
* Move forward, with a slight chance of turning randomly
*/
public 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();
}
/**
* 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 = 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.
}
}
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 greeps (for display on the result board).
* Try to keep the name short so that it displays nicely on the result board.
*/
public String getName()
{
return "Swag Kings"; // write your name here!
}
}