-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
183 lines (160 loc) · 5.4 KB
/
World.java
File metadata and controls
183 lines (160 loc) · 5.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ca.bcit.comp2526.a2a;
/**
* World
*(a) Holds Cells and containing Entities (Herbivore & Plant).
* ActionListener present to take turn.
* @author Kevin Oane
* @version 1.0.
*
*/
class World {
static int turnCount = 0;
private Cell[][] cells;
private final int rows;
private final int columns;
private int herbsRemoved = 0;
private int seeded = 0;
private Entity en;
/**
* Constructs the world.
* Holds cells.
* When world is created, cells will havce
* 20% chance = Herbivore
* 30% chance = Plant
* @param rows : X coords.
* @param cols : Y coords.
*/
World(final int rows, final int cols) {
this.rows = rows;
this.columns = cols;
this.cells = new Cell[rows][cols];
}
/**
*
* Puts the Cells on the world and adds the
* appropriate number of Herbivores and
* Plants
* Creates world.
* Connect GameFrame to Word,
* connect row & col # cell,
* cell inhabitant using RandomGenerator.
*/
public void init() {
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.columns; col++) {
Cell newCell = new Cell(this, row, col);
this.cells[row][col] = newCell;
}
}
}
/**
* Retrieves the requested Cell
* from the specified location
* in the World.
* @param row
* Row relative to world.
* @param column
* Column relative to world.
* @return cells[][]
* Cell at this array position.
*/
public Cell getCellAt(int row, int column) {
return this.cells[row][column];
}
/**
* Removes dead herbivores, checks each plant to see if it can seed
* and then moves remaining living
* Herbivores one Cell.
*/
public void takeTurn() {
removeHerbivores();
seedPlants();
moveHerbivores();
System.out.print(turnCount++);
}
/**
* Removes dead herbivores, checking if lifespan = 0.
* If so, set herbivore entity to NULL.
*/
public void removeHerbivores() {
System.out.println("Removing");
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.columns; col++) {
Entity en = this.cells[row][col].getEntity();
if ((en.getEntityID().equals(EntityID.HERBIVORE))
&& (((Herbivore) en).getLifeSpan() == 0)) {
cells[row][col].isDead();
herbsRemoved++;
}
}
}
System.out.printf("Done removing %d herbs", herbsRemoved);
}
/**
* Searches every Plant entity in every Cell.
* and invokes spreadPlant cell method on such entity.
*/
public void seedPlants() {
System.out.println("Seeding");
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.columns; col++) {
Entity en = this.cells[row][col].getEntity();
if (en.getEntityID().equals(EntityID.PLANT)) {
/* boolean sprout = false;
if (sprout == ((Plant) en).seed()) {
cells[row][col].sprout(cells[row][col]);
seeded++;
}*/
}
}
}
System.out.println("Done seeding" + seeded + " cells.");
}
/**
* Searches every Plant entity in every Cell
* and invokes moveHerbivores method on such entity.
*/
public void moveHerbivores() {
System.out.println("Moving");
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.columns; col++) {
Entity en = this.cells[row][col].getEntity();
if (en.getEntityID().equals(EntityID.HERBIVORE) &&
((Herbivore)en).getLifeSpan() > 0) {
System.out.printf("\nAt World:Row %s. Col %s\n", row, col);
System.out.printf("At World old cell: %s\n",this.cells[row][col].getEntity().getEntityID());
((Herbivore) en).move();
System.out.printf("At World old cell: %s\n",this.cells[row][col].getEntity().getEntityID());
this.cells[row][col].getEntity().init();
}
}
}
System.out.println("Done moving");
}
/* public Entity paint() {
Entity en;
for (int row = 0; row < this.rows; row++) {
for (int col = 0; col < this.columns; col++) {
en = this.cells[row][col].getEntity();
en.init();
}
}
return en;
}*/
/**
* Return number of rows in World.
* @return ROWS
* No. of rows.
*/
public int getRowCount() {
return this.rows;
}
/**
* Return number of columns in World.
* @return COLUMNS
* No. of columns.
*/
public int getColumnCount() {
return this.columns;
}
}