-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
364 lines (304 loc) · 12.6 KB
/
Copy pathGrid.java
File metadata and controls
364 lines (304 loc) · 12.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Grid {
private Cell[][] grid;
private int rows;
private int cols;
private Cell initial_fire_cell;
private Cell button_Cell;
private Cell bot_Cell;
private List<Cell> blockedCells_with_one_open_neighbour;
public Grid(int rows, int cols) {
this.rows = rows;
this.cols = cols;
grid = new Cell[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
grid[r][c] = new Cell(r, c);
}
}
setupGrid();
}
//sets up tgrid
private void setupGrid() {
//opens the initial cell
int row = (int) (Math.random() * rows);
int col = (int) (Math.random() * cols);
grid[row][col].setOpen(true);
List<Cell> all_blocked_cells_with_one_open_neigbor = get_all_blocked_cells_with_exactly_one_open_neighbour();
while(!all_blocked_cells_with_one_open_neigbor.isEmpty()){
all_blocked_cells_with_one_open_neigbor = get_all_blocked_cells_with_exactly_one_open_neighbour();
randomly_open_one_of_adjcells(all_blocked_cells_with_one_open_neigbor);
}
List<Cell> openCells_with_one_open_neighbour = get_open_cells_with_one_open_neighbour();
this.randomly_select_half_open_cells_with_one_open_neighbour_and_open_blocked_neigbor(openCells_with_one_open_neighbour);
List<Cell> openCells = this.get_all_open_cells();
this.generate_unique_random_fire_button_and_bot_cells(openCells);
printGrid();
}
public List<Cell> get_all_blocked_cells_with_exactly_one_open_neighbour() {
List<Cell> blockedCells_with_one_open_neighbour = new ArrayList<Cell>();
for (int r = 0; r < rows; r++) {
for (int c = 0; c< cols; c++) {
if(grid[r][c].isOpen() == false){
int count = 0;
if(r-1 >= 0 && grid[r-1][c].isOpen() == true){
count++;
}
if(r+1 < rows && grid[r+1][c].isOpen() == true){
count++;
}
if(c-1 >= 0 && grid[r][c-1].isOpen() == true){
count++;
}
if(c+1 < cols && grid[r][c+1].isOpen() == true){
count++;
}
if(count == 1){
blockedCells_with_one_open_neighbour.add(grid[r][c]);
}
}
}
}
return blockedCells_with_one_open_neighbour;
}
public void randomly_open_one_of_adjcells(List<Cell> cells) {
if (cells.isEmpty()) {
return;
}
int index = (int) (Math.random() * cells.size());
cells.get(index).setOpen(true);
}
public void setCell(int row, int col, Cell cell) {
grid[row][col] = cell;
}
public List<Cell> get_blockedCells_with_one_open_neighbour() {
if(blockedCells_with_one_open_neighbour.isEmpty()){
// System.out.println("No more blocked cells with one open neighbour");
}
return blockedCells_with_one_open_neighbour;
}
public List<Cell> get_open_cells_with_one_open_neighbour() {
List<Cell> openCells_with_one_open_neighbour = new ArrayList<Cell>();
for (int r = 0; r < rows; r++) {
for (int c = 0; c< cols; c++) {
if(grid[r][c].isOpen() == true){
int count = 0;
if(r-1 >= 0 && grid[r-1][c].isOpen() == true){
count++;
}
if(r+1 < rows && grid[r+1][c].isOpen() == true){
count++;
}
if(c-1 >= 0 && grid[r][c-1].isOpen() == true){
count++;
}
if(c+1 < cols && grid[r][c+1].isOpen() == true){
count++;
}
if(count == 1){
openCells_with_one_open_neighbour.add(grid[r][c]);
}
}
}
}
return openCells_with_one_open_neighbour;
}
public Cell getCell(int row, int col) {
return grid[row][col];
}
public List<Cell> get_all_blocked_neigbors_of_a_cell(Cell cell) {
List<Cell> blocked_neigbors = new ArrayList<Cell>();
int r = cell.getRow();
int c = cell.getCol();
if(r-1 >= 0 && grid[r-1][c].isOpen() == false){
blocked_neigbors.add(grid[r-1][c]);
}
if(r+1 < rows && grid[r+1][c].isOpen() == false){
blocked_neigbors.add(grid[r+1][c]);
}
if(c-1 >= 0 && grid[r][c-1].isOpen() == false){
blocked_neigbors.add(grid[r][c-1]);
}
if(c+1 < cols && grid[r][c+1].isOpen() == false){
blocked_neigbors.add(grid[r][c+1]);
}
return blocked_neigbors;
}
public void randomly_select_half_open_cells_with_one_open_neighbour_and_open_blocked_neigbor(List<Cell> cells) {
List<Cell> half_of_open_cells_with_open_neigCells = new ArrayList<Cell>();
int half = cells.size()/2;
for(int i = 0; i < half; i++){
int index = (int) (Math.random() * cells.size());
half_of_open_cells_with_open_neigCells.add(cells.get(index));
cells.remove(index);
}
for(Cell cell: half_of_open_cells_with_open_neigCells){
List<Cell> blocked_neigbors = get_all_blocked_neigbors_of_a_cell(cell);
if(blocked_neigbors.size() > 0){
int index = (int) (Math.random() * blocked_neigbors.size());
blocked_neigbors.get(index).setOpen(true);
}
}
}
public List<Cell> get_all_open_cells() {
List<Cell> openCells = new ArrayList<Cell>();
for (int r = 0; r < rows; r++) {
for (int c = 0; c< cols; c++) {
if(grid[r][c].isOpen() == true){
openCells.add(grid[r][c]);
}
}
}
return openCells;
}
public void printGrid() {
// Determine the width for each cell based on the maximum number of digits in row/col indices
int cellWidth = String.valueOf(Math.max(rows, cols)).length() + 1;
String format = "%" + cellWidth + "s";
// Print column indices header
System.out.print(" ");
for (int c = 0; c < cols; c++) {
System.out.printf(format, c);
}
System.out.println();
// Print top border
System.out.print(" ┌");
for (int c = 0; c < cols; c++) {
System.out.print("─".repeat(cellWidth));
if (c < cols - 1) System.out.print("┬");
}
System.out.println("┐");
// Print grid with row indices
for (int r = 0; r < rows; r++) {
System.out.printf("%2d │", r); // Print row index
for (int c = 0; c < cols; c++) {
String cellContent;
if (grid[r][c].hasButton()) {
cellContent = "B";
} else if (grid[r][c].hasFire()) {
cellContent = "*";
} else if (grid[r][c].hasBot()) {
cellContent = "T";
} else if (grid[r][c].isOpen()) {
cellContent = "O";
} else {
cellContent = "X";
}
System.out.printf(format, cellContent);
if (c < cols - 1) System.out.print("│");
}
System.out.println("│");
// Print row separator, except for the last row
if (r < rows - 1) {
System.out.print(" ├");
for (int c = 0; c < cols; c++) {
System.out.print("─".repeat(cellWidth));
if (c < cols - 1) System.out.print("┼");
}
System.out.println("┤");
}
}
// Print bottom border
System.out.print(" └");
for (int c = 0; c < cols; c++) {
System.out.print("─".repeat(cellWidth));
if (c < cols - 1) System.out.print("┴");
}
System.out.println("┘");
}
public Cell getInitialFireCell() {
return initial_fire_cell;
}
public Cell getButtonCell() {
return button_Cell;
}
public Cell getBotCell() {
return bot_Cell;
}
public void setBotCell(Cell cell) {
bot_Cell = cell;
}
public int getRows()
{
return rows;
}
public int getCols()
{
return cols;
}
public void generate_unique_random_fire_button_and_bot_cells(List<Cell> openCells) {
int index = (int) (Math.random() * openCells.size());
openCells.get(index).setButton(true);
button_Cell = openCells.get(index);
openCells.remove(index);
int index2 = (int) (Math.random() * openCells.size());
openCells.get(index2).setFire(true);
openCells.get(index2).setInitialFire(true);
initial_fire_cell = openCells.get(index2);
openCells.remove(index2);
int index3 = (int) (Math.random() * openCells.size());
openCells.get(index3).setBot(true);
bot_Cell = openCells.get(index3);
openCells.remove(index3);
}
public List<Cell> get_all_adj_open_neigbors_of_bot_cells(Cell cell) {
List<Cell> open_neigbors = new ArrayList<Cell>();
int r = cell.getRow();
int c = cell.getCol();
if(r-1 >= 0 && grid[r-1][c].isOpen() == true){
open_neigbors.add(grid[r-1][c]);
}
if(r+1 < rows && grid[r+1][c].isOpen() == true){
open_neigbors.add(grid[r+1][c]);
}
if(c-1 >= 0 && grid[r][c-1].isOpen() == true){
open_neigbors.add(grid[r][c-1]);
}
if(c+1 < cols && grid[r][c+1].isOpen() == true){
open_neigbors.add(grid[r][c+1]);
}
return open_neigbors;
}
public Cell[][] getGrid() {
return grid;
}
public static void main(String[] args) {
Grid grid = new Grid(25, 25);
Fire fire = new Fire(grid);
// //Bot 1 - This bot plans the shortest path to the button, avoiding the initial fire cell, and then executes thatplan. The spread of the fire is ignored by the bot
// Bot1 bot1 = new Bot1(grid, fire);
// bot1.move_bot(0.5);
// System.out.print(grid.getBotCell().getRow() + " " + grid.getBotCell().getCol());
// System.out.println();
// System.out.println(grid.getButtonCell().getRow() + " " + grid.getButtonCell().getCol());
// //Bot 2 - At every time step, the bot re-plans the shortest path to the button, avoiding the current fire cells,and then executes the next step in that plan.
// grid = new Grid(25, 25);
// fire = new Fire(grid);
// Bot2 bot2 = new Bot2(grid, fire);
// bot2.move_bot(0.5);
// System.out.print(grid.getBotCell().getRow() + " " + grid.getBotCell().getCol());
// System.out.println();
// System.out.println(grid.getButtonCell().getRow() + " " + grid.getButtonCell().getCol());
// Bot 3 - This bot plans the shortest path to the button, avoiding the current fire cells, and then executes the next step in that plan. The bot also considers the spread of the fire when planning its path.
grid = new Grid(25, 25);
fire = new Fire(grid);
Bot3 bot3 = new Bot3(grid, fire);
bot3.move_bot(0.8);
System.out.print(grid.getBotCell().getRow() + " " + grid.getBotCell().getCol());
System.out.println();
System.out.println(grid.getButtonCell().getRow() + " " + grid.getButtonCell().getCol());
//Bot 4 - Uses A* with distances heruisitc to find the shortest path to the button, avoiding the current fire cells, and then executes the next step in that plan. The bot also considers the spread of the fire when planning its path.
// grid = new Grid(25, 25);
// fire = new Fire(grid);
// Bot4 bot4 = new Bot4(grid, fire);
// bot4.move_bot(0.5);
// System.out.print(grid.getBotCell().getRow() + " " + grid.getBotCell().getCol());
// System.out.println();
// System.out.println(grid.getButtonCell().getRow() + " " + grid.getButtonCell().getCol());
}
}