Skip to content

Commit 2bacb48

Browse files
committed
fixes
1 parent ce0bed5 commit 2bacb48

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

week4/priority-queues/assignment-8-puzzle/Board.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public Iterable<Board> neighbors() {
109109
int emptyY = -1;
110110
for (int i = 0; i < dimension(); ++i)
111111
for (int j = 0; j < dimension(); ++j) {
112-
if (tiles[i][j] == 0) {
112+
var v = tiles[i][j];
113+
if (v == 0) {
113114
emptyX = i;
114115
emptyY = j;
115116
break;
@@ -118,9 +119,9 @@ public Iterable<Board> neighbors() {
118119

119120
ArrayList<Board> neighbors = new ArrayList<Board>();
120121
if (emptyX > 0) neighbors.add(neighbour(emptyX, emptyY, -1, 0));
121-
if (emptyX < 2) neighbors.add(neighbour(emptyX, emptyY, 1, 0));
122+
if (emptyX < dimension() - 1) neighbors.add(neighbour(emptyX, emptyY, 1, 0));
122123
if (emptyY > 0) neighbors.add(neighbour(emptyX, emptyY, 0, -1));
123-
if (emptyY < 2) neighbors.add(neighbour(emptyX, emptyY, 0, 1));
124+
if (emptyY < dimension() - 1) neighbors.add(neighbour(emptyX, emptyY, 0, 1));
124125
return neighbors;
125126
}
126127

@@ -145,9 +146,8 @@ private int goalAt(int i, int j) {
145146
}
146147

147148
private Board neighbour(int x, int y, int dx, int dy) {
148-
swap(x, y, x + dx, y + dy);
149149
Board board = new Board(tiles);
150-
swap(x, y, x + dx, y + dy);
150+
board.swap(x, y, x + dx, y + dy);
151151
return board;
152152
}
153153

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
0 1 3
2-
4 2 5
3-
7 8 6
1+
3
2+
0 1 3
3+
4 2 5
4+
7 8 6

0 commit comments

Comments
 (0)