Java Programming: Okay, Im writing a program to check any column duplicates, row
ID: 3875125 • Letter: J
Question
Java Programming:
Okay, Im writing a program to check any column duplicates, row duplicates and box duplicates in a Sudoku game (the three rules of Sudoku). I have my rowDuplicates(), and columnDuplicates() meathod working properly. However my issue is my subBoxDuplicates() meathod. I can't get it to work properly. It's the very last meathod posted in this picture:
// Returns true if there are row duplicates, else false public static boolean rowDuplicates (int [[1 sudokuBoard) int roW; int col int tempNum int nxtCol; // Traverse rows 0 (n 3) for (row=0; rowExplanation / Answer
The solution that you have written doesn't work because it will stop at the first number because it is comparing tempNum with itself. Even if that is handled (through a flag variable), for each subbox, your solution is taking only first number as tempNum and comparing it with all the other numbers. It doesn't take take other numbers and moves directly to next sub box.
I have handled the number comparing itself situation with a flag variable.
Here is my method:
public static boolean subBoxDuplicates(int [][] sudokuBoard) {
int row, col, tempNum, k;
for(row = 0; row < 9; row += 3) {
for(col = 0; col < 9; col += 3) {
for(k = 0; k < 9; k++) {
tempNum = sudokuBoard[row + k/3][col + k%3];
//we will compare rest of the numbers with this tempNum
//we will do this for every number
//start checking from row,col and with the conditions
int flag = 0;
for(int l = 0; l < 9; l++) {
//this will loop through all the values in the current box
//flag if flag is > 0 then it means that number was encountered before
System.out.println(sudokuBoard[row + l/3][col + l%3]);
if(tempNum == sudokuBoard[row + l/3][col + l%3]) {
if(flag == 0) {
flag = 1;
}
else
return true;
}
}
}
}
}
return false;
}
Here is the complete working example with debug statements and a sample solved sudoku to explain the traversal:
public class sudoku {
public static boolean subBoxDuplicates(int [][] sudokuBoard) {
int row, col, tempNum, k;
for(row = 0; row < 9; row += 3) {
for(col = 0; col < 9; col += 3) {
System.out.println("Row: "+row+" Col: "+col);
//this needs to loop for every number and check if there is no duplicate
for(k = 0; k < 9; k++) {
tempNum = sudokuBoard[row + k/3][col + k%3];
System.out.println("Number to compare: " + tempNum);
//we will compare rest of the numbers with this tempNum
//we will do this for every number
//start checking from row,col and with the conditions
int flag = 0;
for(int l = 0; l < 9; l++) {
//this will loop through all the values in the current box
//flag if flag is > 0 then it means that number was encountered before
System.out.println(sudokuBoard[row + l/3][col + l%3]);
if(tempNum == sudokuBoard[row + l/3][col + l%3]) {
if(flag == 0) {
flag = 1;
}
else
return true;
}
}
}
System.out.println("==========================================================================");
}
}
return false;
}
public static void main(String[] args) {
int[][] multi = new int[][]{
{ 1, 5, 2, 4, 8, 9, 3, 7, 6 },
{ 7, 3, 9, 2, 5, 6, 8, 4, 1 },
{ 4, 6, 8, 3, 7, 1, 2, 9, 5 },
{ 3, 8, 7, 1, 2, 4, 6, 5, 9 },
{ 5, 9, 1, 7, 6, 3, 4, 2, 8 },
{ 2, 4, 6, 8, 9, 5, 7, 1, 3 },
{ 9, 1, 4, 6, 3, 7, 5, 8, 2 },
{ 6, 2, 5, 9, 4, 8, 1, 3, 7 },
{ 8, 7, 3, 5, 1, 2, 9, 6, 4 }
};
boolean a = subBoxDuplicates(multi);
if(a == true) {
System.out.println("The solution is incorrect");
}
else
System.out.println("The solution is correct");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.