-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHerbivore.java
More file actions
124 lines (103 loc) · 2.85 KB
/
Herbivore.java
File metadata and controls
124 lines (103 loc) · 2.85 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
package ca.bcit.comp2526.a2a;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JLabel;
/**
* Herbivore moves into adjacent Cells randomly
* and will either eat a plant (if cell has one).
* If empty, herbivore starves to death.
* Herbivore must not move to cell containing herbivore.
* @author Kevin Oane
* @version 1.0.
*/
public class Herbivore extends Entity {
/**
* Generated serialVersionUID.
*/
private static final long serialVersionUID = -4361425553964034383L;
private Cell cell;
private int life = 10;
/**
* Class constructor assigning
* herbivore object to specified
* cell location.
* @param cell
* Accepts cell location.
*/
public Herbivore(Cell cell) {
super(cell, EntityID.HERBIVORE, Color.yellow, new JLabel("H"));
this.cell = cell;
init();
}
/**
* Sets cell background to yellow.
*/
public void init() {
//super.init();
cell.setBackground(Color.yellow);
cell.add(new JLabel("H"));
}
/**
* Decreases herbivore life by 1 point if
* unable to eat a plant-cell at the end of a turn.
*/
public void decreaseLife() {
life--;
}
/**
* Puts Herbivore to location in Cell 2d array
* in world.
* @param location
* Cell coordinates
*/
public void setCell(Cell location) {
this.cell = location;
}
/**
* Gets specified cell.
* @return
* cell specified.
*/
public Cell getCell() {
return cell;
}
/**
* Moves herbivore one-cell and
* will eat a plant should adjacent
* cell contain one. In effect restores
* lifespan to maximum. Otherwise, herbivore
* moves and has lifespan decreased by 1 per turn.
* @return
*/
public void move() {
//for (cE;;)
//ArrayList <Cell> adjacent = cell.getAdjacentCells()
//int size = adjacent.size();
int moveRandom;
int size = cell.getAdjacentCells().size();
int count = 0;
do {
moveRandom = RandomGenerator.nextNumber(cell.getAdjacentCells().size() - 1);
count++;
} while ((count != size) && (!cell.getAdjacentCells().get(moveRandom).getEntity().getEntityID().equals(EntityID.PLANT))
);
if(cell.getAdjacentCells().get(moveRandom).getEntity().getEntityID().equals(EntityID.PLANT)) {
cell.eat((cell.getAdjacentCells().get(moveRandom)), cell);
} else if(cell.getAdjacentCells().get(moveRandom).getEntity().getEntityID().equals(EntityID.NIL)) {
cell.swapEntities(cell.getAdjacentCells().get(moveRandom));
life--;
}
//while moveStatus false, iterate every cell, return true, randomval
//cells that contain plant
//else, randomvalue and move
}
/**
* Returns this Herbivore's life.
* @return Lifespan
* Herbivore's lifespan.
*
*/
public int getLifeSpan() {
return life;
}
}