1

I get the following error on check50:

:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
\ expected "4" character 3 of line 17, not "0"

I have tried this out, and the fourth instruction 'down' returns illegal move, thus my move function must be the problem, however I can't figure out the exact problem never mind trying to resolve it!

Some direction would be appreciated, many thanks

Here is my move function:

bool move(int tile)
{
    // TODO
    //int row = board[d - 1][d - 1]; 
    //int col = board[d - 1][d - 1];
int temp;

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (tile == board[i][j]) 
        {

            {
                if (board[i - 1][j] == 0 && i - 1 > 0)
                {
                    temp = board[i - 1][j];
                    board[i - 1][j] = board[i][j];
                    board[i][j] = temp;
                    return true;
                }
                if (board[i + 1][j] == 0 && i + 1 < d)
                {
                    temp = board[i + 1][j];
                    board[i + 1][j] = board[i][j];
                    board[i][j] = temp;
                    return true;
                }
                if (board[i][j - 1] == 0 && j - 1 > 0)
                {
                    temp = board[i][j - 1];
                    board[i][j - 1] = board[i][j];
                    board[i][j] = temp;

                    return true;
                }
                if (board[i][j + 1] == 0 && j + 1 < d) 
                {
                    temp = board[i][j + 1];
                    board[i][j + 1] = board[i][j];
                    board[i][j] = temp;
                    return true;
                }
            }

        }
    }
}


return false;
}
0

if (board[i - 1][j] == 0 && i - 1 > 0) and if (board[i][j - 1] == 0 && j - 1 > 0) causes a serious problem. board[i - 1][j] will only be 0 if i - 1 is 0. Let's say that i - 1 equals 0. The first part of your if statement evaluates to true, but then we reach i - 1 > 0. If i - 1 equals 0, then you get 0 > 0, which is not true. Thus, the code drops through to the bottom and returns false when it should've returned true. The same goes for j - 1.

In addition, you never use the variable tile that is passed to the function. Might I ask why? Instead of setting board[i][j - 1] and any variation thereof to tile and then continue the swapping process, you create a new variable temp and then swap that way. It'd be more efficient to use tile.

There might be more problems in your code, but I hope that what I stated above will push you in the right direction. If this answers your question, please hit the green checkmark! If you still have errors, comment below.

2
  • 1
    Thank you for your help. with if (board[i-1][j]) == 0 && i - 1 > 0, I was trying to say if the value of (board[i-1][j]) and position of (i-1) is greater than zero do X, although I should probably change that to greater than or equal to 0. Is this the wrong way of doing this? Any clues as to the right way of doing this?
    – Sofia
    Feb 2 '17 at 19:48
  • @Sofia That's not the "wrong" way of doing this, but a more efficient way would be to have if (board[i - 1][j] == 0 && i - 1 > -1) I hope this answers your question, and if it does, please hit the green checkmark! It helps a lot :-) Feb 2 '17 at 22:39

You must log in to answer this question.

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