0

I have been stuck on this for two days now. Would really appreciate some fast help! every time i run this it and put in Tile to move:3 all the numbers on the board change can u give me ideas on how to solve this issue. Also if i make board[r][c]==tile in move() it says illegal move and when i run check50 it doesnt recognize it and just returns false thats why i had to change all my code. atleast now it doesnt say illegal move....please help me!!!

    void init(void)
{
// TODO
int board[d][d];
int t=d*d;
for(r=0;r<d;r++)
{
    for(c=0;c<d;c++)
    {
        board[r][c]=t-1;
        t=t-1;
    }
}
if(d%2==0)
 {
    board[d-1][d-2]=2;
    board[d-1][d-3]=1;
}
}

 int br;
int bc;

 void draw(void)
  {
  // TODO
   int board[d][d];
    for(r=0;r<d;r++)
    {
    for(c=0;c<d;c++)
    {
        if(board[r][c]==0)
        {
            printf("__");
        }
        else
        {   
            printf("%2i  ",board[r][c]);
        }

    }
    printf("\n");
      }
    }

 bool move(int tile)
     {
   for(r=0;r<d;r++)
{
    for(c=0;c<d;c++)
    { 
        if(tile==board[r][c])
        {
            br=r;
            bc=c;
        }
    }
   }
    if(board[br][bc+1]==0 &&  c+1<=d-1) 
           {
               board[br][bc+1]=tile;
               board[br][bc]=0;
               return true;
           }
           else if(board[br][bc-1]==0 && c-1>=0) 
           {
               board[br][bc-1]=tile;
               board[br][bc]=0;
               return true;
           }
           else if(board[br-1][bc]==0 && r-1>=0) 
           {
               board[br-1][bc]=tile;
               board[br][bc]=0;
               return true;
           }
           else if(board[br-1][bc]==0 && r+1<=d-1) 
           {
               board[br-1][bc]=tile;
               board[br][bc]=0;
               return true;
           }

         return false;// TODO
           }
1

Why is this line, board[r][c]=t-1; in the move() function? It will reassign values to every tile on the board. It looks like a remnant of a cut and paste from init(). The code needs to find the blank tile and the tile to move without altering anything along the way.

[EDIT: following code update]

As it turns out, a larger problem is lurking in the code that wasn't originally posted. The following line appears in both init() and draw(): int board[d][d]; The board[ ][ ] array needs to be a global variable. By redeclaring it inside of each function, the code creates something called a shadow variable. Simply put, a shadow variable is a local variable that masks another var of the same name. So, when the code creates the shadow var array in init, that array gets processed locally in init, but the global array remains hidden and unaltered. When the code exits init, the shadow array ceases to exist and the global array (assuming you created one) is once again exposed. The same process happens in draw. This is the source of the random numbers. Also, by declaring locally, the content of the array is not accessible in any other function.

Once you cure this problem, there are still issues with move, but they are of a different nature, so a new question would be in order after you've worked on it.

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

2
  • I edited my code but i still get the same problem when i enter a number for tile to move it doesn't do anything and changes all the numbers to some random numbers.
    – user18412
    Jul 11 '17 at 15:29
  • You just made my day! THANK YOU SO MUCH FOR YOUR EXPLANATION! IT WORKS PERFECTLY.I have been trying this for so long!!! Thank you thank you thank you !
    – user18412
    Jul 11 '17 at 19:53

You must log in to answer this question.