In: Computer Science
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not.
• You determine if the puzzle is complete and valid, incomplete, or is invalid.
• A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank.
• If you don’t know how a Sudoku Puzzle works, do some research, or download an app.
• You will be given hard-coded sudoku puzzle, as a 2-dim array. You can also make your own. Place the array into your code.
• Your task will be to write java code to determine if the puzzle is a Valid, Invalid, or Incomplete.
• A Sudoku Puzzle is:
- Valid: If all 81 elements make sense.
- Invalid: If any row, column, or section is invalid.
- Incomplete: If the puzzle is not totally filled in, but what is filled in, is valid.
Requirements
1. A comment header is required.
2. Name the java class Lab_ValidateSudokuPuzzle. Create only 1 java file, contains the main() and any other methods.
3. Input: The puzzle solution will be a constant 2-dimensional array in your code.
a. You can change the puzzle values, to test your software.
b. Use a “zero” in an element to indicate this puzzle has blank elements.
4. First, print the array (in a 9 x 9 format). This makes the puzzle easy to follow, for the user.
5. Next, validate all of the rows. Display a message if any row is invalid or incomplete.
a. If a row is incomplete or invalid, display an error message.
b. Create a method called boolean validateRows(int puzzle[][]).
c. The Boolean return value is TRUE if at least 1 row was invalid.
6. Next, validate all of the columns,
a. If a row is incomplete or invalid, display an error message.
b. Create a method called boolean validateCols(int puzzle[][]).
7. Next, validate the 9 sections. Hint: calculate the row and column offset.
a. Create a method called boolean validateSections(int puzzle[][]).
8. If you find any rows/cols/sections invalid or incomplete . . .
a. Display the improper row/co/section with a message stating what is wrong.
9. Validate all rows, columns, and sections. If error was found, display the message and keep going.
10. Make sure the output is easy to understand.
11. At the end, state if the entire puzzle is incomplete, invalid, or valid.
import java.util.*;
class HelloWorld {
// Function to check if a given row is valid. It will return:
// -1 if the row contains an invalid value
// 0 if thr row contains repeated values
// 1 is the row is valid.
public static int valid_row(int row, int [][] grid){
int temp[] = grid[row];
Set<Integer>set = new HashSet<Integer>();
for (int value : temp) {
// Checking for values outside 0 and 9;
// 0 is considered valid because it
// denotes an empty cell.
// Removing zeros and the checking for values and
// outside 1 and 9 is another way of doing
// the same thing.
if (value < 0 || value > 9){
System.out.println( "Invalid value" );
return -1;
}
//Checking for repeated values.
else if (value != 0){
if (set.add(value) == false) {
return 0;
}
}
}
return 1;
}
// Function to check if a given column is valid. It will return:
// -1 if the column contains an invalid value
// 0 if the column contains repeated values
// 1 is the column is valid.
public static int valid_col(int col, int [][] grid){
Set<Integer>set = new HashSet<Integer>();
for (int i =0 ; i< 9; i++) {
// Checking for values outside 0 and 9;
// 0 is considered valid because it
// denotes an empty cell.
// Removing zeros and the checking for values and
// outside 1 and 9 is another way of doing
// the same thing.
if (grid[i][col] < 0 || grid[i][col] > 9){
System.out.println( "Invalid value" );
return -1;
}
// Checking for repeated values.
else if (grid[i][col] != 0){
if (set.add(grid[i][col]) == false) {
return 0;
}
}
}
return 1;
}
// Function to check if all the subsquares are valid. It will return:
// -1 if a subsquare contains an invalid value
// 0 if a subsquare contains repeated values
// 1 if the subsquares are valid.
public static int valid_subsquares(int [][] grid){
for (int row = 0 ; row < 9; row = row + 3) {
for (int col = 0; col < 9; col = col + 3) {
Set<Integer>set = new HashSet<Integer>();
for(int r = row; r < row+3; r++) {
for(int c= col; c < col+3; c++){
// Checking for values outside 0 and 9;
// 0 is considered valid because it
// denotes an empty cell.
// Removing zeros and the checking for values and
// outside 1 and 9 is another way of doing
// the same thing.
if (grid[r][c] < 0 || grid[r][c] > 9){
System.out.println( "Invalid value" );
return -1;
}
// Checking for repeated values.
else if (grid[r][c] != 0){
if (set.add(grid[r][c]) == false) {
return 0;
}
}
}
}
}
}
return 1;
}
//Function to check if the board invalid.
public static void valid_board(int [][] grid){
// Checking the rows and columns.
for (int i =0 ; i< 9; i++) {
int res1 = valid_row(i, grid);
int res2 = valid_col(i, grid);
// if a row or column is invalid, then the board is invalid.
if (res1 < 1 || res2 < 1) {
System.out.println( "The board is invalid." );
return;
}
}
int res3 = valid_subsquares(grid);
// if any one the subsquares is invalid, then the board is invalid.
if (res3 < 1) {
System.out.println( "The board is invalid." );
}
else {
System.out.println( "The board is valid." );
}
}
// Function to print the board.
public static void print_board(int [][] grid){
for (int[] row : grid) {
System.out.print("[");
for (int y : row) {
System.out.print(y + ", ");
}
System.out.println("]");
}
}
public static void main( String args[] ) {
// A valid board.
int [] [] board = {
{1, 4, 7, 0, 0, 0, 0, 0, 3},
{2, 5, 0, 0, 0, 1, 0, 0, 0},
{3, 0, 9, 0, 0, 0, 0, 0, 0},
{0, 8, 0, 0, 2, 0, 0, 0, 4},
{0, 0, 0, 4, 1, 0, 0, 2, 0},
{9, 0, 0, 0, 0, 0, 6, 0, 0},
{0, 0, 3, 0, 0, 0, 0, 0, 9},
{4, 0, 0, 0, 0, 2, 0, 0, 0},
{0, 0, 1, 0, 0, 8, 0, 0, 7},
};
print_board(board);
valid_board(board);
// An invalid board. The first row contains
// repeated values.
int [] [] board2 = {
{1, 4, 4, 0, 0, 0, 0, 0, 3},
{2, 5, 0, 0, 0, 1, 0, 0, 0},
{3, 0, 9, 0, 0, 0, 0, 0, 0},
{0, 8, 0, 0, 2, 0, 0, 0, 4},
{0, 0, 0, 4, 1, 0, 0, 2, 0},
{9, 0, 0, 0, 0, 0, 6, 0, 0},
{0, 0, 3, 0, 0, 0, 0, 0, 9},
{4, 0, 0, 0, 0, 2, 0, 0, 0},
{0, 0, 1, 0, 0, 8, 0, 0, 7},
};
print_board(board2);
valid_board(board2);
}
}