-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
222 lines (209 loc) · 5.59 KB
/
Grid.java
File metadata and controls
222 lines (209 loc) · 5.59 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.ArrayList;
public class Grid {
//Variable Declaration
private Animal[][] _grid;
private int sideLength;
private Object[][] _usedCors;
//Constructor
/*==============
Creates a grid with Animals (Empty squares hold "Empty", a subclass of Animal)
Creates a 2D Array to store used coordinates
===============*/
public Grid (int s){
sideLength = s;
_grid = new Animal[sideLength][sideLength];
_usedCors = new Object[sideLength][sideLength];
initPopulateUsed();
}
//Adjustable Grid: maxes out at 16x16 grid
public String toString(){
String retStr = " ";
//Sets the x-axis
for(int x = 1; x <= sideLength; x++){
if( x == sideLength){
retStr += Integer.toString(x) + "\n";
}
else{
if( x >= 10 ) {
retStr += Integer.toString(x) + " ";
}
else{
retStr += Integer.toString(x) + " ";
}
}
}
String horizontal = " -";
//Sets the horizontal grid lines
for(int x = 0; x < sideLength; x++){
if( x == sideLength - 1){
horizontal += "------\n";
}
else{
horizontal += "------";
}
}
retStr += horizontal;//??
int count = 1;
for (int l = 0; l < _grid.length; l ++){
//if a double digit number
if (count > 9){
retStr += " " + count + "|";
}
//else: a single digit number
else{
retStr += " " + count + "|";
}
for (int w = 0; w < _grid[l].length; w++){
//if the coordinates haven't been accessed yet
if (!(this.getUsedCors()[l][w] instanceof Empty)){
if ((_grid[l][w] instanceof Prey) || (_grid[l][w] instanceof Predator)){
retStr += _grid[l][w].getSusName() + "|";
}
else{retStr += _grid[l][w].getSusName() + "|";}
}
else{
retStr += " |";
}
}
retStr += "\n";
retStr += horizontal;
count ++;
}
return retStr;
}
//Overloaded toString() method in order to diagnose errors
public String toString(String diag){
String retStr = " 1 2 3 4 5 6 7 8 9\n";
retStr += " -------------------------------------------------------\n";
int count = 1;
for (Animal[] a : _grid){
retStr += " " + count + "|";
for (Animal b: a){
if ((b instanceof Prey) || (b instanceof Predator)){retStr += b.getSusName() + "|";}
else{retStr += b + "|";}
}
retStr += "\n";
retStr += " -------------------------------------------------------\n";
count ++;
}
return retStr;
}
//ACCESSORS
//Returns sidelength of hunting grounds
public int getSideLength(){
return sideLength;
}
//Returns the grid
public Animal[][] getArray(){
return _grid;
}
//Returns the element at (xcor, ycor)
public Object getCor (int xcor, int ycor){
return _grid[ycor - 1][xcor - 1];
}
//Returns the 2D array of used coordinates
public Object[][] getUsedCors(){
return _usedCors;
}
//MUTATORS
//Updates the used coordinates 2D array
public Object setUsedCors(Integer xcor, Integer ycor, Integer a){
Object temp = _usedCors[ycor][xcor];
_usedCors[ycor][xcor] = a;
return temp;
}
/*====
Populate
Using nested for loops, populates a 2D Array with Animals in it.
For MVP, the spawn probability for each Animal is as shown below:
Predator: 1/5
Prey: 1/5
Empty: 3/5
====*/
public static int populate(Grid s){
int numPrey = 0;
int damage = 0;
int numAnimals = (int)(s.sideLength*s.sideLength*0.1);
int predCount = 0;
while (predCount <= numAnimals){
int rand1 = (int) (Math.random()*s.sideLength);
int rand2 = (int) (Math.random()*s.sideLength);
if (s._grid[rand1][rand2] instanceof Animal){}
else{
//if you reached half of the predator count...
if (predCount == (int)(numAnimals/2)){
//...and the damage is not 50 HP yet.
if (damage < 50){
//Call the overloaded constructor, granting the animal enough damage.
Animal a = new Predator(50-damage);
damage += ((Predator)a).getDamage();
s._grid[rand1][rand2] = a;
predCount ++;
}
else{
Animal d = new Predator();
s._grid[rand1][rand2] = d;
predCount ++;
}
}
//if you're on the last predator
else if (predCount == numAnimals){
//if there isn't enough cumulative damage to kill the Cazador (100 HP)
if (damage < 100){
Animal b = new Predator(100-damage);
s._grid[rand1][rand2] = b;
predCount ++;
}
else{
Animal c = new Predator();
s._grid[rand1][rand2] = c;
predCount ++;
}
}
else{
Animal e = new Predator();
damage += ((Predator)e).getDamage();
s._grid[rand1][rand2] = e;
predCount ++;
}
}
}
//Make sure there are enough prey as well.
while (numPrey <= numAnimals){
int rand1 = (int) (Math.random()*s.sideLength);
int rand2 = (int) (Math.random()*s.sideLength);
if (s._grid[rand1][rand2] instanceof Animal){}
else{
Animal f = new Prey();
s._grid[rand1][rand2] = f;
numPrey ++;
}
}
//Populate the remaining squares with empties
for (int i = 0; i < s.sideLength; i++) {
for (int j = 0; j < s.sideLength; j++) {
if (s._grid[i][j] instanceof Animal) {
}
else {
Animal g = new Empty(i, j);
s._grid [i][j] = g;
}
}
}
return numPrey;
}
//Populates the 2D Array with Used Coordinates with Empties
public void initPopulateUsed(){
for (int i = 0; i < _usedCors.length; i ++){
for (int j = 0; j < _usedCors.length; j ++){
Animal a = new Empty();
_usedCors[i][j] = a;
}
}
}
public static void main(String[] args){
Grid a = new Grid(5);
populate(a);
System.out.println(a);
}
}