-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzleBoard.java
108 lines (94 loc) · 2.22 KB
/
PuzzleBoard.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class PuzzleBoard {
byte[][] board;
int SIZE;
// (x,y) coordinates of the empty square
int emptyX;
int emptyY;
/*
* Construct board from file.
*/
public PuzzleBoard(String filename) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
SIZE = Integer.parseInt(reader.readLine());
} catch (Exception e) {
}
try {
board = new byte[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
board[i] = getByteArr(reader.readLine().split(" "));
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
this.emptyX = i;
this.emptyY = j;
}
}
}
reader.close();
} catch (Exception e) {
System.out.println("I/O problem");
System.exit(0);
}
}
/*
* Returns value at (x,y)
*/
public byte get(int x, int y) {
return board[x][y];
}
//Creates deep copy of board
public PuzzleBoard(PuzzleBoard board) {
this.SIZE = board.SIZE;
this.board = new byte[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
this.board[i] = Arrays.copyOf(board.board[i], SIZE);
}
this.emptyX = board.emptyX;
this.emptyY = board.emptyY;
}
//Helper method for constructors
private byte[] getByteArr(String[] arr) {
byte[] toReturn = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
toReturn[i] = Byte.parseByte(arr[i]);
}
return toReturn;
}
//Takes swaps tiles (emptyX,empty) and (x,y)
public void swapWithEmpty(int x, int y) {
byte temp = board[emptyX][emptyY];
board[emptyX][emptyY] = board[x][y];
board[x][y] = temp;
emptyX = x;
emptyY = y;
}
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (this.getClass() != other.getClass()) {
return false;
}
final PuzzleBoard otherBoard = (PuzzleBoard) other;
return Arrays.deepEquals(this.board, otherBoard.board);
}
@Override
public int hashCode() {
return Arrays.deepHashCode(board);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
sb.append("" + this.board[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
}