0

For some reason whenever I try to swap 0 with board[0][0] I get an illegal move error. Besides that everything is fine. What gives?

int column = 0;
int row = 0;
int box;
int space;

for (int i = 0; i < d * d; i++)
{
    space = 0;
    column++;
    if (column == d)
    {
        column = 0;
        row++;
    }

    if (board[row][column] == tile)
    {
       if (column > 0)
        {
           if (space == board[row][column - 1])
           {
                box = board[row][column - 1];
                board[row][column - 1] = board[row][column];
                board[row][column] = box;
                return true;
           }
        }       

        if (column < d - 1)
        {
             if (space == board[row][column + 1])
           {
                box = board[row][column + 1];
                board[row][column + 1] = board[row][column];
                board[row][column] = box;
                return true;
           }
        }

        if (row > 0)
        {
            if (space == board[row - 1][column])
            {
                box = board[row - 1][column];
                board[row - 1][column] = board[row][column];
                board[row][column] = box;
                return true;    
            }
        }

        if (row < d - 1)
        {
            if (space == board[row + 1][column])
            {
                box = board[row + 1][column];
                board[row + 1][column] = board[row][column];
                board[row][column] = box;
                return true;
            }
        }
    }

}

return false;

}

1

The code never tests 0,0. In the search for tile, the first array element that is checked is board[0][1], continuing through the rest of the array. That means that the if (board[row][column] == tile) statement always evaluates as false and the code drops down to the retun false statement.

The reason that it never tests 0,0 is that column is initialized to 0, and then incremented, column++; before the first time it is checked.

It's an easy fix, but I'll let you figure it out. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .