-
Notifications
You must be signed in to change notification settings - Fork 1
/
Puzzle.java
191 lines (157 loc) · 3.39 KB
/
Puzzle.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Puzzle {
private int[][] grid = new int[3][3];
private int emptyX;
private int emptyY;
public Puzzle() {
grid = new int[3][3];
emptyX = 0;
emptyY = 0;
}
public Puzzle(Puzzle p) {
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
grid[x][y] = p.getGrid()[x][y];
}
}
emptyX = p.getEmptyX();
emptyY = p.getEmptyY();
}
public Puzzle(String file) {
try {
readFile(file);
} catch (IOException e) {
System.out.println("File can't be read");
}
}
public boolean moveTile(int x, int y) {
boolean success = false;
/**
* Move the empty tile horizontally and vertically
*/
if (x != 0) { // move horizontally
if (x > 0 && emptyX < 2) { // move right
grid[emptyX][emptyY] = grid[emptyX + 1][emptyY];
grid[emptyX + 1][emptyY] = 0;
emptyX = emptyX + 1;
success = true;
} else if (x < 0 && emptyX > 0) { // move left
grid[emptyX][emptyY] = grid[emptyX - 1][emptyY];
grid[emptyX - 1][emptyY] = 0;
emptyX = emptyX - 1;
success = true;
}
} else if (y != 0) {
if (y > 0 && emptyY < 2) { // move down
grid[emptyX][emptyY] = grid[emptyX][emptyY + 1];
grid[emptyX][emptyY + 1] = 0;
emptyY = emptyY + 1;
success = true;
} else if (y < 0 && emptyY > 0) { // move up
grid[emptyX][emptyY] = grid[emptyX][emptyY - 1];
grid[emptyX][emptyY - 1] = 0;
emptyY = emptyY - 1;
success = true;
}
}
return success;
}
public int[] getCoordinates(int tile) {
int[] position = new int[2];
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
if (grid[x][y] == tile) {
position[0] = x;
position[1] = y;
break;
}
}
}
return position;
}
public boolean equalTo(Puzzle pg) {
boolean equal = true;
for (int y = 0; y < 3 && equal == true; y++) {
for (int x = 0; x < 3 && equal == true; x++) {
if (grid[x][y] != pg.getGrid()[x][y]) {
equal = false;
}
}
}
return equal;
}
public void readFile(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String[] data = new String[3];
String line = null;
int i = 0, j = 0;
line = br.readLine();
while (line != null) {
data = line.split(",");
for (i = 0; i <= 2; i++) {
grid[i][j] = Integer.parseInt(data[i]);
if (grid[i][j] == 0) {
emptyX = i;
emptyY = j;
}
}
j++;
line = br.readLine();
}
br.close();
}
/**
* @return the grid
*/
public int[][] getGrid() {
return grid;
}
/**
* @param grid
* the grid to set
*/
public void setGrid(int[][] grid) {
this.grid = grid;
}
/**
* @return the emptyX
*/
public int getEmptyX() {
return emptyX;
}
/**
* @param emptyX
* the emptyX to set
*/
public void setEmptyX(int emptyX) {
this.emptyX = emptyX;
}
/**
* @return the emptyY
*/
public int getEmptyY() {
return emptyY;
}
/**
* @param emptyY
* the emptyY to set
*/
public void setEmptyY(int emptyY) {
this.emptyY = emptyY;
}
public void printPuzzle() {
for (int y = 0; y < 3; y++) {
System.out.print("\t");
for (int x = 0; x < 3; x++) {
if (x == emptyX && y == emptyY)
System.out.print(" " + "\t");
else
System.out.print(grid[x][y] + "\t");
}
System.out.print("\n");
}
System.out.print("\n");
}
}