Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I fix this stack overflow error?
    primarykey
    data
    text
    <p>So I have what I think is pretty good code for a sudoku solver in java but I need some help with this method. It gives me a stack overflow when I embed it in a main method. The problem is that my method doesn't know how to turn around and fix its mistakes. I need a boolean flag (one that, unlike the one used in the code below, actually works preferably) or something to let it know when it should turn back and when it can again go forwards and continue solving the game. Thanks for any help you can give</p> <pre><code>public void play(int r, int c){//this method throws the StackOverflowError if(needAtLoc(r,c).size()==9){ int num=1+generator.nextInt(9); setCell(r,c,num,this); if(c&lt;8){ System.out.println(this);/////////////// play(r, c+1); } else{ play(r+1, 0); } } else{ if(needAtLoc(r,c).size()==0){//no possible moves THIS IS THE PROBLEM LINE!!! if(c&gt;0){ play(r, c-1);//play last cell, in column to left } else{ if(r==0){ play(r,c);//first square, so must play again (can't go back) } else{ play(r-1, 8);/*first cell of row so must go to previous row and the end column*/ } } } else{//if there are possible moves int num=needAtLoc(r,c).remove(generator.nextInt(needAtLoc(r,c).size())); setCell(r,c,num,this);//set the value of the cell System.out.println(this);////////////// if(r==8 &amp;&amp; c==8){//the end of the cell has been reached so must end recursive call return; } else{ if(c&lt;8){ play(r, c+1);//normal, next cell } else{ play(r+1, 0);/*last cell in row so we go to next one in the first column ("return" button)*/ } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload