0

I cant understend why, but my move function does not work properly. It looks correct to y mind. But no Can you please chck what's wrong?

bool move(int tile)
{
    if(tile > d*d-1 || tile <= 0)
    {
        return false;
    }
    for(i=0; i<d; i++)   
    {
        for(j=0; j<d; j++)
        {
            if(board[i][j] == tile)
            {
                if((board[i-1][j]==0 && i>0) || (board[i+1][j]==0 && i<d) || (board[i][j-1]==0 && j>0) || (board[i][j+1]==0 && j<d)) //is there draw near our title        {
                    board[i][j] = 0;
                    for(int m = 0; m < d; m++)
                    {
                        for(int n = 0; n < d; n++)  //look for gap tile
                        {
                            if(board[m][n] == 0)
                            {
                                board[m][n] = tile;
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}
1

In board[i-1][j]==0 && i>0, the i>0 is meant to be a guard, it would have to be performed before you access board[i-1][j]. && will evaluate its left side first, and if the result is false, not evaluate the right side at all (short-circuit).

i<d in the same context should be i<d-1.

If you simply board[m][n] = tile;, you now have the same tile twice. Don't forget to also board[i][j] = 0;.

| improve this answer | |

You must log in to answer this question.

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