0

Can anyone see the issue here? It moves "5" from the start position ok, but when it comes to "1" it is just two blank tiles in adjacent squares ("1" disappears). Obviously there is some issue with the "move" function here. Can you help me please?

bool move(int tile)
{
int t; 
//int xrow; int xcol; int orow; int ocol;
for (int xrow = 0; xrow < d; xrow++)
{
    for (int xcol = 0; xcol < d; xcol++)
    {
        if (board [xrow][xcol] == tile)
        {
            if (board [xrow - 1][xcol] == 0)
            {
                t = board [xrow][xcol];
                board [xrow][xcol] = board [xrow - 1][xcol];
                board [xrow - 1][xcol] = t;
                return true;
            }
            else if (board [xrow + 1][xcol] == 0)
            {
                t = board [xrow][xcol];
                board [xrow][xcol] = board [xrow + 1][xcol];
                board [xrow + 1][xcol] = t;
                return true;
            }
            else if (board [xrow][xcol - 1] == 0)
            {
                t = board [xrow][xcol];
                board [xrow][xcol] = board [xrow][xcol - 1];
                board [xrow][xcol - 1] = t;
                return true;
            }
            else if (board [xrow][xcol + 1] == 0)
            {
                t = board [xrow][xcol];
                board [xrow][xcol] = board [xrow][xcol + 1];
                board [xrow][xcol + 1] = t;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
return false;
}
  • Please comment your code and let us know clearly what's the issue, from when you launch the program to when the problem happens. I personally don't get what your problem is. – Aizen Aug 21 '16 at 16:44
  • When I run the program (d=3) and enter number "3" as a tile to move it works fine. But if I choose tile number "1" to move first it shows me this. ---- 8 7 6-------------------------------------------------------------------------------------------------------------------------------- ----5 4 3------------------------------------------------------------------------- ------------------------------------------------------2 _ _ ------------------------------------------------------------------------"zero" goes to the previous position and "1" disappears. – Michael Lil Aug 21 '16 at 17:58
0

The code accesses elements that are not in the array becasue it allows the indexes to go out of range. Say that d=3. The limits for the array indexes are 0 to 2. So, when xrow=2, xrow+1 =3. Similarly, if xrow = 0, xrow-1 = -1. Both of these are out of range. Same for xcol. You need to add code to make sure xrow and xcol don't go out of range.

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

  • Thank you Cliff! – Michael Lil Aug 21 '16 at 20:48

You must log in to answer this question.

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