Why does this program never end?

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
I've been working on this program of mine for a month or so (it's a sudoku solver, I've been testing whether or not it's feasible to solve a sudoku puzzle using a brute force method)

Anyway after a couple of weeks of digging out bugs my program works flawlessly... Except for one aspect, that is. It never really terminates? I keep seeing "Running..." at the bottom of my screen, something none of my programs have ever done before.

This one was written in java. The general layout of it is something like this:

Code:
public class sudoku solver {
public static void main(String[] args) {
System.out.println("Sudoku solver V1");
System.out.println("Enter sudoku puzzle clues");
// User inputs some staff, bla bla bla
boolean puzzlesolved = false;
while (puzzlesolved == false) {
/* Code to solve
the puzzle
goes here */
if ( [TheConditionsForAValidSudokuSolution] = true) {
puzzlesolved = true;
}
}
System.out.println("Done.");
}
}

I know for a fact that the while loop stops executing (as it should, since solved is made true eventually), but it never outputs "Done.", my laptop is really warm, and the IDE I use still says it's running. I have to force quit the program.

Anyone know what it could be? I've checked everywhere for a missing closed bracket or semicolon or anything like that. Nada.
 

Youkai

Demon
Member
Joined
Jul 1, 2004
Messages
2,552
Trophies
1
Age
36
Location
Germany , NRW
XP
2,445
Country
Germany
hard to say when you can't or don't want to post the real code ^^V

in your example you made several typos which would cause a problem with the loop like
first you used "puzzlesolved" than you used "puzzle solved" and in the end only "solved" as your variable which won't work, but as you seem to be able to write a Programm I assume you are not as stupid as to make stuff like this wrong in the actually programm

I am not a Programmer so I only know the basics but yeah ;)
(I mostly used C++ and VB ... )



P.S. does it try to solve a Sudoku which has numbers inside already ? maybe the numbers in the Sudoku make in unsolvable ?
 

Arras

Well-Known Member
Member
Joined
Sep 14, 2010
Messages
6,318
Trophies
2
XP
5,407
Country
Netherlands
Try just using the debugger/break points to check what exactly happens, I suppose. Just plonk down a break point after the loop to check whether it exits it properly and what happens afterwards, at least. The code thing you posted doesn't really help. It just leads to questions like "why would you set a flag to true, then immediately check whether it's false? just put the code inside the first if".
 

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,626
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
hard to say when you can't or don't want to post the real code ^^V

in your example you made several typos which would cause a problem with the loop like
first you used "puzzlesolved" than you used "puzzle solved" and in the end only "solved" as your variable which won't work, but as you seem to be able to write a Programm I assume you are not as stupid as to make stuff like this wrong in the actually programm

I am not a Programmer so I only know the basics but yeah ;)
(I mostly used C++ and VB ... )



P.S. does it try to solve a Sudoku which has numbers inside already ? maybe the numbers in the Sudoku make in unsolvable ?

yeah no I was typing that with my eyes shut while lying down on a sofa at college, just removed all those mistakes. no of course I didn't make those mistakes in my actual code :)

Try just using the debugger/break points to check what exactly happens, I suppose. Just plonk down a break point after the loop to check whether it exits it properly and what happens afterwards, at least. The code thing you posted doesn't really help. It just leads to questions like "why would you set a flag to true, then immediately check whether it's false? just put the code inside the first if".


you're right, for the sake of the example it's useless so i removed the redundancy. It makes sense inside the context of my actual code though.

I just posted it to show the general structure. basically I have a while loop that will continue as long as a boolean is false, but some of the code executed within said loop may make the boolean true, thereby presumably exiting the while loop. That's all I wanted to get across really

i'll put a break point when the boolean is assigned a value of true and see what happens. thanks. weird issue huh
 

Reiten

Well-Known Member
Newcomer
Joined
Feb 28, 2014
Messages
82
Trophies
1
XP
2,311
Country
Germany
The only reason I can see for the loop never ending is that this part never evaluates to true(also there is a typo there. Assuming that in the real code you have == and not = in the if statement):
Code:
if ( [TheConditionsForAValidSudokuSolution] = true) {
puzzlesolved = true;
}
No idea how you do the checks and what puzzle you use, so can't help with that.
I'd also suggest you use a do{}while loop, but that's not really important.
 

TVL

#|
Member
Joined
Feb 17, 2004
Messages
577
Trophies
2
Location
World -1
XP
2,307
Country
Sweden
"if ( [TheConditionsForAValidSudokuSolution] = true)"

That line sets [TheConditionsForAValidSudokuSolution] to true.

Use == to actually test and not set it.


Edit: But that would make the loop exit right away... I actually tried it and I do get to see "Done." written out. Putting a bool in place of [TheConditionsForAValidSudokuSolution]. I had to look if maybe you had your statement outside of main... but it isn't, so I have no idea, have you run that code just as you posted it? You have probably edited out where it all goes wrong.

If I do change it to == It's locked up in an infinite loop though. That's how you know there's no solution to the sudoko "yeah, the program just hangs and you have to force quit.". Making something userfriendly is just a big waste of time. ;)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    SylverReZ @ SylverReZ: But I bet that would be more for a flashcart than a consumer repro board.