Skip to content
Snippets Groups Projects
Commit af92bbf6 authored by Nimrod Abramovich's avatar Nimrod Abramovich
Browse files

final steps

parent 4d1369f1
Branches master
No related tags found
No related merge requests found
11+ 1,7
2÷ 2,3
20x 4,10
6x 5,6,12,18
3- 8,9
3÷ 11,17
240x 13,14,19,20
6x 15,16
6x 21,27
7+ 22,28,29
30x 23,24
6x 25,26
9+ 30,36
8+ 31,32,33
2÷ 34,35
\ No newline at end of file
File added
......@@ -142,12 +142,12 @@ public class ConstraintsHandler {
}
}
public void checkWinCondition() {
public boolean checkWinCondition() {
updateData();
if (checkCellsFull() && checkConstraintErrors()) {
Animation animation = new Animation();
animation.go();
return true;
}
return false;
}
private boolean checkCellsFull() {
......
This diff is collapsed.
......@@ -411,14 +411,4 @@ public class RandomGenerator {
grid = temp;
}
// private void output() {
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// System.out.print(grid[i][j] + " ");
// }
// System.out.print("\n");
// }
// System.out.print("\n");
// }
}
\ No newline at end of file
package coursework;
import java.util.List;
public class Solver {
private int N;
private int[][] gridValues; // -1 = not possible | 1 = possible | 2 = definite
private List<Cage> gridCages;
public Solver(int N, List<Cage> gridCages) {
this.N = N;
this.gridCages = gridCages;
gridValues = new int[N*N][N];
}
public void solvePuzzel() {
setDefaultValues();
output();
checkCageValues();
}
private void setDefaultValues() {
for (int i = 0; i < N*N; i++) {
for (int j = 0; j < N; j++) {
gridValues[i][j] = 1; // sets all values to possible
}
}
}
private void checkCageValues() {
for (Cage cage : gridCages) {
int cageValue = cage.getCageValue();
int length = cage.getCageIds().length;
if (length == 1) {
gridValues[cage.getCageIds()[0]][cageValue - 1] = 2;
removePossibleValue(cage.getCageIds()[0], cageValue);
}
else {
String operator = cage.getCageOperator();
int counter = 1;
switch (operator) {
case "+":
int[] possibleValues = new int[length];
for (int i = 0; i < length; i++) {
possibleValues[i] = 1;
}
while (valuesMultiplied(possibleValues) < Math.pow(N, length)) {
if (sumOfValues(possibleValues, cageValue)) {
for (int possibleVal : possibleValues) {
for (int id : cage.getCageIds()) {
gridValues[id][possibleVal - 1] = 1;
}
}
}
for (int j = 1; j <= counter; j++) {
if (possibleValues[possibleValues.length - counter] == N) {
possibleValues[possibleValues.length - counter] = 0;
possibleValues[possibleValues.length - 2] += 1;
counter += 1;
}
}
}
break;
case "-":
break;
case "x":
break;
case "":
break;
}
}
}
}
private boolean sumOfValues(int[] values, int cageValue) {
int sum = 0;
for (int value : values) {
sum += value;
}
return sum == cageValue;
}
private int valuesMultiplied(int[] values) {
int result = 1;
for (int value : values) {
result *= value;
}
return result;
}
private void removePossibleValue(int id, int cageValue) {
int row = (int) id / N;
int col = id % N;
for (int i = 0; i < N; i++) {
if (i != col)
gridValues[row*N + i][cageValue - 1] = -1;
}
for (int i = 0; i < N; i++) {
if (i != row)
gridValues[i*N + col][cageValue - 1] = -1;
}
for (int i = 0; i < N; i++) {
if (i != cageValue - 1)
gridValues[id][i] = -1;
}
}
private void output() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print("[");
for (int k = 0; k < N; k++) {
System.out.print(gridValues[i*N + j][k]);
System.out.print(",");
}
System.out.print("], ");
}
System.out.print("\n");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment