Question

In: Computer Science

• This lab, you will write a Java program to determine if a given Sudoku puzzle...

• 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.

Solutions

Expert Solution

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);      

  }

}


Related Solutions

Write Prolog code which can solve any given 9x9 Sudoku puzzle. Test your implementation with at...
Write Prolog code which can solve any given 9x9 Sudoku puzzle. Test your implementation with at least 2 querries and show the results in README. Writing README carries 1 point.
Scala: Write and test a program as elegantly as possible You are given a puzzle like...
Scala: Write and test a program as elegantly as possible You are given a puzzle like this: 7 __ 10 __ 2 Each blank may be filled with a ‘+’ (plus) or ‘-’ (minus), producing a value k. For all combinations of plus and minus, find the value of k that is closest to 0. In the above case, there are 4 combinations, each producing a different value: 7 + 10 + 2 = 19 7 + 10 - 2...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves from an initial board...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional array to store how many pounds of food three monkeys eats each day in a week. The 2D array is then passed to functions to find total, average and least amount of food consumption, etc. The program then need to display: the average amount of food the three monkeys ate per day the least amount of food eaten all week by any one monkey....
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT