简体   繁体   中英

Java Bishop Chess Board

Im working on figuring out the maximum number of bishops I can place on a nxn board without them being able to attack each other. Im having trouble checking the Diagonals. below is my method to check the diagonals. The squares where a bishop currently is are marked as true so the method is supposed to check the diagonals and if it returns true then the method to place the bishops will move to the next row.

Im not quite sure whats going wrong, any help would be appreciated.

private boolean bishopAttack(int row, int column)
{
    int a,b,c;

    for(a = 1; a <= column; a++)
    {
        if(row<a)
        {
            break;
        }

        if(board[row-a][column-a])
        {
            return true;
        }
    }   
    for(b = 1; b <= column; b++)
    {
        if(row<b)
        {
            break;
        }
        if(board[row+b][column-b])
        {
            return true;
        }
    }   
    for(c = 1; b <= column; b++)
    {
        if(row<c)
        {
            break;
        }
        if(board[row+c][column+c])
        {
            return true;
        }
    }
    return false;
}
for(c = 1; b <= column; b++)

Shouldn't it be

for(c = 1; c <= column; c++)

By the way:

1) Use i, j, k instead of a, b, c, etc. No REAL reason why... it's just convention.

2) You don't have to keep naming new variables. Try something like this:

for(int i = 1; i <= column; i++)
{
    ...
}
//because i was declared in the for loop, after the } it no longer exists and we can redeclare and reuse it
for(int i = 1; i <= column; i++)
{
    ...
}

3) Your error checking is incorrect. It should be something like this:

for(int i = 1; i < 8; i++)
{
    int newrow = row - i;
    int newcolumn = column - i;
    if (newrow < 0 || newrow > 7 || newcolumn < 0 || newcolumn > 7)
    {
       break;
    }
    if (board[newrow][newcolumn])
    {
        return true;
    }
}

Now when you copy+paste your for loop, you only have to change how newrow and newcolumn are calculated, and everything else (including loop variable name) will be identical. The less you have to edit when copy+pasting, the better. We also attempt all 7 squares so we don't have to change the ending condition - the if check within the loop will stop us if we attempt to go out of bounds in ANY direction.

4) Better still, of course, would be using the for loop only once and passing only the changing thing into it... something like...

private boolean bishopAttackOneDirection(int rowdelta, int coldelta, int row, int column)
{
    for(int i = 1; i < 8; i++)
    {
        int newrow = row + rowdelta*i;
        int newcolumn = column + columndelta*i;
        if (newrow < 0 || newrow > 7 || newcolumn < 0 || newcolumn > 7)
        {
           break;
        }
        if (board[newrow][newcolumn])
        {
            return true;
        }
    }
    return false;
}

private boolean BishopAttack(int row, int column)
{
   return BishopAttackInOneDirection(-1, -1, row, column)
   || BishopAttackInOneDirection(1, -1, row, column)
   || BishopAttackInOneDirection(1, 1, row, column)
   || BishopAttackInOneDirection(-1, 1, row, column);
}

Probably not quite the expected answer, but there is no reason to make life more complex then it is.

Im working on figuring out the maximum number of bishops I can place on a nxn board without them being able to attack each other.

public int getMaximumNumberOfNonAttackingBishopsForSquareBoardSize(final int boardSize) {
    if (boardSize < 2 || boardSize > (Integer.MAX_VALUE / 2))
        throw new IllegalArgumentException("Invalid boardSize, must be between 2 and " + Integer.MAX_VALUE / 2 + ", got: " + boardSize);
    return 2 * boardSize - 2;
}

Source: http://mathworld.wolfram.com/BishopsProblem.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM