-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridMap.java
More file actions
155 lines (137 loc) · 5.33 KB
/
Copy pathGridMap.java
File metadata and controls
155 lines (137 loc) · 5.33 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
// public class for the GridMap
public class GridMap {
// Change this variable to change ceiling of GridMap dimension.
final private int GridMap_DIM_MAX = 100;
private GridMapCell[][] GridMap;
// n*n GridMap constructor
public GridMap(int dim) {
if (dim < 3) {
throw new IllegalArgumentException("Specified dimension cannot be lower than 3.");
} else if (dim > GridMap_DIM_MAX) {
throw new IllegalArgumentException("The specified dimension is higher than the GridMap_DIM_MAX ("
+ Integer.toString(GridMap_DIM_MAX) + ").");
}
GridMap = new GridMapCell[dim][dim];
for (int r = 0; r < GridMap.length; r++){
for (int c = 0; c < GridMap[r].length; c++) {
// create the inaccessible lane every two columns
if(c % 3 == 2){
GridMap[r][c] = new GridMapCell(r,c,new InaccesibleTerrain());
}
else if(r == 0) {
GridMap[r][c] = new GridMapCell(r,c,new Nexus("ENEMY"));
}
else if (r == GridMap.length -1){
GridMap[r][c] = new GridMapCell(r,c,new Nexus("HERO"));
}
else{
Double rand = Math.random();
if(rand < 0.7){
GridMap[r][c] = new GridMapCell(r,c,new PlainTerrain());
}
else if(rand >= 0.7 && rand < 0.8){
GridMap[r][c] = new GridMapCell(r,c,new CaveTerrain());
}
else if (rand >= 0.8 && rand > 0.9){
GridMap[r][c] = new GridMapCell(r,c,new BushTerrain());
}
else{
GridMap[r][c] = new GridMapCell(r,c,new KoulouTerrain());
}
}
}
}
}
// n*m GridMap constructor
public GridMap(int row, int col) {
if (row < 3 || col < 3) {
throw new IllegalArgumentException("GridMap dimensions cannot be lower than 3");
} else if (row > GridMap_DIM_MAX || col > GridMap_DIM_MAX) {
throw new IllegalArgumentException("The specified dimension is higher than the GridMap_DIM_MAX ("
+ Integer.toString(GridMap_DIM_MAX) + ")");
}
GridMap = new GridMapCell[row][col];
for (int r = 0; r < GridMap.length; r++) {
for (int c = 0; c < GridMap[r].length; c++) {
GridMap[r][c] = new GridMapCell(r,c,new InaccesibleTerrain());
}
}
}
// toString implementation for a GridMap
public String toString() {
String GridMap_repr = "";
String divisor = "";
for (int i = 0; i < GridMap[0].length; i++) {
divisor += "+----------";
}
divisor += "+\n";
for (int r = 0; r < GridMap.length; r++) {
GridMap_repr += divisor;
for (int c = 0; c < GridMap[0].length; c++) {
GridMap_repr += "| " + GridMap[r][c] + " ";
}
GridMap_repr += "|\n";
}
GridMap_repr += divisor;
return GridMap_repr;
}
// checks if a can be moved to
public boolean check(int row, int col) {
return !(GridMap[row][col].getEntity() instanceof InaccesibleTerrain) && GridMap[row][col].heroCount() < 1;
}
public boolean canReceiveHero(int row, int col) {
return GridMap[row][col].heroCount() == 0;
}
public boolean canReceiveEnemy(int row, int col) {
return GridMap[row][col].enemyCount() == 0;
}
public GridMapCell getCellAt(int row, int col) {
return GridMap[row][col];
}
// swap two cell locations
public void swapCell(int r1, int c1, int r2, int c2) {
GridMapCell temp = GridMap[r1][c1];
GridMap[r1][c1] = GridMap[r2][c2];
GridMap[r2][c2] = temp;
GridMap[r1][c1].setLocation(r1, c1);
GridMap[r2][c2].setLocation(r2, c2);
}
// takes in an array representation of 1s and 0s and fills the GridMap to match
// that array. Locations with 1s will get tiles and locations with 0s will stay
// empty
public void fill(int[][] arr, Entity entity) {
for (int r = 0; r < GridMap.length; r++) {
for (int c = 0; c < GridMap[r].length; c++) {
if (arr[r][c] == 1) {
GridMap[r][c].setEntity(entity);
}
}
}
}
// deep copy of the GridMap
public GridMapCell[][] deep_copy() {
GridMapCell[][] deep_copy = new GridMapCell[GridMap.length][GridMap[0].length];
for (int r = 0; r < GridMap.length; r++) {
for (int c = 0; c < GridMap[0].length; c++) {
deep_copy[r][c] = GridMap[r][c];
}
}
return deep_copy;
}
// get copy of the original GridMap array
public GridMapCell[][] getGridMap() {
return GridMap;
}
// get the GridMap dimension max (GridMap dimension ceiling)
public int GET_GridMap_DIM_MAX() {
return GridMap_DIM_MAX;
}
// get the row dimension of the GridMap
public int rowCount() {
return GridMap.length;
}
// get the column dimension of the GridMap
public int colCount() {
return GridMap[0].length;
}
}