-
Notifications
You must be signed in to change notification settings - Fork 0
/
PuzzleNode.java
175 lines (143 loc) · 5.07 KB
/
PuzzleNode.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
public class PuzzleNode {
private int[][] puzzle = null;
private int boardSize = 0;
private int fromInitialCost;
private int blankRow = 0;
private int blankColumn = 0;
private PuzzleNode prevState;
private String move;
public int getBoardSize() {
return boardSize;
}
public void setBoardSize(int boardSize) {
this.boardSize = boardSize;
}
public int[][] getpuzzle() {
return puzzle;
}
public void setpuzzle(int[][] puzzle) {
this.puzzle = puzzle;
}
public int getFromInitialCost() {
return fromInitialCost;
}
public void setFromInitialCost(int fromInitialCost) {
this.fromInitialCost = fromInitialCost;
}
public int getBlankRow() {
return blankRow;
}
public void setBlankRow(int blankRow) {
this.blankRow = blankRow;
}
public int getBlankColumn() {
return blankColumn;
}
public void setBlankColumn(int blankColumn) {
this.blankColumn = blankColumn;
}
public PuzzleNode getPrevState() {
return prevState;
}
public void setPrevState(PuzzleNode prevState) {
this.prevState = prevState;
}
public String getMove() {
return move;
}
public void setMove(String move) {
this.move = move;
}
public PuzzleNode(int[][] puzzle, int size, PuzzleNode prevState) {
this.puzzle = puzzle;
this.boardSize = size;
int k = 0;
if (prevState != null) {
fromInitialCost = prevState.fromInitialCost + 1;
} else {
fromInitialCost = 0;
}
}
public int getInverseCount(int[] arr) {
int inverseCount = 0;
for (int i = 0; i < boardSize*boardSize; i++) {
for (int j = i + 1; j < boardSize*boardSize; j++) {
if (arr[i] != 0 && arr[j] != 0 && arr[j] < arr[i]) {
inverseCount++;
}
}
}
return inverseCount;
}
public int blankFinder() {
for (int i = boardSize - 1; i >= 0; i--) {
for (int j = boardSize - 1; j >= 0; j--) {
if (puzzle[i][j] == 0) {
this.blankRow = i;
this.blankColumn = j;
return boardSize - i;
}
}
}
return -1;
}
public boolean isSolvable() {
int arr[];
arr = new int[boardSize * boardSize];
int k = 0;
for (int i = 0; i < boardSize; i++)
for (int j = 0; j < boardSize; j++)
arr[k++] = puzzle[i][j];
int inversionCount = getInverseCount(arr);
if (boardSize*boardSize % 2 == 1)
return inversionCount % 2 == 0;
else {
int blank = blankFinder();
if (blank % 2 == 1)
return inversionCount % 2 == 0;
else
return inversionCount % 2 == 1;
}
}
public PuzzleNode neighbourGenerator(String action) {
int[][] newBoard = new int[boardSize][boardSize];
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
newBoard[i][j] = puzzle[i][j];
}
}
if (action.equalsIgnoreCase("right")) {
newBoard[blankRow][blankColumn] = puzzle[blankRow][blankColumn - 1];
newBoard[blankRow][blankColumn - 1] = 0;
PuzzleNode newNode = new PuzzleNode(newBoard, boardSize, this);
newNode.setMove("right");
newNode.setBlankColumn(this.blankColumn - 1);
newNode.setBlankRow(this.blankRow);
return newNode;
} else if (action.equalsIgnoreCase("left")) {
newBoard[blankRow][blankColumn] = puzzle[blankRow][blankColumn + 1];
newBoard[blankRow][blankColumn + 1] = 0;
PuzzleNode newNode = new PuzzleNode(newBoard, boardSize, this);
newNode.setMove("left");
newNode.setBlankColumn(this.blankColumn + 1);
newNode.setBlankRow(this.blankRow);
return newNode;
} else if (action.equalsIgnoreCase("up")) {
newBoard[blankRow][blankColumn] = puzzle[blankRow + 1][blankColumn];
newBoard[blankRow + 1][blankColumn] = 0;
PuzzleNode newNode = new PuzzleNode(newBoard, boardSize, this);
newNode.setMove("up");
newNode.setBlankColumn(this.blankColumn);
newNode.setBlankRow(this.blankRow + 1);
return newNode;
} else {
newBoard[blankRow][blankColumn] = puzzle[blankRow - 1][blankColumn];
newBoard[blankRow - 1][blankColumn] = 0;
PuzzleNode newNode = new PuzzleNode(newBoard, boardSize, this);
newNode.setMove("down");
newNode.setBlankColumn(this.blankColumn);
newNode.setBlankRow(this.blankRow - 1);
return newNode;
}
}
}