Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Sudoku puzzle is a grid of 9x9 squares or cells, that has been subdivided into nine sub-grids or regions of 3x3. The objective of

A Sudoku puzzle is a grid of 9x9 squares or cells, that has been subdivided into nine sub-grids or regions of 3x3. The objective of a game is to enter a digit from 1 through 9 in each cell, in such a way that: Row rule: each horizontal row contains each digit exactly once Column rule: each vertical column contains each digit exactly once Grid rule: each sub-grid or region contains each digit exactly once In a Sudoku puzzle, several digits have already been entered; these may not be changed. The players job is to solve the empty grids with digits respecting the three rules. 2. Completing the Sudoku class You can find an initial version of the Sudoku class in Sudoku.java. As always, download this file and create a project in Eclipse. The Sudoku.java file is incomplete and you need to complete it. The Sudoku class has the following constants pertaining to the game itself: public static final int[][] BOARD = { // a solution to the Sudoku game, we get {4, 3, 5, 8, 7, 6, 1, 2, 9}, // new puzzle by hiding some cells of {8, 7, 6, 2, 1, 9, 3, 4, 5}, // this solution board {2, 1, 9, 4, 3, 5, 7, 8, 6}, {5, 2, 3, 6, 4, 7, 8, 9, 1}, {9, 8, 1, 5, 2, 3, 4, 6, 7}, {6, 4, 7, 9, 8, 1, 2, 5, 3}, {7, 5, 4, 1, 6, 8, 9, 3, 2}, {3, 9, 2, 7, 5, 4, 6, 1, 8}, {1, 6, 8, 3, 9, 2, 5, 7, 4}}; public static final int GRID_SIZE = 9; // the size of Sudoku game, it is 9X9 public static final int SUBGRID_SIZE = 3; // the size of the subgrid region // the percentage of cells that we hide when creating new puzzle public static final double EMPTYCELL_PERCENTAGE = 0.6; //a string used to check if one row/column/subgrid contains all the numbers 1-9 public static final String VALUE = "123456789"; The Sudoku class also contains an instance variable, which is a 2D array: private int[][] puzzle; // the puzzle for the game, some cells are empty In the above puzzle array, we use 0s to represent empty cells. The code that you add to the Sudoku needs to do the following steps in the following order (where indicated by the comments in the code itself): 1. Complete the private int[][] copyOfBoard(int[][] originalBoard) method. In this method, you need to declare a 2D array with the same length of the parameter array originalBoard. Then your code must copy values in the parameter array originalBoard to the array you just created and then return it. 2. Complete the private int[] getTwoRanNum(int min, int range) method. In this method, you should generate two random numbers in the range min ~(min + range) and put them into a single dimensional array. Then your code must return this array. For example, if the passed value of min is 5, the passed value of range is 10, then your code need to generate two random numbers in the range 5 ~ 14. 3. Complete the public boolean checkWin(int[][] board) method. In order to check if a puzzle is solved or not, we need to check if the current puzzle satisfy the row rule, column rule, and grid rule. So that is to check if every row of the puzzle contains the number 1 to 9 exactly once, every column contains the number 1 to 9 exactly once, and every 3X3 subgrid contains the number 1 to 9 exactly once. The nested loops for checking rows are given to you for your reference. Read the comments to understand it. Then complete the code to check the columns and the subgrids. 4. Complete the public boolean isComplete(int [][] puzzle) method. This method checks if the current puzzle is completed or not. You can check if some cells are 0 or not in the puzzle array. If you find some cells with 0s, return false; if none of the cells is 0 return true. The given printArray() method can be used to print the current puzzle. Empty cells will be printed to represent the missing values that need to user to figure out. The given Main.java contains the code that plays the Sudoku game. In the while loop, when the puzzle is not completed, then the program will promote the user to enter the row, column, and value that the user wants to enter in the puzzle. DONT MODIFY THIS FILE. Just use it to test your game.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions