The file in sudokutxt at the end of this folder after PPt9 c
Solution
public class Sudoku
{
//instance variables
private int row;
private int col;
private int number;
private int [][] puzzle;
private long steps;
/**
*
* @param sudokuReader the scanner containing the puzzle
* file to be read and scanned into an array
* @throws IOException
*
*/
public Sudoku (File sudokuFile)
{
try
{
Scanner sudokuReader = new Scanner(sudokuFile);
puzzle = new int [9][9];
for (row = 0; row < 8; row++)
{
for (col = 0; col < 8; col++)
{
if(sudokuReader.hasNext())
{
if(sudokuReader.hasNextInt())
{
number = sudokuReader.nextInt();
puzzle[row][col] = number;
}
}
}
}
for(int row = 0; row < puzzle.length; row++)
{
for(int col = 0; col < puzzle.length; col++)
{
System.out.print(puzzle[row][col]);
}
System.out.println();
}
}
catch (IOException e)
{
System.out.println(\"Cannot find file\");
}
}
/**
* Method that checks to see if a number can be put in to
* a specific position on the Sudoku board
* @param row row position
* @param col column position
* @param num number being analyzed
* @return value stating if the move is legal
*/
public boolean isLegal (int row, int col, int num)
{
for(int i = 0; i < 9; i++)//checks values row-wise
if (num == puzzle[i][col])
return false;
for(int j = 0; j < 9; j++)//checks values column-wise
if (num == puzzle[row][j])
return false;
//for(int k = 0; k < 9; k++)
return true; //no violations, so it\'s a legal move
}
/**
* Methods that uses a backtracking algorithm to put
* numbers into positions on the puzzle and tries to
* solve it. If there is a solution, it returns true.
* If there is no solution, it returns false.
* @param row row position to start solving from
* @param col column position to start solving from
* @return value boolean stating if puzzle can be solved
*/
public boolean solve (int row, int col)
{
if(++row == 8)
return true;
if (puzzle[row][col] == \'.\')
return(solve(row++, col++));
for(int num = 1; num <= 9; ++num)
{
if(isLegal(row, col, num))
{
puzzle[row][col] = num;
steps++;
if(solve(row, col++))
return true;
}
}
return false;
}
/**
* Returns a string representing the contents of the
* sudoku.
* @return string representation of the puzzle
*/
public String ToString()
{
return \"Unsolved: \" + puzzle;
}
}



