I have been able to draw the board properly. I can locate the blank tile and tell when the blank tile is adjacent to the tile the user wants to move. My program warns the user when he/she selects a non-adjacent tile.

But I cannot figure out how to swap them once I know they are adjacent! Any hints or pseudocode would be much appreciated!!!!

2 Answers 2


If board[][] is an array that stores the board's current configuration and board[tracki2][trackj2] contains the tile, and you need to swap it with board[tracki1][trackj1], then you can do it in this way.

int x = board[tracki1][trackj1];
board[tracki1][trackj1] = board[tracki2][trackj2];
board[tracki2][trackj2] = x;

x is declared int because we know that elements of board are integers.

  • I tried using the code you provided, it doesn't work for me :(
    – hunter
    Aug 20, 2014 at 1:04
  • That worked for me and I don't see any logical error in it, may be some another reason would be leading to that problem.
    – sinister
    Aug 20, 2014 at 9:11
  • This sorta worked for me. When the board redraws after "true" is returned, I do end up with the character for the blank spot (in my case "") in the spot where the "tile" to be moved was before. But i still get a "" in the original blank spot. So now I have two. Also, when I try a second move afterwards, it seems to act as though the swap has not occurred. In a 4 x 4 board. the blank is in the bottom right corner below the 4 and to the right of the 2. After the swap, I do get a blank in the 4 spot. But also in the original zero spot. Ideas? Sep 7, 2014 at 0:05
  • Relevant excerpt of my code is: for (int j = 0 ; j < d; j++) { if (board[i][j] == tile) { // test to see if the tile being moved is above the blank tile if (i + 1 == ii && j +1 == jj) { printf("The tile being moved %d located placeholder = board[ii][jj]; board[ii][jj] = board[i][j]; board[i][j] = placeholder; return true; } Sep 7, 2014 at 0:10
  • Why do you have only ` if (i + 1 == ii && j +1 == jj)` condition for swapping? I suppose these to be insufficient, there are more possible cases, consider all up, down, right, left.
    – sinister
    Sep 7, 2014 at 3:31

Well in my case, I found it helpful not to keep track of the space but rather check if what the user chooses is next to the space.... if they choose a number in the corner, I check two sides, On the side, three sides, in the middle, four sides, then swap... Hope this helps :)

You must log in to answer this question.

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