0

I have tried many different variations of the check feature of the move function to try and find if it is a legal move. This is about my forth attempt and I still cant get it to find an illegal move. If the number is in the middle it is found illegal but if its around the outside it always let it move I cant figure out the problem. Thank you very much in advance. Here is my move function code.

bool move(int tile) { if (tile > 0 && tile < (d * d))

for (int i = 0; i < d; i++)
{   
    for (int t = 0; t < d ; t++)
    {            
        if (board[i][t] == tile)
        {
            if (board[i][t+1] == 0 && t < d ) 
                    {       
                        board[emptyz][emptyx] = tile;
                        board[i][t] = 0;
                        emptyz = i;
                        emptyx = t;
                        return true;
                    }
            else if (board[i+1][t] == 0 && i < d ) 
                    {       
                        board[emptyz][emptyx] = tile;
                        board[i][t] = 0;
                        emptyz = i;
                        emptyx = t;
                        return true;
                    }
            else if (board[i][t-1] == 0 && t > -1) 
                    {       
                        board[emptyz][emptyx] = tile;
                        board[i][t] = 0;
                        emptyz = i;
                        emptyx = t;
                        return true;
                    }
            else if (board[i-1][t] == 0 && i > -1) 
                    {       
                        board[emptyz][emptyx] = tile;
                        board[i][t] = 0;
                        emptyz = i;
                        emptyx = t;
                        return true;
                    }
            else
                {
                    return false;
                }
        }
    }
}
return false;

}

1

Right idea to check limits of t and i, but the wrong choices for limits. For example, with if (board[i][t+1] == 0 && t < d ), t+1 must be less than d, not t. If d is 3 and t is 2, then t+1=3, which is out of bounds.

Similarly, else if (board[i-1][t] == 0 && i > -1) should be i>0 or i-1> -1. Same logic.

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

  • That was it! Thank you very much, it is greatly appreciated. Saved me from my own brain. – Tyler Gotto Sep 6 '16 at 10:36

You must log in to answer this question.

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