Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

8 queens 1 dimensional array

$
0
0
I have to write the program 8 queens. where the out put looks like
Q******
**Q****
****Q**
******Q
*Q*****

I have to print out all 92 solutions.
I am not even sure if I am on the right track.

I get this error message when I go to compile.
error: method displayBoard in class EightQu eens cannot be applied to given types;
displayBoard();
^
required: int[]
found: no arguments
reason: actual and formal argument lists differ in length

public class EightQueens
{
private final int size = 8;
int [] board = new int [size];


    public  void main (String args []) // main method
    {
    int  y =0;
    board[0] = 1;
    while (y >=0)
    {
board[y]++;
    }
while ((board[y]< 8) && (solveQueens(y)));
    if (board[y] < 8)
    {
    if( y < 7)
    {
    board[++y]=1;
    }
    else
    {
        displayBoard();
    }
    }
    }//  end main

    private  boolean solveQueens(int y)
    {
int x = board[y];

for (int i = 1;i < y; i++)
{
int t = board[y-1];
if (t==x || t==x - i || t ==x +1)
{
    return true;
private void displayBoard (int[] board)
{
    int A =board.length; // sets the lenght for the loops
    for ( int i =0; i < A; i++)
    {
        for (int j=0; j < A; j++)
        {
            if (board[A] == j)
                System.out.print("Q ");
            else
                System.out.print("* ");
         }
        System.out.println();
    }
System.out.println();
    }
}



ps.
is there an easy way to copy and past my code from linux vim to this site. with out duplicating the code that is in the vim?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>