-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFutoshiki_Problem.java
106 lines (97 loc) · 4.15 KB
/
Futoshiki_Problem.java
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
import consts.FutoshikiConsts;
import consts.FutoshikiEnum;
import consts.FutoshikiHeuristicEnum;
import consts.ProjectConsts;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Futoshiki_Problem extends Grid_Problem<Object, FutoshikiEnum, Integer, FutoshikiHeuristicEnum> {
public final int gridX, gridY;
public static final String neutral = "-", lessThan = "<", moreThan = ">";
public Futoshiki_Problem(FutoshikiEnum chosenProblem) {
super(chosenProblem,
FutoshikiConsts.getSize(chosenProblem)[0] * 2 - 1,
FutoshikiConsts.getSize(chosenProblem)[1] * 2 - 1,
new ArrayList<>(){{
for (int i = 0; i < FutoshikiConsts.getSize(chosenProblem)[0]; i++) {
add(i + 1);
}
}}
);
gridX = FutoshikiConsts.getSize(chosenProblem)[0];
gridY = FutoshikiConsts.getSize(chosenProblem)[1];
readProblem(ProjectConsts.folderPath);
setIndexesToFill();
}
private void readProblem(String folderPath) {
String filename = String.format("%s\\%s_%sx%s", folderPath, "futoshiki", gridX, gridY);
try {
Scanner scanner = new Scanner(new File(filename));
int rowCounter = 0;
while (scanner.hasNextLine()) {
var nextLine = scanner.nextLine().split("");
for (int i = 0; i < nextLine.length; i++) {
var nextChar = nextLine[i];
Object nextValue = nextChar;
try {
nextValue = Integer.valueOf(nextChar);
}
catch (NumberFormatException ignored) {}
if(nextValue.equals("x")) {
nextValue = (Integer) null;
}
problem.add(nextValue);
if(rowCounter % 2 == 1 && i < gridX - 1) {
problem.add("-");
}
}
rowCounter += 1;
}
scanner.close();
} catch (IOException e) {
String errorMessage = String.format("Problem reading file \"%s\"", filename);
System.out.println(errorMessage);
}
}
public String getSimpleDisplay(ArrayList<Object> grid) {
var allToDisplay = "";
for (int i = 0; i < grid.size(); i++) {
if (grid.get(i) == null ) { allToDisplay += ANSI_YELLOW + "x" + ANSI_RESET; }
else if (grid.get(i).equals("-") ) { allToDisplay += " "; }
else { allToDisplay += grid.get(i); }
allToDisplay += " ";
if((i + 1) % x == 0) { allToDisplay += "\n"; }
}
allToDisplay = allToDisplay.substring(0, allToDisplay.length() - 3);
return allToDisplay;
}
public String toDisplay(ArrayList<Object> grid) { return toDisplay(grid, -1); }
@Override
public String toDisplay(ArrayList<Object> grid, int changedItemInx) {
var allToDisplay = "";
for (int i = 0; i < grid.size(); i++) {
if(Futoshiki_Problem.lessThan.equals(grid.get(i)) || Futoshiki_Problem.moreThan.equals(grid.get(i))) {
allToDisplay += ANSI_PURPLE;
}
else if(i == changedItemInx) {
allToDisplay += ANSI_RED;
}
else if(!indexesToFill.contains(i)) {
allToDisplay += ANSI_GREEN;
}
if (grid.get(i) == null ) { allToDisplay += ANSI_YELLOW + "x" + ANSI_RESET; }
else if (grid.get(i).equals("-") ) { allToDisplay += " "; }
else { allToDisplay += grid.get(i); }
if(!indexesToFill.contains(i) || i == changedItemInx) {
allToDisplay += ANSI_RESET;
}
allToDisplay += " ";
if((i + 1) % x == 0) { allToDisplay += "\n"; }
}
allToDisplay = allToDisplay.substring(0, allToDisplay.length() - 3) + "\n";
return allToDisplay;
}
@Override
public Futoshiki_PartialSolution getInitialPartialSolution() { return new Futoshiki_PartialSolution(this); }
}