0

This is my move function for the Game of Fifteen. My board initialised okay but check50 is throwing errors at Move. For example:-

:( 3x3 board, move blank up twice \ expected "0" character 5 of line 6, not "3":( 3x3 board, move blank left twice

Also, should I break up the if statement? It seems ridiculously long. Is that bad form?

bool move(int tile)
{
//user has already given tile number but not the tile location.
//First find the tile location
    for (int i=0;i<d;i++)// search board rows for users tile
    {
        for (int j=0;j<d;j++) //search columns
        {
            int blank = 0; //variable 0 = blankspace

            if(tile==board[i][j])
            {
                int x=i; //row
                int y=j; //column

                if (((x==(i-1)) && (j==y)) || ((x==(i+1)) &&(j==y))|| ((i==x) && (y ==(j-1)))|| ((i==x) && (y==(j+1))))
                {
                    board[x][y] = tile;
                    board[i][j] = blank;

                    return true;
                }
            } 
        }
    }
    return false;
}
1

Looks like you never tried the game before submitting to check50. The upside to that: now you can try the tests and check your results. Run the game and try to move the blank tile "up twice". This move always returns false, so every attempt will return "Illegal move".

When x = i it can never be == i + 1 or == i - 1. Ditto y.

You shouldn't be interested in what the indexes (i and j) are. You should be interested in what the board contains at those indexes (ie board[i - 1][j]).

You will have to split up the if, because you need to take a different action depending on which condition is true.

Don't forget the edges of the board. If i is 0, board[i - 1][j] is not valid.

1
  • Thanks heaps for getting me back on track. Your explanation is really clear. – Android1 Apr 8 '17 at 13:52

You must log in to answer this question.

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