-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinary_Problem.java
79 lines (71 loc) · 2.72 KB
/
Binary_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
import consts.BinaryConsts;
import consts.BinaryEnum;
import consts.BinaryHeuristicEnum;
import consts.ProjectConsts;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Binary_Problem extends Grid_Problem<Integer, BinaryEnum, Integer, BinaryHeuristicEnum> {
public Binary_Problem(BinaryEnum chosenProblem) {
super(chosenProblem,
BinaryConsts.getSize(chosenProblem)[0],
BinaryConsts.getSize(chosenProblem)[1],
new ArrayList<>(){ { add(0); add(1); } }
);
readProblem(ProjectConsts.folderPath);
setIndexesToFill();
displaySplitter = " | ";
}
private void readProblem(String folderPath) {
String filename = String.format("%s\\%s_%sx%s", folderPath, "binary", x, y);
try {
Scanner scanner = new Scanner(new File(filename));
while (scanner.hasNextLine()) {
var nextLine = scanner.nextLine().split("");
for (int i = 0; i < x; i++) {
Integer selectedVal = switch (nextLine[i]) {
case "x" -> null;
case "1" -> 1;
case "0" -> 0;
default -> throw new IllegalArgumentException("Incorrect input file");
};
problem.add(selectedVal);
}
}
scanner.close();
} catch (IOException e) {
String errorMessage = String.format("Problem reading file \"%s\"", filename);
System.out.println(errorMessage);
}
}
@Override
public String toDisplay(ArrayList<Integer> grid) {
return toDisplay(grid, -1);
}
@Override
public String toDisplay(ArrayList<Integer> grid, int changedItemInx) {
var allToDisplay = displaySplitter;
for (int i = 0; i < grid.size(); i++) {
if(!indexesToFill.contains(i)) {
allToDisplay += this.ANSI_GREEN;
}
if(i == changedItemInx) {
allToDisplay += this.ANSI_RED;
}
if (grid.get(i) == null) { allToDisplay += " "; }
else { allToDisplay += grid.get(i); }
if(!indexesToFill.contains(i) || i == changedItemInx) {
allToDisplay += this.ANSI_RESET;
}
allToDisplay += displaySplitter;
if((i + 1) % x == 0) { allToDisplay += "\n" + displaySplitter; }
}
allToDisplay = allToDisplay.substring(0, allToDisplay.length() - 3);
return allToDisplay;
}
@Override
public Binary_PartialSolution getInitialPartialSolution() {
return new Binary_PartialSolution(this);
}
}