0

Hey! My name is Samuel and I'm 14 years old. I am doing fifteen.c in problem set four. My program compiles and runs perfectly and my initiate and draw functions seem to work correctly as well. Though, when I try to move a number, I find that every move is illegal. Could someone please help me with my "move" function?

bool move(int tile)
{
int blank_space = 0, temp;

if (tile == 0)
{
    return false;
}
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (board[i][j] == tile)
        {
            if (j > 0)
            {
                if (board[i-0][j-1] == blank_space)
                {
                    temp = board[i][j];
                    blank_space = board[i-0][j-1];
                    board[i][j] = temp;
                    return true;
                }
            }
            if (j > d-1)
            {
                if (board[i-1][j-0] == blank_space)
                {
                    temp = board[i][j];
                    blank_space = board[i-1][j-0];
                    board[i][j] = temp;
                    return true;
                }
            }
            if (i < 0)
            {
                if (board[i+0][j+1] == blank_space)
                {
                    temp = board[i][j];
                    blank_space = board[i+0][j+1];
                    board[i][j] = temp;
                    return true;
                }
            }
            if (i < d-1)
            {
                if (board[i+1][j+0] == blank_space)
                {
                    temp = board[i][j];
                    blank_space = board[i+1][j+0];
                    board[i][j] = temp;
                    return true;
                }
            }
        }
    }
}
return false;
}

Thank you! :D,

From,

Samuel

0

Two problems jump out that at least contribute to the problem. When will this if (j > d-1) evaluate to true? When will this if (i < 0) evaluate to true?

These may not be the only problems in the function. Don't forget that debug50 (or gdb) is your friend. And if the debugger seems "stuck", it's probably because fifteen is waiting for input.

You must log in to answer this question.

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