-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
132 lines (106 loc) · 3.01 KB
/
Node.java
File metadata and controls
132 lines (106 loc) · 3.01 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
/**
*
* @author Zafrir
*/
public class Node implements Comparable<Node>{
private int x;
private int y;
int f,g,h;
private Node parent;
private int depth;
private char letter;
public Node(int x, int y, Node parent,char letter) {
this.x = x;
this.y = y;
this.parent = parent;
this.letter=letter;
this.g=Integer.MAX_VALUE;
}
public char getLetter()
{
return letter;
}
public void setLetter(char letter){
this.letter=letter;
}
public int getDepth() {
return depth;
}
public int getG() {
return g;
}
public void setDepth(int depth) {
this.depth = depth;
}
public void setG(int g) {
this.g = g;
}
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 Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
private int heuristic(Node finish) {
int distance = (int) Math.sqrt(Math.pow(this.getX() - finish.getX(), 2) + Math.pow(this.getY() - finish.getY(), 2));
return distance;
}
public void calcF(Node finish){
f=heuristic(finish)+this.getG();
}
private int getF(){
return f;
}
@Override
public boolean equals(Object o){
if(o==null || getClass()!=o.getClass())return false;
if(((Node) o).x==this.x && ((Node)o).y==this.y )
return true;
return false;
}
@Override
public String toString(){
return "("+x+","+y+")";
}
@Override
public int hashCode() {
int result =13;
result=31*result+x;
result=31*result+y;
return result;
}
public int getPriceOfMat(){
int ans=0;
switch (this.letter){
case 'D': ans=2;
break;
case 'R': ans=3;
break;
case 'H': ans=5;
break;
case 'G': ans=5;
break;
}
return ans;
}
@Override
public int compareTo(Node o) {
if(this.getF()<o.getF())
return -1;
else if(o.getF()<this.getF())
return 1;
return 0;
}
}