0

I'm a beginner so please bear with me. For the game of fifteen I have successfully implemented the Draw function but I'm not having the same luck with Move. It is very erratic, sometimes the tiles move, other times they're just replaced with the blank tile. I end up with a lot of blank tiles on my board. Also, moves that are out of bounds aren't always counted as illegal, but in one of my previous tries I found that as long as the tile was in the same row or column, not necessarily adjacent to the blank, the move would be permitted. Why is this? I'm obviously missing something very important but after hours of going over this and trying to change it, I can't figure it out on my own. Time has come to ask for help. If anyone can hint at what I'm missing or what's wrong I will greatly appreciate it. Let me know if you need more information or code.

bool move(int tile)
{
    int blank = board[d][d];

    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == tile)
            {
                if(board[(i-1)][j] == blank)  //up
                {
                    board[(i-1)][j] = tile;
                    board[i][j] = blank;
                    return true;
                }

                if(board[(i+1)][j] == blank)  //down
                {
                    board[(i+1)][j] = tile;
                    board[i][j] = blank;
                    return true;
                }

                if(board[i][(j-1)] == blank)  //left
                {
                    board[i][(j-1)] = tile;
                    board[i][j] = blank;
                    return true;
                }

                if(board[i][(j+1)] == blank)  //right
                {
                    board[i][(j+1)] = tile;
                    board[i][j] = blank;
                    return true;
                }
            }
        }
    }

    return false;
}
3

You have two serious problems here. First, your move() function defines int blank = board[d][d]; The array element board[d][d] doesn't exist because both indexes run from 0 to d-1. Anything with d as an index is out of range. Even if it had d-1 as the two indexes, it's still broken because the blank cell is only known to be in the lower right when the game starts. After the first move, it could be anywhere.

And once again...

This problem is showing up over and over. When the board[][] array indexes are set by d, and i and j must both be less than j, what happens when i+1 or j+1 = d? In other words, say that d=3 and i = 2. Now look at this:

else if (board[i + 1][j] == 0)

With these numbers, i+1 = 3 and d=3, but the index must be < 3! Same logic applies to i-1 going to -1. In all these cases, the system won't automatically generate an error, but it will look where it thinks the array element can be found. That means it's going to look at another memory location that is probably in use by something else. If it happens to be data that passes the given test, it's going to do a swap and move a tile out of the array.

The array indexes MUST NOT be allowed to be out of the valid range!

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

| improve this answer | |
  • Even though your response didn't exactly answer my question it made me think about other things that eventually led me to solve my problem. Thank you. – jetgo Mar 8 '16 at 15:10

You must log in to answer this question.

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