You are on page 1of 8

public class Sudoku

{
Element S[][] = new Element[9][9];
boolean solved;
//Inner class Element
private class Element
{
int value; //value of the cell
int possible[] = new int[9]; //possible values for the cell
boolean set; //user set or finalized
int numel; // number of possible values
public Element()
{
//System.out.print("Hi");
value = 0;
possible = new int[]{1,2,3,4,5,6,7,8,9};
set = false;
numel = 9;
}
//Print element
public void print()
{
System.out.print(" ");
for(int i=0; i<9; i++)
{
if(possible[i] == 0)
System.out.print(" ");
else
System.out.print(possible[i]);
}
System.out.print(" |");
}
}
//Sudoku constructor
public Sudoku()
{
for(int i =0; i<9; i++)
for(int j =0; j<9; j++)
{
S[i][j] = new Element();
}
solved = false;
}
//clear a cell for an element
public void clearcell(int i, int j, int k)
{
int from_row, to_row, from_col, to_col, temp1, temp2;
from_row = i - i%3;
to_row = (i/3 + 1)*3;
from_col = j - j%3;
to_col = (j/3 + 1)*3;

for(temp1 = from_row; temp1 < to_row; temp1++)


for(temp2 = from_col; temp2 < to_col; temp2++)
{
if(S[temp1][temp2].set == false)
if(S[temp1][temp2].possible[k-1] != 0)
{
S[temp1][temp2].possible[k-1] = 0;
S[temp1][temp2].numel = S[temp1][temp2].numel - 1;
}
}
}
//clear a row of an element
public void clearrow(int i, int k)
{
//loop for all element in row
for(int x = 0; x<9; x++)
{
if(S[i][x].set == false)
if(S[i][x].possible[k-1] != 0)
{
S[i][x].possible[k-1] = 0;
S[i][x].numel = S[i][x].numel - 1;
}
}
}
//clear a column of an element
public void clearcol(int j, int k)
{
//loop for all element in row
for(int x = 0; x<9; x++)
{
if(S[x][j].set == false)
if(S[x][j].possible[k-1] != 0)
{
S[x][j].possible[k-1] = 0;
S[x][j].numel = S[x][j].numel - 1;
}
}
}
//Set elements in Sudoku
//row and col are as user sees, so sudoku starts from column 1 to 9
//i and j are as java sees, start at 0 and run to 8
public void Set(int row, int col, int k)
{
int i,j;
//Set cell(i,j) = k
i = row-1; j = col-1;
S[i][j].value = k;
S[i][j].set = true;
S[i][j].numel = 1;
//set every element other than the user input element to 0
for(int x = 0; x<9; x++)
{
if(S[i][j].possible[x] != k)

S[i][j].possible[x] = 0;
}
clearcol(j,k);
clearrow(i,k);
clearcell(i, j, k);
}
public void lone_possible()
{
int i, j, x;
for(i = 0;i < 9;i++)
for(j = 0;j < 9;j++)
{
if((S[i][j].numel == 1) && (S[i][j].set == false)) //lone possible eleme
nt
{
for(x = 0; x<9; x++)
{
if(S[i][j].possible[x] == (x+1))
{
System.out.println("Lone possibility " + " " + (
i+1) + " " + (j+1) + " " + (x+1));
Set(i+1, j+1, (x+1));
}
}
}
}
}//Lone possible must end with this
//Clear lone elements in rows
public void clearlone_rows()
{
int sum[] = new int[]{0,0,0,0,0,0,0,0,0};
int i, j, x, y;
//iterate over rows
for(i=0; i<9; i++)
{
sum[0] = 0;
sum[1] = 0;
sum[2] = 0;
sum[3] = 0;
sum[4] = 0;
sum[5] = 0;
sum[6] = 0;
sum[7] = 0;
sum[8] = 0;
//iterate over column
for(j=0; j<9; j++)
{
//update array values
for(x=0; x<9; x++)
sum[x] += S[i][j].possible[x];
}
//check if any value in sum is a lone occurrence
for(x=0; x<9; x++)

{
//System.out.print(x + " " + sum[x] + "; ");
if(sum[x] == x+1) //if sum[0] = 1, means there was a lone occurr
ence of 1
{
//we need to find the column of the lone occurrence
for(y=0; y<9; y++)
{
if(S[i][y].possible[x] == x+1)
if(S[i][y].set == false)
{
System.out.println("Lone occurrence in " + i + "
" + y + " value" + (x+1));
Set(i+1, y+1, x+1);
}
}
}
}
}
}
//Clear lone elements in cell
public void clearlone_cell()
{
int sum[] = new int[]{0,0,0,0,0,0,0,0,0};
int cell_i, cell_j;
int i, j, x, y;
int t_i, t_j;
for(cell_i = 0; cell_i<3; cell_i++)
for(cell_j = 0; cell_j<3; cell_j++)
{
sum[0] = 0;
sum[1] = 0;
sum[2] = 0;
sum[3] = 0;
sum[4] = 0;
sum[5] = 0;
sum[6] = 0;
sum[7] = 0;
sum[8] = 0;
//Iterate over rows
for(i = cell_i*3; i < (cell_i*3+3); i++)
{
//Iterate over column
for(j = cell_j*3; j < (cell_j*3+3); j++)
{
//update array values
for(x=0; x<9; x++)
sum[x] += S[i][j].possible[x];
}
}
//Check if any value in sum is a lone occurrence
for(x=0; x<9; x++)
{
//System.out.print(x + " " + sum[x] + "; ");
if(sum[x] == x+1) //if sum[0] = 1, means there was a lone occurr
ence of 1
{

//We need to find the row and column of the lone occurre
nce
for(t_i = cell_i*3; t_i< (cell_i*3+3); t_i++)
for(t_j = cell_j*3; t_j< (cell_j*3+3); t_j++)
{
if(S[t_i][t_j].possible[x] == x+1)
if(S[t_i][t_j].set == false)
{
System.out.println("Lone occurrence in " + t_i +
" " + t_j + " value" + (x+1));
Set(t_i+1, t_j+1, x+1);
}
}
}
}
}
}
//Clear lone elements in col
public void clearlone_col()
{
int sum[] = new int[]{0,0,0,0,0,0,0,0,0};
int i, j, x, y;
//iterate over rows
for(j=0; j<9; j++)
{
sum[0] = 0;
sum[1] = 0;
sum[2] = 0;
sum[3] = 0;
sum[4] = 0;
sum[5] = 0;
sum[6] = 0;
sum[7] = 0;
sum[8] = 0;
//iterate over rows
for(i=0; i<9; i++)
{
//update array values
for(x=0; x<9; x++)
sum[x] += S[i][j].possible[x];
}
//check if any value in sum is a lone occurrence
for(x=0; x<9; x++)
{
//System.out.print(x + " " + sum[x] + "; ");
if(sum[x] == x+1) //if sum[0] = 1, means there was a lone occurr
ence of 1
{
//We need to find the column of the lone occurrence
for(y=0; y<9; y++)
if(S[y][j].possible[x] == x+1)
if(S[y][j].set == false)
{
System.out.println("Lone occurrence in " + y + "
" + j + " value" + (x+1));
Set(y+1, j+1, x+1);

}
}
}
}
}
//print Sudoku
public void print()
{
for(int i =0; i<9; i++)
{
for(int j =0; j<9; j++)
{
S[i][j].print();
if((j+1)%3 == 0)
System.out.print("|");
}
System.out.println();
if((i+1)%3==0)
{
for(int k =0; k<111; k++)
System.out.print("-");
System.out.println();
}
}
}
//print Sudoku numel
public void print_numel()
{
for(int i =0; i<9; i++)
{
for(int j =0; j<9; j++)
{
System.out.print(" " + S[i][j].numel);
if((j+1)%3 == 0)
System.out.print("|");
}
System.out.println();
if((i+1)%3==0)
{
for(int k =0; k<111; k++)
System.out.print("-");
System.out.println();
}
}
}
//return Sudoku numel
public void numel()
{
int sum = 0;
for(int i =0; i<9; i++)
for(int j =0; j<9; j++)
sum += S[i][j].numel;

}
public static void main(String args[])
{
Sudoku sudoku = new Sudoku();
int row, col, value;
String filename1 = args[0];
In in1 = new In(filename1);
//System.out.println(" numel ");
//sudoku.print_numel();
while
{
row =
col =
value

(!in1.isEmpty())
in1.readInt();
in1.readInt();
= in1.readInt();

sudoku.Set(row, col, value);


}
//System.out.println(" numel ");
//sudoku.print_numel();
sudoku.print();
System.out.println();
System.out.println();
System.out.println("Iteration 1");
sudoku.clearlone_rows();
sudoku.clearlone_col();
sudoku.clearlone_cell();
sudoku.lone_possible();
sudoku.print();
//System.out.println(" numel ");
//sudoku.print_numel();
System.out.println();
System.out.println();
System.out.println("Iteration 2");
sudoku.clearlone_rows();
sudoku.clearlone_col();
sudoku.clearlone_cell();
sudoku.lone_possible();
sudoku.print();
//System.out.println(" numel ");
//sudoku.print_numel();
System.out.println();
System.out.println();
System.out.println("Iteration 3");
sudoku.clearlone_rows();
sudoku.clearlone_col();
sudoku.clearlone_cell();
sudoku.lone_possible();
sudoku.print();
System.out.println();
System.out.println();

System.out.println("Iteration 4");
sudoku.clearlone_rows();
sudoku.clearlone_col();
sudoku.clearlone_cell();
sudoku.lone_possible();
sudoku.print();
System.out.println();
System.out.println();
System.out.println("Iteration 5");
sudoku.clearlone_rows();
sudoku.clearlone_col();
sudoku.clearlone_cell();
sudoku.lone_possible();
sudoku.print();
System.out.println();
System.out.println();
System.out.println("Iteration 6");
sudoku.clearlone_rows();
sudoku.clearlone_col();
sudoku.clearlone_cell();
sudoku.lone_possible();
sudoku.print();
System.out.println();
System.out.println();
}
}//Sudoku ends here

You might also like