0

This is an N Queens problem where the board has been given and you must use methods to check where the rows, the columns and diagonally. My method for checking the row is here:It works if you were counting the Queens as a whole but I only want to check row by row, resetting the count and rowcount.

private boolean oneQueenPerRow() //ensures that there is only 1 queen in each row
{
    int count = 0;
    int rowcount = 0;
    for (int i = 0; i < board.length; i++)
    {
        //count  = 0;
        for (int j = 0; j < board.length; j++)
        {
            //rowcount = 0;
            while (rowcount <= size-1)
            {
                if (board[i][j] == QUEEN)
                {
                    count++;
                    rowcount++;  
                }
                if (board[i][j] == BLANK)
                {
                    rowcount++;
                }
            }
            if (count != 1) // if size replaces 1 then it works, but counts Q's as a whole
            {
                return false;
            }             
        } 
    }
    return true;
}

The idea is that all the methods return true or false and then are called by final boolean method. If all are true than the board is a valid solution. If one is false, the board is not a valid solution. Here is a text file example I was given:

4 BQBB BBBQ QBBB BBQB

(They should be stacked..)

I don't have enough knowledge about arrays and for loops to tell if this is going all the way through the whole file or just a row at a time, although trust me when I say I have exhausted all resources.

I have been working on this for days and I can't figure it out and connection with my Prof is spotty because of this virus! I desperately need help!

private boolean noDiagonalAttacks() //makes sure that Queens cannot attack diagonally
    {
        for (int i = 0; i < board.length; i++)
        {
            int count = 0;
            for (int j = 0; j < board.length; j++)
            {
                if (board[i][j] == QUEEN)
                {
                    if(this.toRight() == false || this.toLeft() == false)
                    {
                        return false;
                    }
                    count++;
                }
            }
        }
        return true;
    }

    private boolean toRight()
    {
        for (int i = 0; i < board.length; i++)
        {
            for (int j = 0; j < board.length; j++)
            {
                while (board[i][j] != board[i][size-1] || board[i][j] != board[size-1][j]) //add a count to this?
                {
                    if (board[i][j] == QUEEN)
                    {
                        return false;
                    }
                }
            }    
        } 
        return true;       
    }

    private boolean toLeft()
    {
        for (int i = 0; i < board.length; i++)
        {
            for (int j = 0; j < board.length; j++)
            {
                while (board[i][j] != board[i][0] || board[i][j] != board[size-1][j])
                {
                    if (board[i][j] == QUEEN)
                    {
                        return false;
                    }
                }
            }
        }    
        return true;
    }
2
  • May I ask why you use (count < 1) || (count > 1) instead of (count != 1)?
    – NomadMaker
    Apr 1, 2020 at 4:42
  • Yes I thought of that since! I've changed it, thank you :)
    – cb-9099
    Apr 1, 2020 at 11:52

1 Answer 1

0

I tried it once ago and it worked, Hope it help you.

private boolean oneQueenPerRow() {
    int foundQueens;
    for (int i = 0; i < board.length; i++) {
        foundQueens = 0;//each loop is a checked row
        for (int j = 0; j < board.length; j++) {
            if (board[i][j] == QUEEN)
                foundQueens++;
        }
        if (foundQueens > 1) return false;
    }
    return true;
}

private boolean oneQueenPerDiagonal() {
    int inLeftRight = 0;
    int inRightLeft = 0;
    for (int i = 0; i < board.length; i++) {
        if (board[i][i] == QUEEN)
            inLeftRight++;
        if (board[i][board.length-i-1] == QUEEN)
            inRightLeft++;
    }
    return inLeftRight < 1 && inRightLeft < 1;
}

9
  • I understand putting 'foundQueens' in the first loop, but it seems go to wonky if the first array element it checks is not 'Q'..
    – cb-9099
    Apr 1, 2020 at 13:00
  • My scanner for the file was incorrect. This works, thank you!
    – cb-9099
    Apr 1, 2020 at 20:54
  • is it possible you did the Diagonal method for this as well I'm really stuck!!
    – cb-9099
    Apr 2, 2020 at 2:33
  • Is the main diagonal and the opposite ? or all the diagonals in the board ? Note: I want to help but if you do not get experience and get frustrated you can't learn, however I will. Apr 2, 2020 at 2:36
  • I have been working on this all day due to a bug in my scanner and I thought I had it nearly figured out and then there was a a gaping logical hole and now i'm not sure how to proceed. When you say main diagonal and opposite do you mean 'down and to the left' and 'down and to the right'? Because if so, yes.
    – cb-9099
    Apr 2, 2020 at 2:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.