This Sudoku solution validator/verifier/checker implemented in Java is extremely hardcoded code. How do I think modularly?
import java.util.*;
// problems with the code will be reported in comments
public class Example {
public static void main(String[] args) {
int[][] solution = {
{ 9, 6, 3, 1, 7, 4, 2, 5, 8 },
{ 1, 7, 8, 3, 2, 5, 6, 4, 9 },
{ 2, 5, 4, 6, 8, 9, 7, 3, 1 },
{ 8, 2, 1, 4, 3, 7, 5, 9, 6 },
{ 4, 9, 6, 8, 5, 2, 3, 1, 7 },
{ 7, 3, 5, 9, 6, 1, 8, 2, 4 },
{ 5, 8, 9, 7, 1, 3, 4, 6, 2 },
{ 3, 1, 7, 2, 4, 6, 9, 8, 5 },
{ 6, 4, 2, 5, 9, 8, 1, 7, 3 }
};
boolean ok = true;
int[] count = new int[9];
// row-wise loop
for (int i = 0; i < solution.length; i++) {
// -1 added for array index starting from 0, means 9 in above means count[8]
for (int j = 0; j < solution[0].length; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
}
// column-wise loop
for (int i = 0; i < solution.length; i++) {
// -1 added for array index starting from 0, means 9 in above means count[8]
for (int j = 0; j < solution[0].length; j++) {
count[solution[j][i] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
}
// grid check 3x3
// 1st grid,2nd grid, 3rd grid
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
for (int j = 3; j < 6; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
for (int j = 6; j < 9; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
}
// 4th,5th,6th grid
// so on
for (int i = 3; i < 6; i++) {
for (int j = 0; j < 3; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
for (int j = 3; j < 6; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
for (int j = 6; j < 9; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
}
// 7th,8th,9th grid
// so on
for (int i = 6; i < 9; i++) {
for (int j = 0; j < 3; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
for (int j = 3; j < 6; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
for (int j = 6; j < 9; j++) {
count[solution[i][j] - 1]++;
}
ok = checkIfOk(count);
System.out.println(ok);
reset(count);
}
System.out.println(ok);
}
public static void reset(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
}
public static boolean checkIfOk(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 1) {
return false;
}
}
return true;
}
}
Few of the problems that I will report:
The code for checking the 3x3 subgrids is repetitive and could be streamlined. It's currently hard-coded for each set of grids (1st-3rd, 4th-6th, 7th-9th) with unnecessary repetition.
In checkIfOk function, I've not checked for non-appearance of a number.
Most crucial part of implementation is I didn't know how to return "ok"'s boolean value. That's why I've put all the printlns. I am not aware how to combine all of these systematically.