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
|
import java.util.Scanner;
import java.io.*;
public class KPuzzle
{
//a 2-d array of the cages, cages that are more than 1 square will have alieases on all of the squares.
KCage[] p;//The puzzle
int size;
boolean[] p2; //The possiblilities for every square - 3D
public KPuzzle()
{
}
public KPuzzle(String filename)
{
parsePuzzle(filename);
makePossible();
checkCages();
}
private void checkCages()
{
for(int i = 0; i < p.length; i++)
{
KCage c = p[i];
if(c.getNumCells() == 1)
{
Cell cell = c.getCell(0);
this.putNumber(cell.getX(),cell.getY(),c.getTargNumber());
System.out.println("After putting " + c.getTargNumber() + " into (" + cell.getX() + "," + cell.getY() + ") Possibilities are:");
this.printPossibilities();
return;
}
}
}
public Cell getCell(int x, int y)
{
KCage k = p[y*size + x];
for(int i = 0; i < k.getNumCells();i++)
{
Cell c = k.getCell(i);
if(c.getX() == x && c.getY() == y)
{
return c;
}
}
return null;
}
public boolean isComplete()
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
int possibilitiesForCell = 0;
for(int k = 0; k < size; k++)
{
if(p2[(i*size*size)+(j*size)+k])
{
possibilitiesForCell++;
}
}
if(possibilitiesForCell != 1)
{
return false;
}
}
}
return true;
}
public boolean[] getPossibilities(int xn, int yn)
{
boolean[] output = new boolean[size];
for(int i = 0; i < size; i++)
{
output[i] = p2[(yn*size*size)+(xn*size)+i];
}
return output;
}
public void removePossibility(int xn, int yn, int num)
{
p2[(yn*size*size)+(xn*size)+num-1] = false;
}
public void addPossibility(int xn, int yn, int num)
{
p2[(yn*size*size)+(xn*size)+num-1] = true;
}
public void putNumber(int xn, int yn, int num)
{
System.out.println("putting " + num + " in (" + xn + "," + yn + ")");
for(int y = 0; y < size; y++)
{
for(int z = 0; z < size; z++)
{
if(z == num-1)
{
//System.out.println("Setting (" + xn + "," + y + "," + z + ") to false");
p2[(y*size*size)+(xn*size)+z] = false;
}
}
}
for(int x = 0; x < size; x++)
{
for(int z = 0; z < size; z++)
{
if(z == num-1)
p2[(yn*size*size)+(x*size)+z] = false;
}
}
for(int z = 0; z < size; z++)
{
p2[(yn*size*size)+(xn*size)+z] = false;
}
System.out.println("Setting (" + xn + "," + yn + "," + (num-1) + ") to true");
p2[(yn*size*size)+(xn*size)+num-1] = true;
}
public void takeNumber(int xn, int yn)
{
int thisnum = -1;
for(int z = 0; z < size; z++)
{
if(p2[(yn*size*size)+(xn*size)+z])
thisnum = z;
}
if(thisnum == -1)
{
System.out.println("I couldn't find a number there!");
return;
}
for(int y = 0; y < size; y++)
{
for(int z = 0; z < size; z++)
{
if(z == thisnum)
{
System.out.println("Setting (" + xn + "," + y + "," + z + ") to true");
p2[(y*size*size)+(xn*size)+z] = true;
}
}
}
for(int x = 0; x < size; x++)
{
for(int z = 0; z < size; z++)
{
if(z == thisnum)
p2[(yn*size*size)+(x*size)+z] = true;
}
}
for(int z = 0; z < size; z++)
{
p2[(yn*size*size)+(xn*size)+z] = true;
}
}
public void printPossibilities()
{
for(int y = 0; y < size; y++)
{
for(int x = 0; x < size; x++)
{
System.out.print("Possibilities for (" + x + "," + y + ") are: ");
for(int z = 0; z < size; z++)
{
if(p2[(y*size*size)+(x*size)+z])
{
System.out.print(z+1);
}
}
System.out.println("");
}
}
}
private void makePossible()
{
p2 = new boolean[size*size*size];
//System.out.println("Makeing possibilities:");
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
for(int k = 0; k < size; k++)
{
p2[(i*size*size)+(j*size)+k] = true;
//System.out.print(p2[(i*size*size)+(j*size)+k] + " ");
}
//System.out.print("\t");
}
//System.out.println("");
}
}
private void parsePuzzle(String filename)
{
Scanner s = null;
try
{
s = new Scanner(new FileInputStream(filename));
}
catch (Exception e)
{
System.out.println("Something went terribly wrong!\n" + e);
}
size = Integer.parseInt(s.nextLine().replaceAll("\n",""));
p = new KCage[size*size];
//System.out.println("Puzzle size: " + size + " Cages: " + p.length);
for(int i = 0; s.hasNextLine();i++)
{
String t1 = s.nextLine();
//System.out.println("Praseing: " + t1);
String[] t2 = t1.split(",");
//System.out.println("Parts length: " + t2.length);
if(t2.length > 1)
{
Cell[] allcells = new Cell[Integer.parseInt(t2[2])];
for(int j = 0; j < Integer.parseInt(t2[2]);j++)
{
String[] coords = s.nextLine().split(",");
allcells[j] = new Cell(Integer.parseInt(coords[0]),Integer.parseInt(coords[1]));
}
for(Cell c : allcells)
{
p[c.getY()*size + c.getX()] = new KCage(Integer.parseInt(t2[0]),getOp(t2[1]),allcells);
}
//p[i] = new KCage(Integer.parseInt(t2[0]),getOp(t2[1]),allcells);
//System.out.println("Created " + p[i]);
}
}
}
public KCage getCage(int i)
{
return p[i];
}
public int getNumCages()
{
return p.length;
}
public String toString()
{
String output = "Puzzle{\n\t";
if(p.length == 0)
return output+"}";
output += p[0];
for(int i = 1; i < p.length; i++)
{
output += ",\n\t"+p[i];
}
return output + "\n}";
}
private static Operator getOp(String s)
{
switch(s)
{
case "+":
return Operator.PLUS;
case "-":
return Operator.MINUS;
case "*":
return Operator.MULTIPLY;
case "/":
return Operator.DIVIDE;
case " ":
return Operator.NONE;
default:
return Operator.NULL;
}
}
}
|