-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.java
73 lines (63 loc) · 2.47 KB
/
Program.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
import java.util.Scanner;
import java.io.File;
public class Program {
public static void main(String[] args) throws Exception {
//readCSVMatrices(); //check bottom to see what this is for please
Scanner in = new Scanner(System.in);
int x = 9;
int y = 9;
int[][] matrix = new int[x][y];
String line;
for(int i = 0 ; i < x ; ++i) {
line = in.nextLine();
String[] nums = line.split(" ");
for(int j = 0 ; j < y ; ++j) {
matrix[i][j] = Integer.parseInt(nums[j]);
}
}
in.close();
SudokuSolver solve = new SudokuSolver(0,0, matrix); //0,0 just acts as the starting place to solve from
solve.solveMatrix(0, 0);
solve.printMatrix();
}
public static void printMatrix(int x, int y, int[][] matrix) { //simple print function - can be improved for readability with spaces
for(int i = 0; i < x ; ++i) {
for(int j = 0 ; j < y ; ++j) {
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
public static void readCSVMatrices() throws Exception {
int num = 10;
Scanner in = new Scanner(new File("sudoku.txt")); // this is causing an error due to project root. It isnt really that
//important now but it will be for checking against a couple solvable sudokus. If someone wants to sort it so that we can use
//the million sudokus as test cases so when we submit we know the algorithm definitively works this can be done later
//basically emulates a marker
SudokuSolver solve;
in.nextLine();
for(int i = 1 ; i < num ; ++i) {
String line = in.nextLine();
String[] components = line.split(","); //csv structure of original text = question,answer
int[][] question = generateMatrix(components[0]); //get question component
int[][] answer = generateMatrix(components[1]); // get answer (what we know is the valid answer by def
solve = new SudokuSolver(0,0,question); //use our alg to solve
question = solve.matrix; // get the matrix (unnecessary but chose to do like this)
if(question != answer) { //if they aren't the same then clearly we failed
System.out.println("failed at : " + num);
}
}
in.close();
}
public static int[][] generateMatrix(String comp){ //just used to parse the text file string into a valid matrix
int[][] matrix = new int[9][9];
int count = 0;
for(int i = 0 ; i < 9 ; ++i) {
for(int j = 0 ; j < 9 ; ++j) {
matrix[i][j] = Integer.parseInt(comp.charAt(count)+"");
++j;
}
}
return matrix;
}
}