-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
88 lines (79 loc) · 2.23 KB
/
Sudoku.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
import java.util.Arrays;
public class Sudoku {
private int[][] sudoku;
public Sudoku(int[][] sudoku){
this.sudoku = sudoku;
}
public int[] Column(int col){
int[] c = {0,0,0,0,0,0,0,0,0};
for(int i = 0; i<9;i++){
c[i] = sudoku[i][col];
}
return c;
}
public int[] Row(int row){
return sudoku[row];
}
public int[] Square(int square){
int[] squareBox = {0,0,0,0,0,0,0,0,0};
int count = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
squareBox[count] = sudoku[i + 3*(countMultiples(square, 3))][j + 3*(square%3)];
count++;
}
}
return squareBox;
}
public int countMultiples(int base, int subtractor){
int count = 0;
while(base>2){
base-=subtractor;
count++;
}
return count;
}
public boolean checkForInColumn(int num, int col){
for(int cell : Column(col)){
if(cell == num){
return true;
}
}
return false;
}
public boolean checkForInRow(int num, int row){
for(int cell : Row(row)){
if(cell == num){
return true;
}
}
return false;
}
public boolean checkForInSquare(int num, int square){
for(int cell : Square(square)){
if(cell == num){
return true;
}
}
return false;
}
public int findSquare(int row, int col){
return (((row/3)*3)+(col/3 + 1)-1);
}
public int[] cellCandidates(int row, int col){
int[] candList = {1,2,3,4,5,6,7,8,9};
int count = 0;
for(int num = 1; num<10;num++){
if(checkForInColumn(num, col) || checkForInRow(num, row) || checkForInSquare(num, findSquare(row, col))){
candList[count]=0;
}
/*if(checkForInSquare(num, findSquare(row, col))){
System.out.println(num+": "+row+", "+col);
System.out.println((findSquare(row, col)));
}*/
count++;
}
Arrays.sort(candList);
return (candList);
}
}