I am working on an 8 queens problem, I am to place all 8 queens on the board before figuring out if they will attack one another. I am attempting to do this using a 2d array. How do I create random queens (1's) for each row, but making sure there is only 1 queen per column? This is what I have so far... which is just creating/printing the array of 0's.

public class EightQueens2 {

    public static void main(String[] args) {

        // create an 8x8 array for the chess board
        final int CHESS_BOARD = 8;

        int[][] board = new int[CHESS_BOARD][CHESS_BOARD];

        //fill in the board with zeros, empty squares
        for(int i = 0; i <  board.length; i++){
            for(int j = 0; j < board[i].length; j++){

                System.out.print(board[i][j] + " ");
            }
                System.out.println();
            } 


    }
    }

There are more than one ways to do this, but here's one:
Start with an ArrayList containing the numbers of the columns with no queen. Initially it will contain the numbers 1..8.
For each row, pick a column number from the ArrayList at random, place a queen at that row/col, remove that entry from the ArrayList (thus ensuring that you never place another queen in that column)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.