-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzle.java
More file actions
178 lines (160 loc) · 5.2 KB
/
Puzzle.java
File metadata and controls
178 lines (160 loc) · 5.2 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
package Nonogram;
import basicgraphics.BasicFrame;
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.HashMap;
public class Puzzle {
private final Dimension gridDimensions;
private String difficulty;
private int[][] correct;
private String name;
private ArrayList[] rowHints;
private ArrayList[] colHints;
public Puzzle(String name, String difficulty, int[][] data) {
this.difficulty = difficulty;
int width = data[0].length;
int height = data.length;
gridDimensions = new Dimension(width, height);
System.out.println(gridDimensions.toString());
this.name = name;
correct = data;
rowHints = new ArrayList[gridDimensions.height];
colHints = new ArrayList[gridDimensions.width];
// creates the column hints
for (int i = 0; i < gridDimensions.width; i++) {
int count = 0;
ArrayList<Integer> hints = new ArrayList();
for (int j = 0; j <= gridDimensions.height; j++) {
if (j != gridDimensions.height && correct[j][i] == 1) {
count++;
} else if (count > 0) {
hints.add(count);
count = 0;
}
}
colHints[i] = hints;
}
// creates the row hints
for (int i = 0; i < gridDimensions.height; i++) {
int count = 0;
ArrayList<Integer> hints = new ArrayList();
for (int j = 0; j <= gridDimensions.width; j++) {
if (j != gridDimensions.width && correct[i][j] == 1) {
count++;
} else if (count > 0) {
hints.add(count);
count = 0;
}
}
rowHints[i] = hints;
}
}
/**
* Checks if the player's board is equal to the solution
*
* @param boxes the players boxes
* @return if the board is correct
*/
public Boolean isEqual(GridBox[][] boxes) {
for (int i = 0; i < gridDimensions.height; i++) {
for (int j = 0; j < gridDimensions.width; j++) {
int correctStatus = correct[i][j];
int userBoxStatus = boxes[i][j].getStatus().equals(GridBox.FILLED) ? 1 : 0;
if (correctStatus != userBoxStatus) {
return false;
}
}
}
return true;
}
/**
* Get the correct solution
*
* @return
*/
public int[][] getCorrect() {
return correct;
}
/**
* Get the size of the puzzle (10, 15, 20)
*
* @return
*/
public Dimension getSize() {
return gridDimensions;
}
/**
* Get the name of the puzzle
*
* @return
*/
public String getName() {
return name;
}
/**
* Get the difficulty of the puzzle
*
* @return
*/
public String getDifficulty() {
return difficulty;
}
/**
* Get the hints for all rows
*
* @return An array of ArrayLists, the index contains the hints for that
* row's index
*/
public ArrayList[] getRowHints() {
return rowHints;
}
/**
* Get the hints for all columns
*
* @return An array of ArrayLists, the index contains the hints for that
* column's index
*/
public ArrayList[] getColumnHints() {
return colHints;
}
/**
* Generates a small and neat version of the correct puzzle for better
* viewing
*
* @return Picture of the completed puzzle
*/
public Picture getCompletedPicture() {
Picture pic;
final int miniboxHeight = MiniPicture.height / (gridDimensions.width > gridDimensions.height ? gridDimensions.width : gridDimensions.height);
Image im = BasicFrame.createImage(MiniPicture.height, MiniPicture.height);
Graphics g = im.getGraphics();
g.setColor(Color.RED);
pic = new Picture(im);
for (int i = 0; i < gridDimensions.width; i++) {
for (int j = 0; j < gridDimensions.height; j++) {
if (correct[j][i] == 1) {
g.setColor(Color.BLACK);
g.fillRect(i * miniboxHeight, j * miniboxHeight, miniboxHeight, miniboxHeight);
} else {
g.setColor(Color.WHITE);
g.fillRect(i * miniboxHeight, j * miniboxHeight, miniboxHeight, miniboxHeight);
}
}
}
return pic;
}
public String toString() {
String visualization = "";
for (int i = 0; i < gridDimensions.height; i++) {
for (int j = 0; j < gridDimensions.width; j++) {
visualization += (correct[i][j] == 1 ? "X" : "O");
}
visualization += "\n";
}
return "Puzzle \"" + name + "\", " + difficulty + " difficulty\n" + visualization;
}
}