For this assignment I need to write a program that verifies whether a given 9X9
ID: 3641207 • Letter: F
Question
For this assignment I need to write a program that verifies whether a given 9X9 matrix of numbers is a valid Sudoku grid. The given matrix (input) will be from a text file called sudoku.txt. The code should print out the matrix and say if it is valid or invalid such as this:
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
5 6 4 9 1 8 7 3 2
2 3 7 8 9 1 4 6 5
4 5 6 7 8 9 3 2 1
7 8 9 3 2 5 6 1 4
6 5 1 9 3 4 7 2 8
8 2 3 6 1 7 9 4 5
3 7 6 2 8 2 1 9 4
Not valid Sudoku grid.
----------------------------------------------------------------------
The sudoku.txt file displays this when opened:
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
5 6 4 9 1 8 7 3 2
2 3 7 8 9 1 4 6 5
4 5 6 7 8 9 3 2 1
7 8 9 3 2 5 6 1 4
6 5 1 9 3 4 7 2 8
8 2 3 6 1 7 9 4 5
3 7 6 2 8 2 1 9 4
------------------------------------------------------------------
This is for an introduction to Java class so the program should not be overly complex. Thank you I would greatly appreciate a solution!
Explanation / Answer
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class SudokuValidator {
public static void main(String[] args) throws FileNotFoundException {
int [][] grid = new int[9][9];
Scanner in = new Scanner(new FileReader("sudoku.txt"));
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
grid[row][col] = in.nextInt();
}
}
printSudokuGrid(grid);
System.out.println();
if (validSudokuGrid(grid)) {
System.out.println("Valid Sudoku grid.");
}
else {
System.out.println("Not valid Sudoku grid.");
}
in.close();
}
private static void printSudokuGrid(int [][] grid) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
}
private static boolean validSudokuGrid(int [][] grid) {
boolean [] numCheck;
//Check values
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (grid[row][col] > 9 || grid[row][col] < 1) {
return false;
}
}
}
//Check rows
for (int row = 0; row < 9; row++) {
numCheck = new boolean[9];
for (int col = 0; col < 9; col++) {
//look for similar number in numCheck,
//if it is true then it already has in the row/column/region
if (numCheck[grid[row][col] - 1]) {
return false;
}
else {
numCheck[grid[row][col] - 1] = true;
}
}
}
//Check columns
for (int col = 0; col < 9; col++) {
numCheck = new boolean[9];
for (int row = 0; row < 9; row++) {
if (numCheck[grid[row][col] - 1]) {
return false;
}
else {
numCheck[grid[row][col] - 1] = true;
}
}
}
//Check regions
// = 3 * ( 0 0 0 1 1 1 2 2 2) + 0 or 1 or 2
// 0 1 2 3 4 5 6 7 8
//0+0*3 0 . . . . . . . . .
//1+0*3 1 . 0 . . 1 . . 2 .
//2+0*3 2 . . . . . . . . .
//0+1*3 3 . . . . . . . . .
//1+1*3 4 . 3 . . 4 . . 5 . (region index)
//2+1*3 5 . . . . . . . . .
//0+2*3 6 . . . . . . . . .
//1+2*3 7 . 6 . . 7 . . 8 .
//2+2*3 8 . . . . . . . . .
// Hence, row = (reg/3)*3 + {0,1,2}
// (0 or 1 or 2) / 3 = 0
//because (3 or 4 or 5) / 3 = 1
// (6 or 7 or 8) / 3 = 2
//Similar, col = (reg%3)*3 + {0,1,2}
// (0 or 3 or 6) % 3 = 0
//because (1 or 4 or 7) % 3 = 1
// (2 or 5 or 8) % 3 = 2
for (int reg = 0; reg< 9; reg++) {
numCheck = new boolean[9];
for (int row = reg / 3 * 3; row < reg / 3 * 3 + 3; row++) {
for (int col = reg % 3 * 3; col < reg % 3 * 3 + 3; col++) {
if (numCheck[grid[row][col] - 1]) {
return false;
}
else {
numCheck[grid[row][col] - 1] = true;
}
}
}
}
return true; //if the grid passes all checks, return true
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.