-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
303 lines (279 loc) · 10.3 KB
/
Grid.java
File metadata and controls
303 lines (279 loc) · 10.3 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package Nonogram;
import basicgraphics.Sprite;
import basicgraphics.SpriteComponent;
import basicgraphics.*;
import basicgraphics.images.Picture;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Grid extends Sprite {
public static Dimension puzzleDimensions;
public static int boxSize;
public static final int GRID_OFFSET = 200;
public static Picture filled;
public static Picture crossed;
public static Picture empty;
public GridBox[][] boxes;
public Puzzle puzzle;
public static Picture[] numberHints = new Picture[69];
private List<GridBox> history = new ArrayList();
private int historyIndex = 0;
public Grid(SpriteComponent sc, String difficulty, Dimension customBoard) {
super(sc);
if (!difficulty.equals("custom")) {
PuzzleLevels allLevels = new PuzzleLevels();
ArrayList<Puzzle> puzzles = allLevels.levels.get(difficulty);
int randNum = new Random().nextInt(puzzles.size());
puzzle = puzzles.get(randNum);
} else {
puzzle = new Puzzle("blank", "custom", new int[customBoard.width][customBoard.height]);
}
puzzleDimensions = puzzle.getSize();
boxSize = (Nonogram.GAME_DIMENSIONS - Grid.GRID_OFFSET) / Math.max(puzzleDimensions.width, puzzleDimensions.height);
super.setPicture(drawGrid());
super.setDrawingPriority(9);
super.setX(0);
super.setY(0);
loadPictures();
// creating the empty boxes
boxes = new GridBox[puzzleDimensions.height][puzzleDimensions.width];
for (int i = 0; i < puzzleDimensions.height; i++) {
for (int j = 0; j < puzzleDimensions.width; j++) {
GridBox box = new GridBox(sc, i, j);
boxes[i][j] = box;
}
}
drawHints(sc);
}
/**
* Loads all images so that new images aren't loaded every time a box is
* changed
*/
private void loadPictures() {
filled = new Picture("filled.png").resize((float) boxSize / 44);
crossed = new Picture("crossed.png").resize((float) boxSize / 44);
Image im = BasicFrame.createImage(boxSize, boxSize);
Graphics g = im.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, boxSize - 1, boxSize - 1);
empty = new Picture(im);
for (int i = 0; i < numberHints.length; i++) {
numberHints[i] = new Picture((i + 1) + ".png").resize((float) boxSize / 44);
}
}
/**
* Draws the grid outline based on the puzzles dimensions and game dimensions
*
* @return
*/
private Picture drawGrid() {
Image im = BasicFrame.createImage(Nonogram.GAME_DIMENSIONS, Nonogram.GAME_DIMENSIONS);
Graphics g = im.getGraphics();
for (int i = 0; i <= puzzleDimensions.width; i++) {
g.setColor(i % 5 == 0 ? Color.BLACK : Color.LIGHT_GRAY);
g.drawLine(i * boxSize + GRID_OFFSET, 0, i * boxSize + GRID_OFFSET, puzzleDimensions.height * boxSize + GRID_OFFSET);
}
for (int i = 0; i <= puzzleDimensions.height; i++) {
g.setColor(i % 5 == 0 ? Color.BLACK : Color.LIGHT_GRAY);
g.drawLine(0, i * boxSize + GRID_OFFSET, puzzleDimensions.width * boxSize + GRID_OFFSET, i * boxSize + GRID_OFFSET);
}
Picture p = new Picture(im);
return p;
}
/**
* Draws the hints and neatly positions them to the respective row/column
*
* @param sc
*/
private void drawHints(SpriteComponent sc) {
ArrayList<Integer>[] rowHints = puzzle.getRowHints();
ArrayList<Integer>[] colHints = puzzle.getColumnHints();
// row hints
for (int i = 0; i < puzzleDimensions.height; i++) {
ArrayList<Integer> rowHint = rowHints[i];
for (int j = 0; j < rowHint.size(); j++) {
NumberHint hint = new NumberHint(sc, rowHint.get(j), boxSize);
int baseX = 0;
int baseY = GRID_OFFSET + i * boxSize + 10;
hint.setY(baseY);
hint.setX(baseX + (j * 30));
}
}
// column hints
for (int i = 0; i < puzzleDimensions.width; i++) {
ArrayList<Integer> colHint = colHints[i];
for (int j = 0; j < colHint.size(); j++) {
NumberHint hint = new NumberHint(sc, colHint.get(j), boxSize);
int baseX = GRID_OFFSET + i * boxSize + 1;
int baseY = 0;
hint.setY(baseY + (j * 30));
hint.setX(baseX);
}
}
}
/**
* Loops through all boxes and changes them to the correct solution
*/
public void autoSolve() {
int[][] correct = puzzle.getCorrect();
for (int i = 0; i < puzzleDimensions.height; i++) {
for (int j = 0; j < puzzleDimensions.width; j++) {
if (correct[i][j] == 1 && !boxes[i][j].getStatus().equals("filled")) {
boxes[i][j].changeFilled();
} else {
boxes[i][j].changeCrossed();
}
}
}
}
/**
* Generates the data when making a custom puzzle. Instead of manually
* typing the puzzle data, you can draw it on the creation screen and
* generate the data this way
*
* @return
*/
public String generateCustomPuzzle() {
String output = "";
for (int i = 0; i < puzzleDimensions.height; i++) {
output += "{";
for (int j = 0; j < puzzleDimensions.width; j++) {
if (boxes[i][j].getStatus().equals("filled")) {
output += "1";
} else {
output += "0";
}
if (j < puzzleDimensions.width - 1) {
output += ", ";
}
}
output += "},\n";
}
return output;
}
/**
* Reveals the correct status for a given box
*
* @param col
* @param row
*/
public void showHint(int col, int row) {
if (puzzle.getCorrect()[col][row] == 1) {
if (boxes[col][row].getStatus().equals(GridBox.FILLED)) {
return;
}
boxes[col][row].changeFilled();
} else {
if (boxes[col][row].getStatus().equals(GridBox.CROSSED)) {
return;
}
boxes[col][row].changeCrossed();
}
}
/**
* Resets the board to all empty boxes
*/
public void restart() {
for (int i = 0; i < puzzleDimensions.width; i++) {
for (int j = 0; j < puzzleDimensions.height; j++) {
GridBox current = boxes[i][j];
if (!current.getStatus().equals(GridBox.EMPTY)) {
if (current.getStatus().equals(GridBox.FILLED)) {
current.changeFilled();
} else {
current.changeCrossed();
}
}
}
}
}
/**
* Saves the action the user made so that it can be undone
*
* @param pressed The box that was pressed
*/
public void recordAction(GridBox pressed) {
if (historyIndex < history.size() - 1) {
history = history.subList(0, historyIndex + 1);
}
history.add(pressed);
historyIndex = history.size() - 1;
}
/**
* Undo the the move made
*
*/
public void undo() {
if (historyIndex == -1) {
return;
}
GridBox current = history.get(historyIndex);
current.setToPreviousHistory();
historyIndex--;
}
/**
* Checks if the current row and column matches the given hints for that row
* or column. If it matches, empty boxes in that row or column are crossed
* out
*
* @param row
* @param col
*/
public void checkAutoFill(int row, int col) {
ArrayList<Integer> rowHints = puzzle.getRowHints()[row];
ArrayList<Integer> colHints = puzzle.getColumnHints()[col];
ArrayList<Integer> userRow = new ArrayList<>();
ArrayList<Integer> userCol = new ArrayList<>();
int count = 0;
// gets the user's row data
for (int i = 0; i <= puzzleDimensions.height; i++) {
if (i != puzzleDimensions.height && boxes[i][col].getStatus().equals(GridBox.FILLED)) {
count++;
} else if (count > 0) {
userCol.add(count);
count = 0;
}
}
// gets the user's column data
for (int i = 0; i <= puzzleDimensions.width; i++) {
if (i != puzzleDimensions.width && boxes[row][i].getStatus().equals(GridBox.FILLED)) {
count++;
} else if (count > 0) {
userRow.add(count);
count = 0;
}
}
// compare the user row data to the correct row data
// it doesn't have to be in correct order, it just has to be the correct number of filled boxes
if (userRow.equals(rowHints)) {
for (int i = 0; i < puzzleDimensions.width; i++) {
if (boxes[row][i].getStatus().equals(GridBox.EMPTY)) {
boxes[row][i].changeCrossed();
}
}
}
//compare the user column data to the correct column data
if (userCol.equals(colHints)) {
for (int i = 0; i < puzzleDimensions.height; i++) {
if (boxes[i][col].getStatus().equals(GridBox.EMPTY)) {
boxes[i][col].changeCrossed();
}
}
}
}
/**
* Sets all the box sprites active to false in preparation for a new board
*/
public void reset() {
for(int i = 0; i < boxes.length; i++) {
for(int j = 0; j < boxes[0].length; j++) {
boxes[i][j].setActive(false);
}
}
}
}