-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
133 lines (113 loc) · 3.32 KB
/
Cell.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
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
public class Cell {
public static final int NORTH = 0;
public static final int SOUTH = 1;
public static final int EAST = 2;
public static final int WEST = 3;
private boolean neighbors[] = new boolean[4];
boolean isWallE = false;
boolean isWallS = false;
boolean isWallW = false;
boolean isWallN = false;
Location location;
int horizontalLocation = 0;
int verticalLocation = 0;
Cell northNeighbor = null;
Cell westNeighbor = null;
public boolean hasWallNorth() {
if (this.northNeighbor == null) {
return true;
}
return northNeighbor.hasWallSouth();
}
public boolean hasWallSouth() {
return isWallS;
}
public boolean hasWallEast() {
return isWallE;
}
public boolean hasWallWest() {
if (this.westNeighbor == null) {
return true;
}
return westNeighbor.hasWallEast();
}
public Cell(Location location, Cell northNeighbor, Cell westNeighbor, boolean isWallE, boolean isWallS) {
this.location = location;
this.northNeighbor = northNeighbor;
this.westNeighbor = westNeighbor;
this.isWallE = isWallE;
this.isWallS = isWallS;
}
public Location[] getAccessibleNeighborLocations() {
Location[] neighborLocations = new Location[getNeighborsNum()];
int currentNeightborNum = 0;
if (!hasWallNorth()) {
neighborLocations[currentNeightborNum] = location.getLocation(NORTH);
currentNeightborNum++;
}
if (!hasWallSouth()) {
neighborLocations[currentNeightborNum] = location.getLocation(SOUTH);
currentNeightborNum++;
}
if (!hasWallEast()) {
neighborLocations[currentNeightborNum] = location.getLocation(EAST);
currentNeightborNum++;
}
if (!hasWallWest()) {
neighborLocations[currentNeightborNum] = location.getLocation(WEST);
currentNeightborNum++;
}
return neighborLocations;
}
public int getNeighborsNum() {
int neighborsNum = 0;
if (!hasWallNorth()) {
neighborsNum++;
}
if (!hasWallSouth()) {
neighborsNum++;
}
if (!hasWallEast()) {
neighborsNum++;
}
if (!hasWallWest()) {
neighborsNum++;
}
return neighborsNum;
}
public void setNorthNeighbor(Cell northNeighbor) {
this.northNeighbor = northNeighbor;
}
public Cell getNorthNeighbor() {
return northNeighbor;
}
public void setWestNeighbor(Cell westNeighbor) {
this.westNeighbor = westNeighbor;
}
public Cell getWestNeighbor() {
return westNeighbor;
}
public String toStringEast() {
if (hasWallEast()) {
return "|";
} else {
return " ";
}
}
public String toStringSouth() {
if (hasWallSouth()) {
return "_";
} else {
return " ";
}
}
public String toString() {
return toStringSouth() + toStringEast();
}
public String toString(String substitute) {
return substitute + toStringEast();
}
public boolean equals(Cell otherCell) {
return location.equals(otherCell.location);
}
}