-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot3.java
More file actions
144 lines (114 loc) · 4.55 KB
/
Copy pathBot3.java
File metadata and controls
144 lines (114 loc) · 4.55 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
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashSet;
public class Bot3 implements FireEscapeBot
{
private Grid grid;
private Fire fire;
public Bot3(Grid grid, Fire fire)
{
this.grid = grid;
this.fire = fire;
}
//uses BFS to find the shortest path to the button
public List<Cell> finding_path_for_bot_3(Cell beginning, Cell ending)
{
Set<Cell> seen_these_cells = new HashSet<>();
Queue<Cell> lineup_of_the_cells = new LinkedList<>();
seen_these_cells.add(beginning);
beginning.setParent_of_the_cell(null);
lineup_of_the_cells.add(beginning);
while(!lineup_of_the_cells.isEmpty())
{
Cell the_present_cell = lineup_of_the_cells.remove();
if(the_present_cell.equals(ending))
{
return create_path(ending);
}
//look thru the adjacent cells in the grid
int[] indices_for_the_row = {-1, 1, 0, 0}; // changed
int[] indices_for_the_colum = {0, 0, -1, 1}; // changed
for(int i = 0 ; i < 4; i++)
{
int r = the_present_cell.getRow() + indices_for_the_row[i];
int c = the_present_cell.getCol() + indices_for_the_colum[i];
if(is_it_safe_to_visit_the_cell(r,c,seen_these_cells))
{
seen_these_cells.add(grid.getCell(r,c));
Cell look_at_upcoming_cell = grid.getCell(r,c);
look_at_upcoming_cell.setParent_of_the_cell(the_present_cell);
lineup_of_the_cells.add(look_at_upcoming_cell);
}
}
}
return new LinkedList<>();
}
private boolean is_it_safe_to_visit_the_cell(int r, int c, Set<Cell> seen_these_cells)
{
return r >= 0 &&
r < grid.getCols()&&
c >= 0 &&
c < grid.getRows() &&
!seen_these_cells.contains(grid.getCell(r,c)) &&
!grid.getCell(r,c).hasInitialFire() &&
!grid.getCell(r,c).hasFire() &&
!fire.get_all_adj_open_neigbors_of_fire_cells().contains(grid.getCell(r,c)) &&
grid.getCell(r,c).isOpen();
}
private List<Cell> create_path(Cell end)
{
LinkedList<Cell> path_of_cells = new LinkedList<>();
for (Cell i = end; i != null; i = i.getParent_of_the_Cell())
{
path_of_cells.addFirst(i);
}
return path_of_cells;
}
public void move_bot(double ship_flambility) {
Cell bot = grid.getBotCell();
Cell button = grid.getButtonCell();
List<Cell> path = finding_path_for_bot_3(bot, button);
for(Cell cell:path){
System.out.println(cell.getRow() + " " + cell.getCol());
}
while(path.size() > 1) {
Cell current = path.get(0);
Cell next = path.get(1);
current.setBot(false);
next.setBot(true);
grid.setBotCell(next);
bot = next;
List<Cell> adjOpenCells = fire.get_all_adj_open_neigbors_of_fire_cells();
fire.spread_fire(adjOpenCells, ship_flambility);
if(bot.equals(button)) {
grid.printGrid();
System.out.println(bot.getRow() + " " + bot.getCol());
System.out.println(button.getRow() + " " + button.getCol());
fire.extinguish_fire();
System.out.println("Fire has been extinguished. Task completed.");
break;
}
if(!path.isEmpty()){
grid.printGrid();
path = finding_path_for_bot_3(next, button);
System.out.println("Current path:");
for(Cell cell:path){
System.out.println(cell.getRow() + " " + cell.getCol());
}
}
if(bot.hasFire()) {
grid.printGrid();
System.out.println("Current path:");
System.out.println("Bot caught on fire. Task failed.");
break;
}
if(path.isEmpty()) {
grid.printGrid();
System.out.println("Task failed. No path to button");
}
}
}
}