Search code examples
javaknights-tour

How to fix the error "Bad Operand Types for Binary Operator '>=' first type: int[] second type int"


**The error is with this line: **

if ((board[r + vertical[movenumber]]) <= 8 && board[r + vertical[movenumber]] >= 1)

**Whole method if needed: **

public void tour()
{

    int starter = 1;

    int start1 = (int)(Math.random() * 8 - 1) + 1;
    int start2 = (int)(Math.random() * 8 - 1) + 1;

    board[start1][start2] = starter;

    int r = start1;
    int c = start2;

    for (int count = 0; count < board[row].length; count++)
    {

        numb[count] = count;

    }

    for (int runs = 2; runs <= 64; runs++)
    {

        int movenumber = (int)(Math.random() * 8 - 1) + 1;

        if ((board[r + vertical[movenumber]]) <= 8 && board[r + vertical[movenumber]] >= 1)
        {

            if (board[r + vertical[movenumber]][c + horizontal[movenumber]] == 0)
            {

                board[r + vertical[movenumber]][c + horizontal[movenumber]] = runs;

                // System.out.println(r + "," + c);

                r = r + vertical[movenumber];
                c = c + horizontal[movenumber];

            }

        }

    }

}

Solution

  • The error is pretty specific - you are trying to compare an array with an int. The board[r + vertical[movenumber]] expression is an array (or int[], specifically), because your board is int[][]. To make it an int, you need to add a second index, just like you do in the following lines: board[r + vertical[movenumber]][c + horizontal[movenumber]].

    And from quick glance at the code, I think it should be r + vertical[movenumber] <= 8 instead.