Program 19 60 points -GROUP PROJECT AVAILABLE Due May 1st 11:59 PM INTRODUCTION
ID: 3824283 • Letter: P
Question
Program 19 60 points -GROUP PROJECT AVAILABLE Due May 1st 11:59 PM INTRODUCTION From h wikipedia.org/wiki/Sudoku: "Sudoku (Japanese: sudoku), sometimes spelled Su Doku, is a logic-based placement puzzle, also known as Number Place in the United States. The aim of the canonical puzzle is to enter a numerical digit from 1 through 9 in each cell of a 9x9 grid made up of 3x3 subgrids (called "regions"), starting with various digits given in some cells (the "givens"). Each row, column, and region must contain only one instance of each numeral. For this assignment, you are going to write a Sudoku validator for 4x4 grids made up of 2x2 regions instead of 9x9 grids made up of 3x3 regions. Sudoku puzzles that use 4x4 grids only use the numerical digits 1 through 4 instead of 1 through 9 Here are two c 4x4 Sudoku grids: 4x4 Sudoku Example #1 4x4 Sudoku Example #2 Notice how each row, column and region has the individual numbers from 1 through 4 used only once. This means that the values used in each row. column and region must add up to 10 (1 2 3 4). This is how we will check our 4x4 Sudoku grids for this assignment. Allow your user to enter their Sudoku grids row-by-row, separating each of the four values by a space and hitting ENTER at the end of the row When the user has entered all 16 values into the program, you should output validation checks for each region, row and column. An example run can be seen below with sample user input underlined. Welcome to the Sudoku Checker v1.0! This program checks simple small 4x4 Sudoku grids for correctness. Each column, row and 2x2 region contains the numbers 1 through 4 only once To check your Sudoku, enter your board one row at a time, with each digit separated by a Hit ENTER at the end of a row space Enter Row 1. 3 2 4 1 Enter Row 2 4 1 3 2 Enter Row 3 1 4 2 3 Enter Row 4 2 3 1 4Explanation / Answer
The base program is here. with a main method. You can print output depending on your file.
public class Sudoku_Checker
{
public static void main(String[] args)
{
//sPECIFY YOUR ARRAY HERE
int[][] arr = {
{1,2,3,4},{3,4,2,1},{1,2,3,4},{4,3,2,1}
};
//using this function to see if its right
System.out.println(validate_Sudoku(arr, 2));
if(validate_Sudoku(arr, 2))
System.out.println("VALID");
else
System.out.println("INVALID");
}
public static boolean validate_Sudoku(int[][] my_sudoku_arr, int grid_size)
{
final int size = my_sudoku_arr.length;
//At first we will check the range of values, in our case 1 to 4.
if (!checkRangeOfValues(my_sudoku_arr, 1, size))
{
return false;
}
//Now check for duplicates in both rows && columns
for (int arr_rows = 0; arr_rows < size; ++arr_rows)
{
if (!checkDuplicates_Row(my_sudoku_arr, arr_rows))
{
return false;
}
}
for (int arr_cols = 0; arr_cols < size; ++arr_cols)
{
if (!checkDuplicates_Column(my_sudoku_arr, arr_cols))
{
return false;
}
}
// We must also look out for duplicate values within each of the small squares
for (int squareRow = 0; squareRow < size; squareRow += grid_size)
{
for (int squareCol = 0; squareCol < size; squareCol += grid_size)
{
if (!checkSquare(my_sudoku_arr, squareRow, squareCol, grid_size))
{
return false;
}
}
}
return true;
}
//checking each sub-square
private static boolean checkSquare(int[][] my_sudoku_arr, int squareRow, int squareCol, int grid_size)
{
boolean[] checkIfExists = new boolean[my_sudoku_arr.length];
for (int arr_rows = squareRow; arr_rows < (squareRow + grid_size); ++arr_rows)
{
for (int arr_cols = squareCol; arr_cols < (squareCol + grid_size); ++arr_cols)
{
int i = my_sudoku_arr[arr_rows][arr_cols] - 1;
if (!checkIfExists[i])
{
checkIfExists[i] = true;
}
else
{
return false;
}
}
}
return true;
}
//checking for duplicates in each row
private static boolean checkDuplicates_Row(int[][] my_sudoku_arr, int rowNum)
{
final int size = my_sudoku_arr.length;
boolean[] checkIfExists = new boolean[size];
for (int arr_cols = 0; arr_cols < size; ++arr_cols)
{
int i = my_sudoku_arr[rowNum][arr_cols] - 1;
if (!checkIfExists[i])
{
checkIfExists[i] = true;
}
else
{
return false;
}
}
return true;
}
//checking for duplicates in each col
private static boolean checkDuplicates_Column(int[][] my_sudoku_arr, int colNum)
{
final int size = my_sudoku_arr.length;
boolean[] checkIfExists = new boolean[size];
for (int arr_rows = 0; arr_rows < size; ++arr_rows)
{
int i = my_sudoku_arr[arr_rows][colNum] - 1;
if (!checkIfExists[i])
{
checkIfExists[i] = true;
}
else
{
return false;
}
}
return true;
}
//checking for range..in our case 1 to 4
private static boolean checkRangeOfValues(int[][] my_sudoku_arr, int min, int max)
{
for (int arr_rows = 0; arr_rows < my_sudoku_arr.length; ++arr_rows)
{
for (int arr_cols = 0; arr_cols < my_sudoku_arr[0].length; ++arr_cols)
{
if (my_sudoku_arr[arr_rows][arr_cols] < min || my_sudoku_arr[arr_rows][arr_cols] > max)
{
return false;
}
}
}
return true;
}
}
You will have to change main method to display out put as required. The checkDuplicates_Row function, makes sure each row is good, and the checkDuplicates_Col funcation maes sure that each column is good. The validateSudoku function finally prints T/F depending on valid Sudoku.
if(validate_Sudoku(arr, 2))
System.out.println("VALID");
else
System.out.println("INVALID");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.