Search code examples
javaarrays2drowsn-queens

How do you check a row in a 2D char array for a specific element and then count how many of that element are in the row? (Java)


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;
    }

Solution

  • 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;
    }