-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
160 lines (109 loc) · 2.72 KB
/
Square.java
File metadata and controls
160 lines (109 loc) · 2.72 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
/*
Square.java
Majority of code found online by Shawn Garrison
Editted to fit the project by George Brinzea
Used in A* algorithm
*/
package com.purdue.LawsonNavigator;
import java.util.HashSet;
import java.util.Set;
public class Square {
private int x;
private int y;
private boolean start;
private boolean end;
private double localCost; // cost of getting from this square to goal
private double parentCost; // cost of getting from parent square to this node
private double passThroughCost;// cost of getting from the start to the goal
// through this square
private Maze maze;
private Set<Square> adjacencies = new HashSet<Square>();
private Square parent;
public Square(int x, int y, Maze maze) {
this.x = x;
this.y = y;
this.maze = maze;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public boolean isEnd() {
return end;
}
public void setEnd(boolean end) {
this.end = end;
}
public Set<Square> getAdjacencies() {
return adjacencies;
}
public void setAdjacencies(Set<Square> adjacencies) {
this.adjacencies = adjacencies;
}
public Square getParent() {
return parent;
}
public void setParent(Square parent) {
this.parent = parent;
}
public void calculateAdjacencies(int[][] floorValues) {
int top = x - 1;
int bottom = x + 1;
int left = y - 1;
int right = y + 1;
if (bottom < maze.getRows()) {
if ((floorValues[bottom][y] == 0 || floorValues[bottom][y] == 5) && (floorValues[x][y] == 0 || floorValues[x][y] == 5)) {
maze.getSquare(bottom, y).addAdjacency(this);
this.addAdjacency(maze.getSquare(bottom, y));
}
}
if (right < maze.getColumns()) {
if ((floorValues[x][right] == 0 || floorValues[x][right] == 5) && (floorValues[x][y] == 0 || floorValues[x][y] == 5)) {
maze.getSquare(x, right).addAdjacency(this);
this.addAdjacency(maze.getSquare(x, right));
}
}
}
public void addAdjacency(Square square) {
adjacencies.add(square);
}
public void removeAdjacency(Square square) {
adjacencies.remove(square);
}
public double getPassThrough(Square goal) {
if (isStart()) {
return 0.0;
}
return getLocalCost(goal) + getParentCost();
}
public double getLocalCost(Square goal) {
if (isStart()) {
return 0.0;
}
localCost = 1.0 * (Math.abs(x - goal.getX()) + Math.abs(y - goal.getY()));
return localCost;
}
public double getParentCost() {
if (isStart()) {
return 0.0;
}
if (parentCost == 0.0) {
parentCost = 1.0 + .5 * (parent.getParentCost() - 1.0);
}
return parentCost;
}
}