-
Notifications
You must be signed in to change notification settings - Fork 1
/
Grid.java
90 lines (75 loc) · 2.84 KB
/
Grid.java
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
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
public class Grid implements Serializable {
private HashMap<Coordinate, Cell> grid;
public Grid() {
this.grid = new HashMap<Coordinate, Cell>();
GridImporter gridImporter = GridImporter.getInstance();
try {
this.grid = new Grid(gridImporter.gridImport(new File("display.w"))).grid;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Grid(Grid grid) {
this.grid = grid.grid;
}
public HashMap<Coordinate, Cell> getGrid() {
return grid;
}
public void setCellState(int x, int y, Cell.State state) {
grid.put(new Coordinate(x, y), new Cell(state));
}
public Cell.State getCellState(int x, int y) {
Cell cell = grid.get(new Coordinate(x, y));
return cell != null ? cell.getState() : Cell.State.EMPTY;
}
public Cell getCell(int x, int y) {
return grid.get(new Coordinate(x, y));
}
public void countNeighbourgs() {
Collection<Coordinate> coordinates = grid.keySet();
for(Coordinate c : coordinates) {
Cell cell = grid.get(c); // resets counter
cell.setNeighbourgs(0);
// since only a conduction can become an electron head
// i count only neighbours for conductor
if(cell.getState() == Cell.State.CONDUCTOR) {
int n = 0;
if(getCellState(c.getX() - 1, c.getY() - 1) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX() - 1, c.getY()) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX() - 1, c.getY() + 1) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX(), c.getY() + 1) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX() + 1, c.getY() + 1) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX() + 1, c.getY()) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX() + 1, c.getY() - 1) == Cell.State.ELECTRON_HEAD) n++;
if(getCellState(c.getX(), c.getY() - 1) == Cell.State.ELECTRON_HEAD) n++;
cell.setNeighbourgs(n);
grid.put(c, cell);
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Grid)) return false;
Grid grid1 = (Grid) o;
if (!grid.equals(grid1.grid)) return false;
return true;
}
@Override
public int hashCode() {
return grid.hashCode();
}
@Override
public String toString() {
return "Grid{" +
"grid=" + grid +
'}';
}
}