-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSudokuIO.java
98 lines (93 loc) · 2.79 KB
/
SudokuIO.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
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
public class SudokuIO {
static final int NUM_VARS = 81;
static int vars[] = new int[NUM_VARS];
static boolean varDomain[][] = new boolean[NUM_VARS][9];
//Takes input from a file and creates a Sudoku CSP from that information
public static void makeSudokuPuzzle(String[] args){
try{
File inFile = null;
if (0 < args.length) {
inFile = new File(args[0]);
} else {
System.err.println("Invalid arguments count:" + args.length);
System.exit(0);
}
FileReader file = new FileReader(inFile);
for (int a = 0; a < NUM_VARS; a++){
char ch;
do{
ch = (char)file.read();
}while ((ch == '\n') || (ch == '\r') || (ch == ' '));
if (ch == '-'){
vars[a] = 0;
for (int j = 0; j < 9; j++){
varDomain[a][j] = true;
}
}
else{
String s = "" + ch;
Integer i = new Integer(s);
vars[a] = i.intValue();
for (int j = 0; j < 9; j++){
if (j == i.intValue() - 1)
varDomain[a][j] = true;
else
varDomain[a][j] = false;
}
}
}
Sudoku_AC3.runAC3(varDomain);
int count=0;
for (int i=0; i<81;i++){
count+=Sudoku_NakedVariable.numberOfDomainValues(varDomain,i);
}
System.out.println(count);
Sudoku_NakedVariable.runNakedVariable(varDomain);
count=0;
for (int i=0; i<81;i++){
count+=Sudoku_NakedVariable.numberOfDomainValues(varDomain,i);
}
System.out.println(count);
file.close();
}
catch(IOException e){System.out.println("File read error: " + e);}
}
//Outputs the Sudoku board to the console and a file
public static void printSudoku(){
try{
FileWriter ofile = new FileWriter("output.txt");
for (int a = 0; a < 9; a++){
for (int b = 0; b < 9; b++){
int c = 9*a + b;
if (vars[c] == 0){
System.out.print("- ");
ofile.write("- ");
}
else{
System.out.print(vars[c] + " ");
ofile.write(vars[c] + " ");
}
}
System.out.println("");
ofile.write("\r\n");
}
ofile.write("\r\n");
for (int a = 0; a < 81; a++){
for (int b = 0; b < 9; b++){
ofile.write(varDomain[a][b]+" ");
}
ofile.write("\r\n");
}
ofile.close();
}
catch(IOException e){System.out.println("File read error: " + e);}
}
public static void main(String[] args) {
makeSudokuPuzzle(args);
printSudoku();
}
}