2

Recently, I have been re-learning Java.

I'm working on a program to solve the 8 Queens problem to practice my skills. When I run my program, I don't get any solutions, but I can't seem to find the bug.

The code in question:

public void solve(char [] [] board, int row){
        for (int i = 0; i < 8; i++) {
            if (board[row][i] == 'O') {             //if the position is blank
                char[][] newBoard = board.clone();  //make a duplicate
                newBoard[row][i] = 'q';             //place a queen in the valid position
                invalidate(newBoard, row, i);       //mark the places the queen can take
                //printBoard(newBoard);               //display to check
                if(row < 7){                        //if we didn't just do the final row
                    solve(newBoard, (row+1));       //do the next row
                }else {                             //if we DID just do the final row
                    printBoard(board);              //print the solution
                }
            }
        }
}

When I have my print method running to get the process, I get 5 boards. The last one is:

qXXXXXXX
XXqXXXXX
XXXXqXXX
XqXXXXXX
XXXqXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX

This result seems to imply that the program runs through the first-most path without any problem, but doesn't go through the others for some reason I just can't figure out.

As I touched on at the beginning, I'm not super experienced with coding, so any help would be greatly appreciated!

Joe
  • 31
  • 5
  • 1
    I recommend you read Eric Lippert's ["How to debug small programs"](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) article. – Stephen C Apr 21 '18 at 03:28
  • 1
    Read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems for tips on debugging your program. – Code-Apprentice Apr 21 '18 at 03:28
  • As a side note - the question itself is very well written. Cheers! – Rann Lifshitz Apr 21 '18 at 03:31
  • [This](https://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java/13792183) may help. A side note: better not to use numbers like 8 or 7. If it is a constant, make it a constant field and give it a meaningful name. – c0der Apr 21 '18 at 04:27
  • Consider adding answer to you own question – c0der Apr 21 '18 at 04:31
  • That was exactly the problem! Unfortunately I figured that out moments before you posted. Oh well, thanks anyway. :D (And thanks for the pointer-- I'll be sure to remember to use constants in the future!) – Joe Apr 21 '18 at 04:33
  • Also add @c0der to a comment to address it to me – c0der Apr 21 '18 at 06:10

1 Answers1

1

Solved the problem. Apparently the .clone() method doesn't work for 2-D arrays, so the program re-used the failed one. I used a simple nested loop and a new array and the issue was resolved. Thank you everyone for your help!

Joe
  • 31
  • 5
  • Clone does work ... but just not in the way that you expected it to work. (I didn't spot that problem. However, if you have provided an MVCE, we would have been able to reproduce it ...) – Stephen C Apr 21 '18 at 04:42
  • Just correcting StephenC's typo, so Joe understands. MVCE should have been MCVE, which refers to "[Mcve]" - well worth a read. – Richardissimo Apr 21 '18 at 06:51