package Project_3;
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
public class Project1
{
public static int[][] board = new int[6][6];
public Project1() {
}
public boolean trySolving(int row, int column)
{
int counter = 0;
if (this.board[row-1][column-1] == 0) // is this spot open? (-1 because using 1-6 as numbers buy array is 0-5
{
for (int possibleValues = 1; possibleValues <= 6; possibleValues++) //tries all possible values
{
if ( (!this.doesRowContainValue(possibleValues, row-1, column-1)) && (!this.doesColumnContainValue(possibleValues, row-1, column-1)) && (!this.doesSquareContainValue(possibleValues, row, column)) ) //makes sure the number is not in the row, column, or box
{
this.board[row - 1][column - 1] = possibleValues; //add the value to the square
//if at end of row, and not on the last row, go to beginning of next row
if ( (column == 6) && (row < 6)) {
row++;
column = 0;
}
if (column < 6)
return trySolving(row, column + 1); //moves right 1 square if not at the end of the row
else if (row < 6)
return trySolving(row + 1, column); //moves down 1 square if not at the end of the coloumn
else
return true; //reached the end of the puzzle
}
else // value did not work
counter++; //keeps track of how many numbers fail
}//end of for loop
if (counter == 6) // none of the 6 values worked in this square
return false;
}
//-----------------------------------------Already a value on this spot!------------------------------------
//if at end of row, and not on the last row, go to beginning of next row
if ( (column == 6) && (row < 6))
{
row++;
column = 0;
}
if (column < 6) //spot was not open, so moving to the next spot
return trySolving(row, column + 1); //moves right 1 square if not at the end of the row
else if (row < 6)
return trySolving(row + 1, column); //moves down 1 square if not at the end of the coloumn
else
return true; //reached the end of the puzzle
//---------------------------------Already a value on this spot!--------------------------------------------
}
/*
* doesRowContainValue
* pre - Existing sudoku board
* post - determines if current row contains the current value
* @return - Returns true if current row contains the current value
*/
private boolean doesRowContainValue(int value, int row, int column)
{
for (int columnindex = 0; columnindex <= 5; columnindex++)
{
if ((this.board[row][columnindex] == value) && (column != columnindex))
{
return true;
}
}
return false;
}
/*
* doesColumnContainValue
* pre - Existing sudoku board
* post - determines if current column contains the current value
* @return - Returns true if current column contains the current value
*/
private boolean doesColumnContainValue(int value, int row, int column)
{
for (int rowindex = 0; rowindex <= 5; rowindex++)
{
if ((this.board[rowindex][column] == value) && (row != rowindex) )
{
return true;
}
}
return false;
}
/*
* doesSquareContainValue
* pre - Existing sudoku board
* post - determines if current square contains the current value
* @return - Returns true if current square contains the current value
*/
private boolean doesSquareContainValue(int value, int row, int column)
{
//identify square
int rowStart = ( (row - 1) / 2) + 1;
int columnStart = ( (column - 1) / 3) + 1;
int rowIndexEnd = rowStart * 2;
if (rowIndexEnd == 0)
rowIndexEnd = 3;
int rowIndexStart = rowIndexEnd - 1;
int columnIndexEnd = columnStart * 3;
if (columnIndexEnd == 0)
columnIndexEnd = 3;
int columnIndexStart = columnIndexEnd - 2;
for (int rowIndex = rowIndexStart; rowIndex <= rowIndexEnd; rowIndex++)
{
for (int columnIndex = columnIndexStart+1; columnIndex <= columnIndexEnd; columnIndex++)
{
if ( (this.board[rowIndex][columnIndex] == value) )
{
return true;
}
}
}
return false;
}
public void main(String[] args)
{
try {
System.out.println("Please enter the name of the sudoku file: ");
Scanner sudokuName = new Scanner(System.in);
String name = sudokuName.nextLine();
FileReader reader = new FileReader(name);
Scanner in = new Scanner(reader);
for (int i = 0; i < 6 ; i++)
{
for (int j = 0; j < 6; j++)
{
board[i][j] = in.nextInt();
}
}
if (trySolving(1, 1))
{
System.out.println("Puzzle was successfully solved");
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
System.out.print(board[i][j] + " ");
if (j == 5)
System.out.println();
}
}
}
}
catch (IOException exception)
{
System.out.println("Error processing file: " + exception);
}
}
}