-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
150 lines (116 loc) · 3.1 KB
/
Grid.java
File metadata and controls
150 lines (116 loc) · 3.1 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
import java.util.ArrayList;
import java.util.Arrays;
/**
* Create grid of the world
* @author 1103577p
*
*/
public abstract class Grid {
protected final int NEIGHBOURS2D = 9, COORDS2D = 2;
protected int worldRows, worldCols;
protected Species[][] creatureGrid;
/**
* Constructor for Grid
* @param rows number of rows of the grid
* @param cols number of columns of the grid
*/
Grid(int rows, int cols) {
this.worldRows = rows;
this.worldCols = cols;
creatureGrid = new Species[rows][cols];
}
public int getRows() {
return worldRows;
}
public int getCols() {
return worldCols;
}
public Species[][] getCGrid() {
return creatureGrid;
}
/**
* Get Species at a specific position
*
* @param pos position of the species to retrieve
* @return
*/
public Species getSpecies(int[] pos) {
//return null if position is empty
if (creatureGrid[pos[0]][pos[1]] == null) {
return null;
}
else {
Species resident = creatureGrid[pos[0]][pos[1]];
return resident;
}
}
/**
* Setup string to print in the console
*/
public void printGrid() {
String prepGrid = "";
String showLabel = "";
for (int i = 0; i < worldRows; i++) {
for (int j = 0; j < worldCols; j++){
if (creatureGrid[i][j] == null || creatureGrid[i][j].isAlive() == false) {
showLabel = "-";
}
else {
showLabel = creatureGrid[i][j].getDisp();
}
prepGrid = prepGrid + showLabel;
if (j != (worldCols-1)) {
prepGrid = prepGrid + " ";
}
else {
prepGrid = prepGrid + "\n";
}
}
}
System.out.println(prepGrid);
}
/**
* Set a particular position of the world to a given creature
* @param pos which position to place the creature
* @param spDisp label indicating which species to place
*/
public void setCreature(int pos[], String spDisp) {
if (spDisp == Sp1.DISP) {
creatureGrid[pos[0]][pos[1]] = new Sp1(pos[0], pos[1], this);
creatureGrid[pos[0]][pos[1]].start();
}
else if (spDisp == Sp2.DISP){
creatureGrid[pos[0]][pos[1]] = new Sp2(pos[0], pos[1], this);
creatureGrid[pos[0]][pos[1]].start();
}
}
/**
* Retrieve all possible positions surrounding a given position (including itself)
* Out of bounds positions will also be returned.
* @param pos position for which to return its neighbours
* @return
*/
public int[][] getNeighbours(int[] pos) {
int[][] neighbours = new int[NEIGHBOURS2D][COORDS2D];
//get all surrounding rows and columns
int[] rows = {pos[0]-1, pos[0], pos[0]+1};
int[] cols = {pos[1]-1, pos[1], pos[1]+1};
//combined iterator
int k = 0;
// create all combinations
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
neighbours[k][0] = rows[i];
neighbours[k][1] = cols[j];
k++;
}
}
return neighbours;
}
/**
* Abstract method to allow subclasses sanitize neighbouring positions based on their nature
* @param neighbours array of all possible surrounding cells, unvalidated
* @return
*/
public abstract int[][] validateNeighbours(int[][] neighbours);
}