import java.util.Scanner;
public class Sudoku {
public int[][] grid;
Sudoku(){
grid = new int[3][3]; //empty
}
Sudoku(String start){
grid = new int[3][3];
for(int x = 0; x <3; x++){
for(int y =0; y<3; y++){
grid[x][y] = 0;
}
}
grid[0][0] = 1; //top left most
grid[0][1] = 3;
//grid[0][2] = 2;
grid[1][0] = 3;
grid[1][1] = 2;
grid[1][2] = 1;
grid[2][1] = 1;
grid[2][2] = 3;
}
public void printMySudoku(){
for (int i = 0; i < 3; ++i) {
if (i % 1 == 0)
System.out.println(" -----------------------");
for (int j = 0; j < 3; ++j) {
if (j % 1 == 0) System.out.print("| ");
System.out.print(grid[i][j] == 0
? " "
: Integer.toString(grid[i][j]));
System.out.print(' ');
}
System.out.println("|");
}
System.out.println(" -----------------------");
}
public boolean insertVal(int row, int col, int myVal){
System.out.println("Entered insertVal " + "row " + row + " column " + col + " myVal " + myVal);
if(checkRow(row, col, myVal) == false)
return false;
if(checkCol(row, col, myVal) == false)
return false;
if(checkBox(row, col, myVal) == false)
return false;
grid[row][col] = myVal;
return true;
}
public boolean removeVal(int row, int col, int myVal) {
//grid[row][col] = 0;
return true;
}
private boolean checkRow(int row, int col, int myVal) {
for (int a = 0; a < 3; ++a) // row
if (myVal == grid[row][a]){
System.out.println(myVal + " Already in Row: " + row);
return false;
}
return true;
}
private boolean checkCol(int row, int col, int myVal) {
for (int b = 0; b < 3; ++b) // column
if (myVal == grid[b][col]){
System.out.println(myVal + " Already in Column: " + col);
return false;
}
return true;
}
public boolean checkBox(int row, int col, int myVal) {
int boxRowOffset = (row/3)*3;
int boxColOffset = (col/3)*3;
for (int c = 0; c < 3; ++c) // box
for (int d = 0; d < 3; ++d)
if (myVal == grid[boxRowOffset+c][boxColOffset+d]){
System.out.println(myVal + " Already in Box ");
return false;
}
return true;
}
public static void main(String args[]){
Sudoku i = new Sudoku("start");
i.printMySudoku();
@SuppressWarnings("resource")
Scanner guess = new Scanner(System.in);
int row, col, val;
String action;
while(true){
System.out.println("Enter I for insert or R for remove: ");
action = guess.next();
System.out.println("Row: ");
row = guess.nextInt();
System.out.println("Column: ");
col = guess.nextInt();
System.out.println("Value: ");
val = guess.nextInt();
if(action.equals("I")){
if(i.insertVal(row, col, val)){
System.out.println("Good Job ");
}
else System.out.println("Try Again ");
i.printMySudoku();
}
else{ i.removeVal(row, col, val);
i.printMySudoku();}
}
}
}