Question
Complete the Sudoku Solution Verifier (see slides 92-100 of the Arrays and ArrayLists lecture notes): In particular, complete the 3 missing methods: checkValues ( );
Complete the "Sudoku Solution Verifier" (see slides 92-100 of the "Arrays and ArrayLists" lecture notes):
In particular, complete the 3 missing methods:
checkValues ( );
checkColumn ( );
checkSquare ( );
here is my code for the sudoku solution verifier and i need three more method to complete this assignment
i need the three method from above to finish this assignment
public class VerifySudokuSolution
{
public static void main(String[] args)
{
// Sudoku example from Wikipedia
int[][] arr = {
{5, 3, 4, 6, 7, 8, 9, 1, 2},
{6, 7, 2, 1, 9, 5, 3, 4, 8},
{1, 9, 8, 3, 4, 2, 5, 6, 7},
{8, 5, 9, 7, 6, 1, 4, 2, 3},
{4, 2, 6, 8, 5, 3, 7, 9, 1},
{7, 1, 3, 9, 2, 4, 8, 5, 6},
{9, 6, 1, 5, 3, 7, 2, 8, 4},
{2, 8, 7, 4, 1, 9, 6, 3, 5},
{3, 4, 5, 2, 8, 6, 1, 7, 9}
};
// Solution should have 3 x 3 subsquares containing no duplicates
System.out.println(checkSudokuSolution(arr, 3));
// Swap a couple of values to mess something up, check it again
int temp = arr[2][3];
arr[2][3] = arr[5][6];
arr[5][6] = temp;
System.out.println(checkSudokuSolution(arr, 3));
}
/*
* Check whether the given 2D array is a valid Sudoku solution.
*
* grid: the 2D array to check
* subSquareSize: size of the subsquares in the solution
* return true if the given array is a valid solution, false otherwise
*/
public static boolean checkSudokuSolution(int[][] grid, int subSquareSize)
{
final int size = grid.length;
// First make sure all the values are in the right range.
if (!checkValues(grid, 1, size))
{
return false;
}
// Check that the rows contain no duplicate values
for (int row = 0; row < size; ++row)
{
if (!checkRow(grid, row))
{
return false;
}
}
// Check that the columns contain no duplicate values
for (int col = 0; col < size; ++col)
{
if (!checkColumn(grid, col))
{
return false;
}
}
// Check that the subsquares contain no duplicate values
for (int baseRow = 0; baseRow < size; baseRow += subSquareSize)
{
for (int baseCol = 0; baseCol < size; baseCol += subSquareSize)
{
if (!checkSquare(grid, baseRow, baseCol, subSquareSize))
{
return false;
}
}
}
// If we made it to this point, everything is correct!
return true;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started