Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A Sudoku game board consists of a partially-filled-in 9 times 9 grid of integers

ID: 3860242 • Letter: A

Question

A Sudoku game board consists of a partially-filled-in 9 times 9 grid of integers from 1 -9. The objective of the game is to fill in the board using the integers 1 -9 such that: Every row in the board contains each integer from 1-9 exactly once. Every column in the board contains each integer from 1 -9 exactly once. Every 3 times 3 "subgrid" of the board contains each integer from 1 -9 exactly once. There are 9 such subgrids in the 9 times 9 grid. For example, the picture here shows a solved puzzle. The numbers in black are the ones that were originally filled in: the numbers in red are the ones placed by the player. Write a method public static boolean isValidSolution(int[][] board) that returns whether or not the parameter is a valid Sudoku solution. The method should return false if the array is of the wrong dimensions (i.e., anything besides 9 times 9), or if it contains any integers besides 1 -9. Note that I'm not asking you to generate the puzzle or draw anything on the screen - all the method needs to do is to return whether or not the solution represented by the parameter array is valid.

Explanation / Answer

Given below is the code for isValidSolution() specified in the question. In order to demonstrate the correctness of the method, the main method first creates a valid board and check it and later makes the board invalid and checks again.

If the answer helped, please don't forget to rate it. Thank you.


public class ValidateSudoku {
   public static void main(String[] args) {
       int board[][]={
               {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}
       };
      
       if(isValidSolution(board))
           System.out.println("The sudoku solution is valid");
       else
           System.out.println("The sudoku solution is not valid");
      
      
       System.out.println("Making the board invalid solution , setting board[0][0] = 1");
       board[0][0] = 1;
       if(isValidSolution(board))
           System.out.println("The sudoku solution is valid");
       else
           System.out.println("The sudoku solution is not valid");
   }
  
   public static boolean isValidSolution(int[][] board)
   {
       //check size
       if(board.length != 9) //check if there are 9 rows
           return false;
      
       for(int row = 0; row < 9; row++)
           if(board[row].length != 9) //check each row has 9 columns
               return false;
      
       boolean[] found = new boolean[10]; //an array to indicate which of the numbers were found already
       //check each row has numbers 1 - 9 without repetition
       for(int row = 0; row < 9; row++)
       {
           //reset found array to all false
           for(int i = 1; i <= 9; i++)
               found[i] = false;
          
           for(int col = 0; col < 9; col++)
           {
               int n = board[row][col];
               if(n < 1 || n > 9) //check if its not a number in range 1-9
                   return false;
               if(found[n]) //already the number was found and again its appearing
                   return false;
               else
                   found[n] = true;
           }
              
       }
          
      
       //check each column has numbers 1 - 9 without repetition
       //we don't need to check if the numbers are in range since we have
       //covered that check in the above code while checking each row
       for(int col = 0; col < 9; col++)
       {
           //reset found array to all false
           for(int i = 1; i <= 9; i++)
               found[i] = false;
           for(int row = 0; row < 9; row++)
           {
               int n = board[row][col];
               if(found[n]) //already the number was found and again its appearing
                   return false;
               else
                   found[n] = true;
           }
              
       }
      
      
       //check each subgrid
      
      
       for(int gridRow = 0; gridRow < 9; gridRow += 3) //increment in steps of 3 to get next grid
       {
           for(int gridCol = 0; gridCol < 9; gridCol += 3) //increment in steps fo 3
           {
               //check the subgrid starting at gridRow, gridCol
              
               //reset found array to all false
               for(int i = 1; i <= 9; i++)
                   found[i] = false;
              
               for(int i = 0; i < 3; i++)
                   for(int j = 0; j < 3; j++)
                   {
                       int n = board[gridRow + i][gridCol + j];
                       if(found[n]) //already the number was found and again its appearing
                           return false;
                       else
                           found[n] = true;
                   }
           }
       }
      
       return true; //if all checks passed, then this statement is reached, return true
   }
}

output

The sudoku solution is valid
Making the board invalid solution , setting board[0][0] = 1
The sudoku solution is not valid

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote