Why does this program never end?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Lucifer666, May 21, 2015.

  1. Lucifer666
    OP

    Lucifer666 all the world needs is me

    Member
    1,494
    730
    Apr 22, 2011
    The Fourth Dimension
    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.
     
  2. Youkai

    Youkai Demon

    Member
    2,176
    262
    Jul 1, 2004
    Germany
    Germany , NRW
    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 ?
     
  3. Arras

    Arras GBAtemp Guru

    Member
    5,865
    2,680
    Sep 14, 2010
    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".
     
  4. Lucifer666
    OP

    Lucifer666 all the world needs is me

    Member
    1,494
    730
    Apr 22, 2011
    The Fourth Dimension
    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 :)


    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
     
  5. Reiten

    Reiten Member

    Newcomer
    29
    14
    Feb 28, 2014
    Gambia, The
    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.
     
  6. TVL

    TVL #|

    Member
    484
    217
    Feb 17, 2004
    World -1
    "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. ;)