0

I've been working on fifteen.c for a little bit now. Move works just fine except it fails on one of the check50 tests - move up-up-left-down-down-left-up-up-right-down-down-right test. Is there an issue with how I am checking the moves?

Thanks in advance.

Here is my code:

if ( tile == 0 )
{
    return false;
}
for ( int row = 0; row < d; row++ )
{
    for ( int column = 0; column < d; column++ )
    {
        if ( board[row][column] == tile )
        {
            if ( board[row - 1][column] == 0 && row - 1 > 0 )
            {
                board[row - 1][column] = tile;
                board[row][column] = 0;
                return true;
            }
            else if ( board[row + 1][column] == 0 && row + 1 < d )
            {
                board[row + 1][column] = tile;
                board[row][column] = 0;
                return true;
            }
            else if ( board[row][column - 1] == 0 && column - 1 > 0 )
            {
                board[row][column - 1] = tile;
                board[row][column] = 0;
                return true;
            }
            else if ( board[row][column + 1] == 0 && column + 1 < d )
            {
                board[row][column + 1] = tile;
                board[row][column] = 0;
                return true;
            }
        }
    }
}
return false;
1

Look at the following:

if ( board[row - 1][column] == 0 && row - 1 > 0 )

Why does row-1 have to be greater than 0? 0 is still valid.

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

4
  • When I remove the " && row - 1 > 0 " bit, I get even more errors. When I remove both the " && row - 1 > 0 " and the " && column - 1 > 0 " bit, I still get even more errors. Is there something else wrong with my code? – SuperNovaCoder Sep 16 '16 at 20:23
  • Maybe I should have been more clear. row-1 is still valid at 0, but not at -1. – Cliff B Sep 16 '16 at 20:46
  • Thank you so much! It worked when I made it > - 1 – SuperNovaCoder Sep 16 '16 at 20:48
  • row>0 or row-1>=0 would also have worked and might make more sense a year later if you had to look back at it, especially row-1>=0. ;-) – Cliff B Sep 16 '16 at 20:52

You must log in to answer this question.

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