1

I'm having trouble with my move funtion in the game of fifteen. For some reason the chosen tile and the adjecent zero won't swop places. I've been working on this function for too long and reaching out for help now. I eally hope somebody can help me out!

/** * If tile borders empty space, moves tile and returns true, else * returns false. */

bool move(int tile) { int temp;

if (tile == 0)
{
    return false;
}
for (int row = 0; row < d; row++)
{
    for (int column = 0; column < d; column++)
    {
        if (tile == board[row][column]) //checks array for tile
        {

            if (column > 0) //makes sure C does not acces any space to the left of 2D-array
            {
                if (board[row-0][column-1] == 0) // checks if blank space is left to tile
                {
                    temp = tile;     //switch the tiles
                    board[row][column] = board[row-0][column-1];
                    board[row-0][column-1] = temp;
                    return true;
                }
            }
            if (row > 0) //makes sure C does not access any space above 2D-array
            {
                if (board[row-1][column-0] == 0) // checks if blank space is above tile
                {
                    temp = tile;     //switch the tiles
                    board[row][column] = board[row-1][column-0];
                    board[row-1][column-0] = temp;
                    return true;
                }
            }
            if (column < d-1) //makes sure C does not access any space to the right of 2D-array
            {
                if (board[row+0][column+1] == 0) // checks if blank space is right to tile
                {
                    temp = tile;     //switch the tiles
                    board[row][column] = board[row+0][column+1];
                    board[row+0][column+1] = temp;
                    printf("DEBUG: Do i move trough here?");
                    return true;
                }
            }
            if (row < d-1) //makes sure C does not acces any space underneath 2D-array
            {
                if (board[row+1][column+0] == 0) // checks if blank space is underneath tile 
                {
                    temp = tile;     //switch the tiles
                    board[row][column] = board[row+1][column+0];
                    board[row+1][column+0] = temp;
                    return true;
                }
            }
        }
    }
}
return false;

}

3

Well, I don't know how to tell you this, but I plugged your move function into my code and it worked perfectly. I suspect that you have a problem elsewhere in your code. Have you altered something in main that you shouldn't have? Is it possible that you are reinitializing the board after every move so that it just appears that you aren't making the move? When you wrote draw(), you wrote it using the current positions of each tile and not the initial positions, right? (This is where I'm betting the problem lies, since the board is redrawn after each move.) And you're also storing the results of the swap, right?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Thank you for looking at my code and even more also for testing. I'm happy to hear that my function seems to work in your code. I was thinking the same; 'My program reinitializes after every move', but the initializing function is outside of the while loop in main. Draw() is using the current positions and I think I'am storing the results of my swap right. – SurTJ Jan 18 '16 at 11:01
  • I'm also thinking something is going wrong in main but everything seems fine to me and C is leaving me Clueless. I will add in my full code through Pastbin because it seems Stackechange has some trouble showing my program due to the functions. pastebin.com/s2HcFN8V – SurTJ Jan 18 '16 at 11:56
  • For some reason I implemented another function call to move in main. That's why my the move function is repeated and the tiles where being swopped back again. The problem is solved now. Thanks Cliff B for looking at my problem and youre usefull input. – SurTJ Jan 18 '16 at 13:18

You must log in to answer this question.

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