-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCage.java
More file actions
199 lines (171 loc) · 5.42 KB
/
Cage.java
File metadata and controls
199 lines (171 loc) · 5.42 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
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import java.util.Arrays;
enum Operator{
ADD, SUBTRACT, MULTIPLY, DIVIDE, NONE
}
class Cage {
private Operator op;
private int value;
private int[] cellIndexes;
private boolean[][] walls;
private int n;
private Label cageLabel;
Cage(Operator op, int value, int[] cellIndexes, int n){
this.op = op;
this.value = value;
this.cellIndexes = cellIndexes;
this.n = n;
this.walls = new boolean[cellIndexes.length][4];
calcWalls();
}
public String toString(){
return Arrays.toString(cellIndexes);
}
void setIncorrect(Cell[] cells){
for(int index: cellIndexes){
cells[index].correct = false;
}
}
boolean couldBeCorrect(Cell[] cells){
int total = 0;
switch(op){
case NONE:
case SUBTRACT:
case DIVIDE:
return true;
case ADD:
for(int i: cellIndexes)
total += cells[i].value;
return total < value;
case MULTIPLY:
total = 1;
for(int i: cellIndexes){
if (cells[i].value != 0 && value % cells[i].value != 0)
return false;
total *= cells[i].value;
}
return total < value;
}
return false;
}
boolean correctValues(Cell[] cells){
int total = 0;
int max;
switch(op){
case NONE:
return cells[cellIndexes[0]].value == value;
case ADD:
for(int i: cellIndexes)
total += cells[i].value;
return total == value;
case MULTIPLY:
total = 1;
for(int i: cellIndexes)
total *= cells[i].value;
return total == value;
case SUBTRACT:
max = 0;
for(int i: cellIndexes) {
total += cells[i].value;
if(cells[i].value > max){
max = cells[i].value;
}
}
return 2*max - total == value;
case DIVIDE:
total = 1;
max = 0;
for(int i: cellIndexes){
if(cells[i].value == 0)
return false;
total *= cells[i].value;
if(cells[i].value > max){
max = cells[i].value;
}
}
return max*max/total == value;
}
return false;
}
Operator getOp(){
return op;
}
int getValue(){
return value;
}
int[] getCellIndexes(){
return cellIndexes;
}
boolean isFull(Cell[] cells){
for(int index: cellIndexes){
if(cells[index].value == 0){
return false;
}
}
return true;
}
private String string(){
String contents = Integer.toString(value);
switch(op){
case ADD:
contents += "+";
break;
case SUBTRACT:
contents += "-";
break;
case MULTIPLY:
contents += "\u00D7";
break;
case DIVIDE:
contents += '\u00F7';
break;
}
return contents;
}
void showLabel(StackPane[] panes){
int first = cellIndexes[0];
cageLabel = new Label(string());
cageLabel.getStyleClass().add("cage-label-medium");
panes[first].getChildren().add(cageLabel);
StackPane.setAlignment(cageLabel, Pos.TOP_LEFT);
}
void setFont(int size){
cageLabel.getStyleClass().clear();
switch(size){
case 1:
cageLabel.getStyleClass().add("cage-label-small");
break;
case 2:
cageLabel.getStyleClass().add("cage-label-medium");
break;
case 3:
cageLabel.getStyleClass().add("cage-label-large");
break;
}
}
private void calcWalls(){
for(int a=0; a<cellIndexes.length; a++) {
for (int b = a + 1; b < cellIndexes.length; b++) {
// if index i, j are horizontally adjacent
if (cellIndexes[a] % n < n - 1 && cellIndexes[a] == cellIndexes[b] - 1) {
walls[a][1] = true;
walls[b][3] = true;
} else if (cellIndexes[a] / n < n - 1 && cellIndexes[a] == cellIndexes[b] - n) {
walls[a][2] = true;
walls[b][0] = true;
}
}
}
}
void showWalls(Cell[] cell){
for(int i=0; i<cellIndexes.length; i++){
int a = walls[i][0] ? 1: 4;
int b = walls[i][1] ? 1: 4;
int c = walls[i][2] ? 1: 4;
int d = walls[i][3] ? 1: 4;
cell[cellIndexes[i]].setStyle(String.format("-fx-background-insets: 0, %d %d %d %d ;", a, b, c, d));
}
}
}