# pset3 game of fifteen-1 and 2 not swapping

This is my code for initializing and draw.
1 and 2 however do not swap.
I have also made a function to swap so as to use on move too.
But the move also reports illegal move.

https://gist.github.com/5fa45dd76833593badc7df9d432fdf6f

``````/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
// TODO
int i,j;
int k=d*d-1;
for(i=0;i<d;i++)
{
for(j=0;j<d;j++)
{
board[i][j]=k--;
if(k%2!=0)
{
break;
}
else
{
if((board[i][j]==2) && (board[i][j-1]==1))
metrytoexchange(&board[i][j],&board[i][j-1]);
}
}
}
}

/**
* Prints the board in its current state.
*/
void draw(void)
{// TODO
int k=d*d-1;
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
board[i][j]=k--;
if(board[i][j]==0)
{
printf("_");
}
else
printf("%d \t",board[i][j]);
}
printf("\n");
}
}
void metrytoexchange(int *a,int *b)
{
int temp= *a;
*a = *b;
*b = temp;
}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
int k=d*d-1;
int blank_space=0;
//dont go beyond the grid
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{

if(tile<k && tile>0 && tile==board[i][j])
{
continue;
}
else
{
break;
}
//iterate to check the position of of blank tile;left to right up and down if not return false
if(board[i-1][j]!=blank_space || board[i][j-1]!=blank_space || board[i+1][j]!=blank_space || board[i][j+1]!=blank_space)
{
return false;
}
//else swap tile with blank_space
else
{
metrytoexchange(&tile,&blank_space);
return true;
}
}
}

return false;
}
``````

## 1 Answer

There is a problem in the draw function that is confusing the issue (and the humans!). The only functions that should "set" or "change" a board value (tile) are init and move. This line `board[i][j]=k--;` is "setting" a tile value! So it is changing the current board state. Once you fix that, it will uncover some serious problems in the init function. Some hints:

`if(k%2!=0)` k is odd every other iteration of the loop. That test should be against a constant value.

`if((board[i][j]==2) && (board[i][j-1]==1))` The loops assign "tile" values in decreasing order. If tile 2 has been placed, tile 1 has not, so this expression will never evaluate to true.

I didn't review the rest of the program so as not to spoil your fun! :).

• thank your yuri and Dino.will work on the hint and give feedback on the same. Dec 8 '16 at 13:32
• the below code is corrected.but move changes _ to 95.how do i correct that?@DinoCoderSaurus gist.github.com/a6cbbc29a9f839d2ebc6865e2cd0c65c Dec 9 '16 at 10:44
• There is a "set" in the draw function here `board[i][j]='_';`. Not coincidentally, the underbar/underscore character "_" is ascii 95. There are a few other problems like here `if(board[3][1]==2 && board[3][2]==1) ` That is essentially hard coded for a 3x3 board. It will fail some tests in check50. And the move function does not check for the edge of the board. If you have problems working those things out, suggest you create a new question on the forum. Dec 12 '16 at 1:03
• i want to print the underscore not 95.i have corrected the if(board[3][1]==2...... Thank you for the feedback and correction. Dec 13 '16 at 11:13
• if(board[i][j]==0) { board[i][j]='_'; printf("%c \t",board[i][j]); } Dec 13 '16 at 12:30