Java Sudoku Solving Program: Debugging Help Needed

Click For Summary

Discussion Overview

The discussion revolves around debugging a Java program designed to solve Sudoku puzzles. Participants are sharing their insights and suggestions to identify and fix issues related to the program's logic, particularly concerning the validation of Sudoku rules.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to create a Sudoku solving program and requests debugging assistance.
  • Another participant asks for clarification on where the program's output deviates from expectations.
  • A participant notes that the program outputs a solved board with repeats in the grids, indicating an incorrect solution.
  • Suggestions are made to check function calls that incorrectly validate assignments.
  • Participants propose debugging strategies, such as stopping at different points in the code to identify where the problem arises.
  • One participant suggests checking the iteration logic in the grid validation section of the code.
  • Another participant points out that the condition used in the grid check should be an OR (||) instead of an AND (&&) to correctly identify duplicates.
  • There are discussions about hardcoding issues and the potential use of continue statements to optimize the grid checking process.
  • A participant acknowledges a mistake in their code and reports that correcting it led to a satisfactory output.

Areas of Agreement / Disagreement

Participants express differing views on specific coding practices and debugging strategies. While some suggestions are accepted and lead to improvements, there is no overall consensus on the best approach to debugging the program.

Contextual Notes

Some participants mention potential hardcoding issues and the need for proper validation checks, but these points remain unresolved in terms of their implications for the program's functionality.

Kaura
Messages
122
Reaction score
22
Member warned to post directly in the thread, and give indication of warnings and behavior of program
I am attempting to create a Sudoku Solving Program in Java

This is what I have to far

https://pastebin.com/raw/tgnAGKb2

I have spent close to an hour trying to debug it but to no avail

Can someone more skilled than I please point me in the right direction?
 
Technology news on Phys.org
What goes wrong where?
At which step does the program state deviate from your expectations?
 
It "solves" the Sudoku Board and outputs the solved board but there are repeats in the grids making the solution incorrect
 
Then you should check the function calls that incorrectly claim the assignment would be valid and see what goes wrong there.
 
Why not stop it at different points if possible, i.e., run just the first, say 10 lines and see where, at what step, the problem arises?
 
Yeah when I get the chance I might just try to debug it at every recursive call
Might take forever but it may be my last hope
 
Good luck, if you want help in the Math part, let us know ; unfortunately I am just a beginner programmer, tho others may be able to help you in that regard.
 
Java:
for(int k = (row/3)*3; k < (row/3)*3+3; k++) {
           for(int l = (col/3)*3; l < (col/3)*3+3; l++) {
               if(k != row && l != col) {
                   if(b[row][col] == number) return false;
               }
           }
are you sure about that b[row][col] there? if yes, why are you even iterating over k and l? Hopefully this will solve it?
Also some additional stuff you can think about:
1) you could try to fix some lines where you're using hardcoding (such as the line where you check whether your column or row are above the size of the sudoku board). That's the reason you introduced SIZE in your class, no?
2) you could do use a continue statement within the for loops, since you have already checked the same rows and columns.. in other words, if you are in the top left corner [1]
[1][2][3]
[4][5][6]
[7][8][9]
when you reach the grid iteration you have:already check 2,3,4,7... you don't have to recheck them...
if you are at [5], you have already checked 4,6,2,8... etc...
 
Last edited:
Code:
//check grids//
        for(int k = (row/3)*3; k < (row/3)*3+3; k++) {
            for(int l = (col/3)*3; l < (col/3)*3+3; l++) {
                if(k != row && l != col) {
                    if(b[row][col] == number) return false;
                }
            }

That && should be an ||. If either (k!= row ) or (l != col) than the (k,l) cell differs from the (row,column) cell and you want to check for a duplicate.
 
  • #10
That && should be an ||. If either (k!= row ) or (l != col) than the (k,l) cell differs from the (row,column) cell and you want to check for a duplicate.

That doesn't matter, because you already checked the columns and rows. The output I get now is:

693857124
124569387
512473896
346791258
435928671
278346519
859214763
781632945
967185432

The problem is clearly with, the 3x3 boxes, since rows and colums have unique numbers.

.
 
  • #11
did you check your grid iteration?
 
  • #12
if(b[row][col] == number) return false;
This should check b[k][l] (the loop variables), not b[row][col] (which is the place we want to validate itself).
 
  • #13
mfb said:
This should check b[k][l] (the loop variables), not b[row][col] (which is the place we want to validate itself).

ChrisVer said:
Java:
for(int k = (row/3)*3; k < (row/3)*3+3; k++) {
           for(int l = (col/3)*3; l < (col/3)*3+3; l++) {
               if(k != row && l != col) {
                   if(b[row][col] == number) return false;
               }
           }
are you sure about that b[row][col] there? if yes, why are you even iterating over k and l? Hopefully this will solve it?
Also some additional stuff you can think about:
1) you could try to fix some lines where you're using hardcoding (such as the line where you check whether your column or row are above the size of the sudoku board). That's the reason you introduced SIZE in your class, no?
2) you could do use a continue statement within the for loops, since you have already checked the same rows and columns.. in other words, if you are in the top left corner [1]
[1][2][3]
[4][5][6]
[7][8][9]
when you reach the grid iteration you have:already check 2,3,4,7... you don't have to recheck them...
if you are at [5], you have already checked 4,6,2,8... etc...

Haha I knew I made some dumb mistake
Thank you so much for finding that
I switched b[row][col] with b[k][l] and the result looks fine now
 
  • Like
Likes   Reactions: mfb

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K