2

I made the 8x8 chess board and have a lot of the code done, but for some reason it only print out one solution, does anyone know why this may be and how I can fix it?

public class NonAttackingQueens {
    private int[][] board;
    private int solutionCount = 0;
    private boolean solutionFound = false;

    public NonAttackingQueens() {
        board = new int[8][8];
    }

    public boolean canPlace(int x, int y) {
        // Check if a queen is already placed at position (x, y)
        if (board[x][y] == 1) {
            return false;
        }

        // Check horizontal positions
        for (int i = 0; i < 8; i++) {
            if (board[x][i] == 1) {
                return false;
            }
        }

        // Check vertical positions
        for (int i = 0; i < 8; i++) {
            if (board[i][y] == 1) {
                return false;
            }
        }

        // Check diagonal positions
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == 1 && (Math.abs(i - x) == Math.abs(j - y))) {
                    return false;
                }
            }
        }

        return true;
    }

public void solve() {
    // Check if the solutionCount has reached 92
    if (solutionCount == 92) {
        return;
    }

    // Check if all 8 queens have been placed
    int queensPlaced = 0;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (board[i][j] == 1) {
                queensPlaced++;
            }
        }
    }
    if (queensPlaced == 8) {
        // All positions have been checked, so we have found a solution
        solutionCount++;
        System.out.println("Solution " + solutionCount + ":");
        print();
        return;
    }

    // Try to place a queen at each position on the board
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (canPlace(i, j)) {
                // Place a queen at position (i, j) and try to solve the rest of the board
                board[i][j] = 1;
                solve();
                // Backtrack: remove the queen from position (i, j) and try the next position
                board[i][j] = 0;
            }
        }
    }
}

    public void print() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == 1) {
                    System.out.print(" X");
                } else {
                    System.out.print(" O");
                }
            }
            System.out.println();
        }
        System.out.println("---------------");
    }
}

I'm doing this in blueJ, so I tried to run the void solve(); method and it runs, but it only prints out the first of 92 solutions 92 times. It should print out all 92 different solutions.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
doug
  • 21
  • 2
  • 3
    In your search for the next square to put a queen you are starting from (0, 0) each time. This causes your algorithm to find an identical solution several times. Instead only start the search in the next square (or even the next row) from where the previous queen was put. – Ole V.V. Dec 18 '22 at 05:25
  • The way I would do (which is not the only way) would be pass a row number (0 through 7) to `solve()` so that the method will only search fields in that row before (if successful) passing the next higher row number to the recursive call. (And no, I am not finishing your code for you. Its your exercise and you who should learn from coding it. I won’t deprive you of that benefit. If you want a finished solution, you can find one on the Internet.) – Ole V.V. Dec 18 '22 at 17:56
  • thank you so much I think i got it to work, just gotta test it a few more times! – doug Dec 18 '22 at 18:10
  • That’s so good to read! Thanks for reporting back. You can consider posting your solution as an answer yo your own question. – Ole V.V. Dec 18 '22 at 19:37

0 Answers0