-
Notifications
You must be signed in to change notification settings - Fork 1
/
AStarState.java
198 lines (176 loc) · 4.41 KB
/
AStarState.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
198
import java.util.Arrays;
import java.util.ArrayList;
public class AStarState{
private int sizeOfPuzzle = 9;
private int misplacedTiles = 0;
private int manhattanDistance = 0;
private int[] goalState = new int[]
{ 1, 2, 3, 4, 5, 6, 7, 8, 0 };
private int[] currentBoard;
/**
* Constructor for State
*/
public AStarState(int[] board){
currentBoard = board;
misplacedTiles = calculateMisplacedTiles();
manhattanDistance = calculateManhattanDistance();
}
/**
* Getter for the currentBoard
*/
public int[] getCurrentBoard(){
return currentBoard;
}
/**
* Getter for the misplacedTiles value
*/
public int getMisplacedTiles(){
return misplacedTiles;
}
/**
* Getter for the manhattanDistance value
*/
public int getManhattanDistance(){
return manhattanDistance;
}
/*
* Calculates the number of misplaced tiles in the current board
*/
private int calculateMisplacedTiles(){
int count = 0;
for (int i = 0; i < currentBoard.length; i++){
if (currentBoard[i] != goalState[i]){
count++;
}
}
return count++;
}
/*
* Calculates the Manhattan Distance
*/
private int calculateManhattanDistance(){
int count = 0;
int index = -1;
for (int y = 0; y < 3; y++){
for (int x = 0; x < 3; x++){
index++;
// subtract 1 from the value of the current board at index to assign
// -1 to value when the tile is blank
int value = (currentBoard[index] - 1);
/*
* Checks the manhattan distance of tiles excluding the blank tile
* since value of blank tile = -1
*/
if (value != -1){
int horizontalMove = value % 3;
int verticalMove = value / 3;
count += Math.abs(verticalMove - (y)) + Math.abs(horizontalMove - (x));
}
}
}
return count;
}
/*
* Locates the blank tile and returns the index of it
*/
private int getIndexOfBlankTile(){
int blankIndex = -1;
for (int i = 0; i < sizeOfPuzzle; i++){
if (currentBoard[i] == 0)
blankIndex = i;
}
return blankIndex;
}
/**
* Prints the current state of the board.
*/
public void printBoard(){
int counter=1;
for(int x=0; x<currentBoard.length; x++){
if(counter<3){
System.out.print(currentBoard[x]+" ");
counter++;
} else{
System.out.println(currentBoard[x]+" ");
counter=1;
}
}
}
/*
* Makes a copy of the board
*/
private int[] copyBoard(int[] board){
int[] duplicate = new int[sizeOfPuzzle];
for (int i = 0; i < sizeOfPuzzle; i++)
{
duplicate[i] = board[i];
}
return duplicate;
}
/*
* Switches the data at index1 and index2 in a copy of the current board
*/
private void moveTile(int index1, int index2, ArrayList<AStarState> state){
int[] duplicate = copyBoard(currentBoard);
int temp = duplicate[index1];
duplicate[index1] = currentBoard[index2];
duplicate[index2] = temp;
state.add((new AStarState(duplicate)));
}
/**
* Returns all the Successors
*/
public ArrayList<AStarState> generateSuccessors(){
ArrayList<AStarState> successors = new ArrayList<AStarState>();
int blankIndex = getIndexOfBlankTile();
if (blankIndex != 0 && blankIndex != 3 && blankIndex != 6){
moveTile(blankIndex - 1, blankIndex, successors);
}
if (blankIndex != 6 && blankIndex != 7 && blankIndex != 8){
moveTile(blankIndex + 3, blankIndex, successors);
}
if (blankIndex != 0 && blankIndex != 1 && blankIndex != 2){
moveTile(blankIndex - 3, blankIndex, successors);
}
if (blankIndex != 2 && blankIndex != 5 && blankIndex != 8)
{
moveTile(blankIndex + 1, blankIndex, successors);
}
return successors;
}
/**
* Checks if the current state is the goal state.
*/
public boolean isGoal(){
if (Arrays.equals(currentBoard, goalState)){
return true;
} else{
return false;
}
}
/**
* Equals method to compare two states.
*/
public boolean equals(AStarState s){
if (Arrays.equals(currentBoard, ((AStarState) s).getCurrentBoard())){
return true;
}
else{
return false;
}
}
/*
* Checks if a Node has already been checked.
*/
public static boolean isRepeatedState(Node n){
boolean flag = false;
Node node = n;
while (n.getParent() != null && !flag){
if (n.getParent().getCurrentState().equals(node.getCurrentState())){
flag = true;
}
n = n.getParent();
}
return flag;
}
}