-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFire.java
More file actions
92 lines (81 loc) · 2.78 KB
/
Copy pathFire.java
File metadata and controls
92 lines (81 loc) · 2.78 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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Fire {
Grid grid;
public Fire(Grid grid) {
this.grid = grid;
}
public List<Cell> getFireCells(){
ArrayList<Cell> fire_cells = new ArrayList<Cell>();
for(int i = 0; i < grid.getRows(); i++)
{
for(int j = 0; j < grid.getCols(); j++)
{
if(grid.getCell(i,j).hasFire())
{
fire_cells.add(grid.getCell(i,j));
}
}
}
return fire_cells;
}
public List<Cell> get_all_adj_open_neigbors_of_fire_cells() {
List<Cell> fire_cells = getFireCells();
List<Cell> open_neigbors = new ArrayList<Cell>();
for(Cell cell: fire_cells){
int r = cell.getRow();
int c = cell.getCol();
if(r-1 >= 0 && grid.getCell(r-1,c).isOpen() == true){
open_neigbors.add(grid.getCell(r-1,c));
}
if(r+1 < grid.getRows() && grid.getCell(r+1,c).isOpen() == true){
open_neigbors.add(grid.getCell(r+1,c));
}
if(c-1 >= 0 && grid.getCell(r,c-1).isOpen() == true){
open_neigbors.add(grid.getCell(r,c-1));
}
if(c+1 < grid.getCols() && grid.getCell(r,c+1).isOpen() == true){
open_neigbors.add(grid.getCell(r,c+1));
}
}
return open_neigbors;
}
public int get_number_of_adj_fire_cells(Cell cell){
int r = cell.getRow();
int c = cell.getCol();
int count = 0;
if(r-1 >= 0 && grid.getCell(r-1,c).hasFire() == true){
count++;
}
if(r+1 < grid.getRows() && grid.getCell(r+1,c).hasFire() == true){
count++;
}
if(c-1 >= 0 && grid.getCell(r, c-1).hasFire() == true){
count++;
}
if(c+1 < grid.getCols() && grid.getCell(r, c+1).hasFire() == true){
count++;
}
return count;
}
public void spread_fire(List<Cell> open_neigbors, double ship_flambility) {
for (Cell cell: open_neigbors){
int r = cell.getRow();
int c = cell.getCol();
double probaility_of_open_neigbor_cell_catching_fire = Math.pow(1 - (1 - ship_flambility), get_number_of_adj_fire_cells(cell));
double random = Math.random();
if(random < probaility_of_open_neigbor_cell_catching_fire){
grid.getCell(r,c).setFire(true);
}
}
}
public void extinguish_fire() {
List<Cell> fire_cells = getFireCells();
for (Cell cell : fire_cells) {
int r = cell.getRow();
int c = cell.getCol();
grid.getCell(r, c).setFire(false);
}
}
}