1

I'm sorry for making another post of this, but I don't want to look other classmates' code while I'm still trying to do this myself.

I'm having trouble with move() in fifteen.c. I'm not sure how to tell whether the tile the user wants to move is adjacent to the empty tile on the board. I used strtok() in initials.c because I wasn't sure of how to use indices there, and now I guess it's coming back.

This is what I have for the function right now:

bool move(int tile)
{
    int empty_tile = 0;
    int temp = 0;
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {

        }
    }
    return false;
}
2

@M.Kleiven's tips are interesting, but I just looked at my solution, and I did not need to use pointers to check if a tile is outside the board. Here's what I did, in pseudo-code:

// initialize board using 0 for blank tile (as recommended in the problem spec)
bool move(int tile)
{
    // find blank tile (0) and remember its location (blankrow, blankcol)
    // find user-selected tile and remember its location (tilerow, tilecol)
    // swap user-selected tile and blank tile, if adjacent
    // this part has a lengthy compound boolean expression ...
    // a valid move will satisfy one of these requirements:
    // either the row is the same and the column is off by plus or minus one
    // or the column is the same and the row is off by plus or minus one
    // if the move is valid, swap tiles and return true
    // else, return false
}

Hope this helps!

2

To check if a tile is adjacent to the tile the user requested to move to with indices you simply use an if statement with the condition you wish to check for, which is then followed by something like board[i-1][j], if I remember correctly this looks up the tile to the left of board[i][j]

  • What about the right, top or bottom? – Osman Zakir Oct 25 '16 at 9:08
  • This is what I have right now: pastebin.com/79SsT6jG – Osman Zakir Oct 25 '16 at 9:14
  • 1
    I forgot to mention, but you should just have to modify the code I gave you a little. Watch the walkthrough on Game of Fifteen, in it Zamyla explains why and how to use the syntax I described in my answer. I also want to mention you can check for all possible cases in a single if. – Martin Kleiven Oct 25 '16 at 9:23
  • Yeah, I saw that video. But I'm not sure how should I word my if-condition to check for the case when the tile the user wants to move is above or below or to the right or left of an empty tile, and also whether or not there's a board edge next to it. Also, to move the tiles, should I just do a swap using a temporary variable? – Osman Zakir Oct 25 '16 at 11:10
  • The questions you ask is all the problems you are set to solve in load(). I can tell as much as board[i-1][j], board[+1][j], board[i][j-1] and board[i][j+1] is all valid C syntax. You can use pointers in some way to check if a tile is outside the board. You can use the method you described to "move". If you need more help them you can ask another question later, but until then you must make an effort trying. – Martin Kleiven Oct 25 '16 at 12:34

You must log in to answer this question.

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