-
Notifications
You must be signed in to change notification settings - Fork 0
/
Puzzle.java
215 lines (173 loc) · 8.27 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package sudoku;
import java.util.*;
public class Puzzle {
int[][] numbers = new int[GameBoardPanel.GRID_SIZE][GameBoardPanel.GRID_SIZE];
boolean[][] isGiven = new boolean[GameBoardPanel.GRID_SIZE][GameBoardPanel.GRID_SIZE];
//private static final int NUM_PUZZLES = 10;
private int puzzle_selection = 0;
// Constructor
public Puzzle() {
super();
}
// Generate a new puzzle given the number of cells to be guessed, which can be used
// to control the difficulty level.
// This method shall set (or update) the arrays numbers and isGiven
public void newPuzzle(int difficulty_reference_index) {
// I hardcode a puzzle here for illustration and testing.
Random rand = new Random();
puzzle_selection = rand.nextInt(10);
int[][][] hardcodedNumbers = {
{
{5, 3, 4, 6, 7, 8, 9, 1, 2},
{6, 7, 2, 1, 9, 5, 3, 4, 8},
{1, 9, 8, 3, 4, 2, 5, 6, 7},
{8, 5, 9, 7, 6, 1, 4, 2, 3},
{4, 2, 6, 8, 5, 3, 7, 9, 1},
{7, 1, 3, 9, 2, 4, 8, 5, 6},
{9, 6, 1, 5, 3, 7, 2, 8, 4},
{2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, 3, 8, 5, 2, 6, 9, 1, 7},
{7, 9, 6, 3, 1, 8, 4, 5, 2},
},
{
{9, 1, 2, 6, 8, 7, 3, 4, 5},
{3, 5, 8, 2, 1, 4, 7, 9, 6},
{6, 7, 4, 3, 9, 5, 2, 8, 1},
{8, 9, 5, 1, 7, 2, 4, 6, 3},
{2, 3, 6, 4, 5, 8, 9, 1, 7},
{7, 4, 1, 9, 6, 3, 5, 2, 8},
{5, 6, 3, 8, 4, 9, 1, 7, 2},
{4, 8, 7, 5, 2, 1, 6, 3, 9},
{1, 2, 9, 7, 3, 6, 8, 5, 4},
}};
// Copy from hardcodedNumbers into the array "numbers"
for (int row = 0; row < GameBoardPanel.GRID_SIZE; ++row) {
for (int col = 0; col < GameBoardPanel.GRID_SIZE; ++col) {
numbers[row][col] = hardcodedNumbers[puzzle_selection][row][col];
} // 随机选取一个数独矩阵
}
// Need to use input parameter cellsToGuess!
// Hardcoded for testing, only 2 cells of "8" is NOT GIVEN
boolean[][] hardcodedIsGiven =
{{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true},
{true, true, true, true, true, true, true, true, true}};
int[] oneDArray = new int[hardcodedIsGiven.length * hardcodedIsGiven[0].length];
int index = 0;
for (int i = 0; i < hardcodedIsGiven.length; i++) {
for (int j = 0; j < hardcodedIsGiven[0].length; j++) {
if (hardcodedIsGiven[i][j]) {
oneDArray[index] = 1;
} else {
oneDArray[index] = 0;
}
index++;
}
}
for (int i = 0; i < difficulty_reference_index; i++) {
index = rand.nextInt(81);
oneDArray[index] = 0;
}
System.out.println(difficulty_reference_index + (" empty cells(for debugging)"));
// Copy from hardcodedIsGiven into array "isGiven"
for (int row = 0; row < GameBoardPanel.GRID_SIZE; ++row) {
for (int col = 0; col < GameBoardPanel.GRID_SIZE; ++col) {
index = row * 9 + col;
isGiven[row][col] = ( oneDArray[index] == 1);
}
}
}
}