-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
executable file
·197 lines (159 loc) · 6.84 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
*
* @author Srikarran Sowrirajan
*/
/*
* Node class that will represent each game board tile combination
* in the game tree.
*/
/*
* Node class that will represent each game board tile combination
* in the game tree.
*/
public class Node {
private int[] board;
private Node next;
/*
* This field will denote the number of possible moves that can be made for
* the current board combination. If the empty tile is at a corner, the value
* will be 2. If the empty tile is on an edge, the value will be 3. If the
* empty tile is at the center, the value will be 4.
*/
private int emptyTile;
private int[] childBoard1 = new int[9];
private int[] childBoard2 = new int[9];
private int[] childBoard3 = new int[9];
private int[] childBoard4 = new int[9];
public Node(int[] array) {
this.board = array;
for (int i = 0; i < array.length; i++) {
if (array[i] == 0) {
emptyTile = i;
}
}
this.next = null;
evaluateChildBoards();
}
public void evaluateChildBoards() {
for(int i = 0; i < board.length; i++) {
childBoard1[i] = board[i];
childBoard2[i] = board[i];
childBoard3[i] = board[i];
childBoard4[i] = board[i];
}
// if in the corner
if (emptyTile == 0 || emptyTile == 2 || emptyTile == 6 || emptyTile == 8)
{
childBoard3 = null;
childBoard4 = null;
int value1, value2;
//need to create a for loop that will copy everything from the previous board into the new child board
if (emptyTile == 0){
value1 = board[emptyTile+1];
value2 = board[emptyTile];
childBoard1[emptyTile] = value1;
childBoard1[emptyTile + 1] = value2;
value1 = board[emptyTile + 3];
value2 = board[emptyTile];
childBoard2[emptyTile] = value1;
childBoard2[emptyTile + 3] = value2;
}
if(emptyTile == 2){
value1 = board[emptyTile - 1];
childBoard1[emptyTile] = value1;
value2 = board[emptyTile];
childBoard1[emptyTile - 1] = value2;
value1 = board[emptyTile + 3];
childBoard2[emptyTile] = value1;
value2 = board[emptyTile];
childBoard2[emptyTile + 3] = value2;
}
if(emptyTile == 6){
value1 = board[emptyTile - 3];
value2 = board[emptyTile];
childBoard1[emptyTile] = value1;
childBoard1[emptyTile -3] =value2;
childBoard2[emptyTile] = board[emptyTile + 1];
childBoard2[emptyTile + 1] = board[emptyTile];
}
if(emptyTile == 8){
childBoard1[emptyTile] = board[emptyTile - 1];
childBoard1[emptyTile - 1] = board[emptyTile];
childBoard2[emptyTile] = board[emptyTile - 3];
childBoard2[emptyTile - 3] = board[emptyTile];
}
}
// if on the edges
else if (emptyTile == 1 || emptyTile == 3 || emptyTile == 5 || emptyTile == 7)
{
childBoard4 = null;
if(emptyTile == 1) {
childBoard1[emptyTile] = board[emptyTile - 1];
childBoard1[emptyTile - 1] = board[emptyTile];
childBoard2[emptyTile] = board[emptyTile + 1];
childBoard2[emptyTile + 1] = board[emptyTile];
childBoard3[emptyTile] = board[emptyTile + 3];
childBoard3[emptyTile + 3] = board[emptyTile];
}
if(emptyTile == 3){
childBoard1[emptyTile] = board[emptyTile - 3];
childBoard1[emptyTile - 3] = board[emptyTile];
childBoard2[emptyTile] = board[emptyTile + 3];
childBoard2[emptyTile + 3] = board[emptyTile];
childBoard3[emptyTile] = board[emptyTile + 1];
childBoard3[emptyTile + 1] = board[emptyTile];
}
if(emptyTile == 5){
childBoard1[emptyTile] = board[emptyTile - 3];
childBoard1[emptyTile - 3] = board[emptyTile];
childBoard2[emptyTile] = board[emptyTile + 3];
childBoard2[emptyTile + 3] = board[emptyTile];
childBoard3[emptyTile] = board[emptyTile - 1];
childBoard3[emptyTile - 1] = board[emptyTile];
}
if(emptyTile == 7){
childBoard1[emptyTile] = board[emptyTile - 1];
childBoard1[emptyTile - 1] = board[emptyTile];
childBoard2[emptyTile] = board[emptyTile + 1];
childBoard2[emptyTile + 1] = board[emptyTile];
childBoard3[emptyTile] = board[emptyTile - 3];
childBoard3[emptyTile - 3] = board[emptyTile];
}
}
else
{
childBoard1[emptyTile] = board[emptyTile - 3];
childBoard1[emptyTile - 3] = board[emptyTile];
childBoard2[emptyTile] = board[emptyTile - 1];
childBoard2[emptyTile - 1] = board[emptyTile];
childBoard3[emptyTile] = board[emptyTile + 1];
childBoard3[emptyTile + 1] = board[emptyTile];
childBoard4[emptyTile] = board[emptyTile + 3];
childBoard4[emptyTile + 3] = board[emptyTile];
}
}
public int[] getArray() {
return this.board;
}
public int[] getChildBoard1 (){
return this.childBoard1;
}
public int[] getChildBoard2 (){
return this.childBoard2;
}
public int[] getChildBoard3 (){
return this.childBoard3;
}
public int[] getChildBoard4 (){
return this.childBoard4;
}
/*
* The following method will be triggered from the LinkedList class
*/
public Node next() {
return this.next;
}
public void setNext(Node node) {
this.next = node;
}
}