# pset3 Game of fifteen -make

it is not swapping the values but instead copying it ...pls someone tell me where the problem lies in my code...

``````//move function
bool move(int tile)
{
void swap(int *a,int *b);

//check the given tile value , and if not correct
if (tile == 0 || tile > d*d - 1 || tile < 0)
{
return false;
}
//if the given input for tile is correct
else
{
//iterate to find the value

//for row
for (int row = 0; row < d; row++)
{
//for column
for (int col=0;col<d;col++)
{
//check for the match
if (tile == board[row][col])
{
//check for the blank space around the tile

//above the tile
if(board[row-1][col] == 0)
{
swap(&tile , &board[row-1][col]);
return true;
}
//below the tile
else if(board[row+1][col] == 0)
{
swap(&tile , &board[row+1][col]);
return true;
}
//to the left of the tile
else if (board[row][col-1] == 0)
{
swap (&tile , &board[row][col-1]);
return true;
}
//to the right of the tile
else if (board[row][col+1] == 0)
{
swap(&tile ,&board[row][col+1]);
return true;
}
//if there is no blank space above , below ,to right or to
//left of the tile
else
{
return false;
}
}
}
}

return false;
}
}
``````

here is my swap function

``````//function for swap
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
``````

output after giving the tile value..

``````15 14 13 12

11 10 9 8

7 6 5 4

3 1 2 4
``````

after going through it over n over... it seems to work just fine if the code is change to

``````// above the tile
if (board[row-1][col] == 95)
{
board[row-1][col] = tile;
board[row][col] = 95;

return true;
}
``````

or if its changed to this..

``````// to the right of the tile
else if (board[row][col+1] == 95)
{
swap(&tile, &board[row][col+1]);
board[row][col] = 95;

return true;
}
``````

95 is nothing but '_' in ascii

pls tell me why is it so..thank u

• Two main points: 1) You use too much whitespace. You don't have to leave an empty line between each line in your code. It makes your code unreadable. And you should spend a little more time working on your formatting of the code. I have fixed the code above, but you will have to learn to do it by yourself. 2) You have declared `swap()` inside your `move()` function, and even though this might compile (if you also implemented `swap()` inside `move()`) it's not the right way to declare a new function. It should probably be outside any other function. – ChrisG Sep 13 '17 at 6:33
• @ChrisG could u pls explain me in a bit detail as to y I should not declare swap() inside move() – shaistha Sep 13 '17 at 7:36
• And thank you for formatting my code – shaistha Sep 13 '17 at 7:36
• It has to do with scope. For example, the scope of a variable is inside the most immediate curly braces surrounding it. If you try to access it outside those curly braces it will be undefined. Same thing for functions. Since you declare a function inside another function, the inner function can only be used by the outer function, and no other functions. That is usually unwanted. You want your functions to be able to be called by any other function in your code. – ChrisG Sep 13 '17 at 13:11
• @ChrisG Okey...could you tell me...with my code as example...how I may use another function ...what if I really want to use swap()...what will be the correct way to use it...just so I get the clarity ...thank you – shaistha Sep 13 '17 at 14:36