# pset3 fifteen move creates double numbers

So my move function finally compiles, but when I run the program and chose a legal tile (i.e. one by the blank) the blank turns into the tile I picked. So from this:

``````8   7   6

5   4   3

2   1   _
``````

I pick 3 and get

`````` 8   7   6

5   4   3

2   1   3
``````

This is my code:

``````bool move(int tile)
{
for(int i=0; i<d; i++)
{
for(int j=0; j<d; j++)
{
if(board[i][j] == 0)
{
//if tile is next to 0
if(board[i-1][j] == tile || board[i][j-1] == tile || board[i+1][j] == tile || board[i][j+1] == tile)
{
int swap = tile;
tile = board[i][j];
board[i][j] = swap;
return true;
}
}
}
}
return false;
}
``````

The code for swapping I know works because it is the same one from sort, and when I use gdb to print "tile" after the swap it is 0, as it should be. So where is this 0 getting lost in translation?

you are swapping values of tile and board[i][j] instead of board[i][j] and other adjacent square which contains tile.

``````// Put the value of tile into swap
int swap = tile;

// swap has the value of 3

// Take the value from the board[i][j] and place it into tile
tile = board[i][j];

// tile has the value of 0

// Place the value of swap into board[i][j]
board[i][j] = swap;

// final result: board[i][j] = 3, tile = 0
``````

It is correctly placing the value of `tile` into the board at i, j.

Notice that there is no code that sets the value of the neighbouring tile. In your example the value of `board[i][j + 1]` also needs to be updated, to take on the value of the blank cell.

Also take care when reading and writing values at the edges of the board. What would happen if `i = 2`, and `j = 2`, and you try read `board[i + 1][j]` or `board[i][j + 1]`.

• to my understanding though, "tile" IS the "neighboring tile" i.e. board[i][j+1], so it should be 0. regarding the last comment, wouldn't it just return false? – Sofia Jul 31 '14 at 19:54
• `tile` is just a copy of what is in that location. If you say `tile = board[i][j + 1]`, the contents of the cell get copied into `tile`. When you then set tile to a value, such as `tile = 3`, you're again copying the value into `tile`. If you want to set the value of a tile on the board, it needs to be set directly, such as `board[i][j + 1] = 3`. Later on in the course you will see how to "link" a variable to another one using pointers. – Luke Van In Jul 31 '14 at 20:08
• If you try to reference a location outside of the board you're essentially accessing memory outside of your program. C will happily let you do this, and return whatever values happened to be stored there. These values could be anything. When this happens your program can act unpredictably or crash. You need to add logic to ensure that you don't try to read values from, or write values to, locations outside the board. – Luke Van In Jul 31 '14 at 20:14
• Sure, but say i'm checking if board[i+1][j] == tile, and that position happens to be outside the board. It's pretty unlikely that this random memory would be the same number as tile...right? so the stuff inside the if statement just wouldn't execute. I just don't see the problem. – Sofia Aug 1 '14 at 3:26
• It is unlikely to be the same sure. I wasn't really correct in saying "C will return the value" if you try to access the memory outside the board. I meant that C will let you try to access the memory, and if it doesn't crash, you will get some random value back. There is still a chance the random value could be a zero and your program would not behave correctly when that happens. Your program might work sometimes, other times it will just crash, or behave "randomly" because of these random values. – Luke Van In Aug 1 '14 at 7:19