-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
107 lines (88 loc) · 2.19 KB
/
Node.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
/* ------------------------------------------------
* 8 Tiles GUI
*
* Class: CS 342, Fall 2016
* System: Windows 10, IntelliJ IDE
* Author: Five
* ------------------------------------------------- */
import java.util.ArrayList;
/**
* Node class which encapsulates the Board with extra information used for SearchTree algorithm
* Includes an unique key for each unique board used for hashing
*/
class Node implements Comparable<Node>
{
private Board board;//Game Board state
private int goodness; //Heuristic value of Game Board
private ArrayList<Node> children; //Game Board states for possible moves
private String boardKey; //The board converted to a string
private final int SIZE = Constants.BOARD_SIZE; //Size obtained from Constants class
public Node(Board b)
{
board = b;
goodness = b.gethVal();
children = new ArrayList<>();
boardKey = boardToString(b);
}
public Board getBoard()
{
return board;
}
public int getGoodness()
{
return goodness;
}
public void setGoodness(int goodness)
{
this.goodness = goodness;
}
public ArrayList<Node> getChildren()
{
return children;
}
public String getBoardKey()
{
return boardKey;
}
/**
* Add a child node to the children list
*/
public void addChild(Node child)
{
children.add(child);
}
/**
* Implement compare to for lowest goodness value
*/
public int compareTo(Node o)
{
if(this.goodness < o.goodness)
{
return -1;
}
else if(this.goodness > o.goodness)
{
return 1;
}
else
{
return 0;
}
}
/**
* Converts the board grid into a string
*/
private String boardToString(Board b)
{
int[][] grid = b.getGrid();
String s="";
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
s+=grid[i][j];
}
}
return s;
}
}