I have written a program to create a 4x4 grid Sudoku style.
The numbers for the puzzle are read from a file and placed in the puzzle.
As you can see, i have assigned the first numbers 'a' and the second 'b' etc
However, i can not access the a,b,c,d String in the main method. How can i do this?
Puzzle
3 2 1 4
1 4 3 2
2 3 4 1
4 1 2 3
Solver
import java.util.Scanner;
import java.io.*;
import jp.ac.kobe_u.cs.cream.*;
public class FSolver {
private static Scanner x;
public static void openFile(){
try{
x = new Scanner(new File("Puzzle.txt"));
}
catch(Exception e){
System.out.println("no file");
}
}
public static void readFile(){
while(x.hasNext()){
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
System.out.printf("%s %s %s %s\n", a, b, c, d);
}
}
public static void closeFile(){
x.close();
}
public static void main(String[] args){
openFile();
readFile();
closeFile();
Network net = new Network();
int n = 4;
IntVariable[][] v = new IntVariable[n][n];
IntVariable[] vs = new IntVariable[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
v[i][j] = new IntVariable(net, 1, n);
}
}
v[0][1].equals(2);
v[0][3].equals(4);
v[1][2].equals(3);
v[2][1].equals(3);
v[3][0].equals(4);
v[3][3].ge(v[2][3]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
vs[j] = v[i][j];
}
new NotEquals(net, vs);
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
vs[i] = v[i][j];
}
new NotEquals(net, vs);
}
for (int i0 = 0; i0 < n; i0 += 2) {
for (int j0 = 0; j0 < n; j0 += 2) {
int k = 0;
for (int i = i0; i < i0+2; i++) {
for (int j = j0; j < j0+2; j++) {
vs[k++] = v[i][j];
}
}
new NotEquals(net, vs);
}
}
Solver solver = new DefaultSolver(net);
for (solver.start(); solver.waitNext(); solver.resume()) {
Solution solution = solver.getSolution();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(solution.getIntValue(v[i][j]) + " ");
System.out.println();
}
System.out.println();
}
solver.stop();
}
}