-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCritter.java
More file actions
167 lines (134 loc) · 5.36 KB
/
Critter.java
File metadata and controls
167 lines (134 loc) · 5.36 KB
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
163
164
165
166
167
// Critters
// Authors: Dat Ly, Marty Stepp and Stuart Reges
//
// This class defines the methods necessary for an animal to be part of the simulation.
// Your critter animal classes 'extend' this class to add to its basic functionality.
//
import java.awt.*; // for Color
public abstract class Critter {
// The following five methods are the ones you must implement for your assignment.
// I'm not going to comment them because that's your job.
public boolean eat() {
return false;
}
public Attack fight(String opponent) {
return Attack.FORFEIT;
}
public Color getColor() {
return Color.BLACK;
}
public Direction getMove() {
return Direction.CENTER;
}
public String toString() {
return "?";
}
// I use these fields to implement the methods below such as getX and getNeighbor.
private int x;
private int y;
private int width;
private int height;
private boolean alive = true;
private boolean awake = true;
private final String[] neighbors = {" ", " ", " ", " ", " "};
// constants for directions
public static enum Direction {
NORTH, SOUTH, EAST, WEST, CENTER
}
// constants for fighting
public static enum Attack {
ROAR, POUNCE, SCRATCH, FORFEIT
}
// The following methods are provided to get information about the critter.
// Technically the critter could call setXxxx() on itself,
// but the game model ignores this anyway, so it's useless to do so.
// These methods are declared 'final' so you can't override them.
// Returns the height of the game simulation world.
public final int getHeight() {
return height;
}
// Returns the animal that is 1 square in the given direction away
// from this animal. A blank space, " ", signifies an empty square.
public final String getNeighbor(Direction direction) {
return neighbors[direction.ordinal()];
}
// Returns the width of the game simulation world.
public final int getWidth() {
return width;
}
// Returns this animal's current x-coordinate.
public final int getX() {
return x;
}
// Returns this animal's current y-coordinate.
public final int getY() {
return y;
}
// Returns true if this animal is currently alive.
// This will return false if this animal has lost a fight and died.
public final boolean isAlive() {
return alive;
}
// Returns true if this animal is currently awake.
// This will temporarily return false if this animal has eaten too much food
// and fallen asleep.
public final boolean isAwake() {
return awake;
}
// Sets whether or not this animal is currently alive.
// This method is called by the simulator and not by your animal itself.
public final void setAlive(boolean alive) {
this.alive = alive;
}
// Sets whether or not this animal is currently awake.
// This method is called by the simulator and not by your animal itself.
public final void setAwake(boolean awake) {
this.awake = awake;
}
// Sets the height of the game simulation world to be the given value,
// so that future calls to getHeight will return this value.
// This method is called by the simulator and not by your animal itself.
public final void setHeight(int height) {
this.height = height;
}
// Sets the neighbor of this animal in the given direction to be the given value,
// so that future calls to getNeighbor in that direction will return this value.
// This method is called by the simulator and not by your animal itself.
public final void setNeighbor(Direction direction, String value) {
neighbors[direction.ordinal()] = value;
}
// Sets the width of the game simulation world to be the given value.
// so that future calls to getWidth will return this value.
// This method is called by the simulator and not by your animal itself.
public final void setWidth(int width) {
this.width = width;
}
// Sets this animal's memory of its x-coordinate to be the given value.
// so that future calls to getX will return this value.
// This method is called by the simulator and not by your animal itself.
public final void setX(int x) {
this.x = x;
}
// Sets this animal's memory of its y-coordinate to be the given value.
// so that future calls to getY will return this value.
// This method is called by the simulator and not by your animal itself.
public final void setY(int y) {
this.y = y;
}
// These methods are provided to inform you about the result of fights, sleeping, etc.
// You can override these methods in your Longhorn to be informed of these events.
// called when you win a fight against another animal
public void win() {}
// called when you lose a fight against another animal, and die
public void lose() {}
// called when your animal is put to sleep for eating too much food
public void sleep() {}
// called when your animal wakes up from sleeping
public void wakeup() {}
// called when the game world is reset
public void reset() {}
// called when your critter mates with another critter
public void mate() {}
// called when your critter is done mating with another critter
public void mateEnd() {}
}