-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberPuzzleBoard.java
127 lines (109 loc) · 3.62 KB
/
NumberPuzzleBoard.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
public class NumberPuzzleBoard {
private int[][] board;
private final int s;
private int emptyR;
private int emptyC;
private boolean isShuffled;
private int moves;
public NumberPuzzleBoard(int s) {
this.s = s;
this.board = new int[s][s];
}
public int[][] getBoard() {
return board;
}
//
public void initialize(int[][] initialGrid) {
for (int r = 0; r < s; r++) {
for (int c = 0; c < s; c++) {
board[r][c] = initialGrid[r][c];
if (board[r][c] == 0) {
emptyR = r;
emptyC = c;
}
}
}
}
public void shuffle() {
if (isShuffled) {
System.out.println("The puzzle board has already been shuffled.");
return;
}
String[] moves = {"u", "d", "l", "r"};
int shuffleCount = s * s * 100;
while (shuffleCount > 0) {
int randInd = (int) (Math.random() * 4);
String randMov = moves[randInd];
if (randMov.equals("u") && emptyR > 0) {
swap(emptyR, emptyC, emptyR - 1, emptyC);
emptyR--;
} else if (randMov.equals("d") && emptyR < s - 1) {
swap(emptyR, emptyC, emptyR + 1, emptyC);
emptyR++;
} else if (randMov.equals("l") && emptyC > 0) {
swap(emptyR, emptyC, emptyR, emptyC - 1);
emptyC--;
} else if (randMov.equals("r") && emptyC < s - 1) {
swap(emptyR, emptyC, emptyR, emptyC + 1);
emptyC++;
}
shuffleCount--;
}
isShuffled = true;
}
public void move(String move) {
if (move.equals("u") && emptyR > 0) {
swap(emptyR, emptyC, emptyR - 1, emptyC);
emptyR--;
moves++; // Increment moves
} else if (move.equals("r") && emptyC < s - 1) {
swap(emptyR, emptyC, emptyR, emptyC + 1);
emptyC++;
moves++; // Increment moves
} else if (move.equals("d") && emptyR < s - 1) {
swap(emptyR, emptyC, emptyR + 1, emptyC);
emptyR++;
moves++; // Increment moves
} else if (move.equals("l") && emptyC > 0) {
swap(emptyR, emptyC, emptyR, emptyC - 1);
emptyC--;
moves++; // Increment moves
}
}
private void swap(int r1, int c1, int r2, int c2) {
int temp = board[r1][c1];
board[r1][c1] = board[r2][c2];
board[r2][c2] = temp;
}
public boolean isSolved() {
int count = 1;
for (int r = 0; r < s; r++) {
for (int c = 0; c < s; c++) {
if (r == s - 1 && c == s - 1) {
if (board[r][c] != 0) {
return false;
}
} else {
if (board[r][c] != count) {
return false;
}
count++;
}
}
}
return true;
}
public void displayLevel(int[][] grid) {
for (int[] r : grid) {
for (int num : r) {
if (num == 0) {
System.out.print(" ");
} else {
System.out.print(num + " ");
}
}
System.out.println();
}
System.out.println();
}
}