0

After running the check50, errors below appear:

:( 3x3.txt wins the 3x3 board
\ expected an exit code of 0, not a prompt for input
:( 4x4.txt wins the 4x4 board
\ expected an exit code of 0, not a prompt for input

I tested with .txt and instead of win, it says Move tile: Retry. Illegal move. See my function won:

bool won(void)
{
    int counter = 1;

    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {

            if (board[i][j] == counter && counter < (d*d))
            {
                counter++;
            }
            else
            return false;

        }
    }
   return true;

}
0

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 | |
  • Ok so I changed my codes with your logic (see below) but it keeps on changing the top left number. i/j + 1 <else if (board[i + 1][j] == 0 && i + 1 < d)> i/j - 1<else if (board[i][j - 1] == 0 && j - 1 >= 0)> – cocobongo Mar 5 '16 at 19:54
  • Can you please edit your question and add your current code? – Cliff B Mar 5 '16 at 21:57
  • Hi Cliff, code above has been edited; swap with 1 now works. Thanks. But the problem with changing number on top left still remains. – cocobongo Mar 7 '16 at 16:42
  • Good news and bad news. The good news is that your move is working perfectly now. I plugged it into my program to test it. The bad news is that your problem lies somewhere else in your program. Do any of your other functions alter any data? – Cliff B Mar 7 '16 at 18:15
  • Thanks for confirming Move works. Indeed, it was solved by removing, in Won, an altered data. On the other hand, I updated above a new issue with Won. – cocobongo Mar 8 '16 at 16:44

You must log in to answer this question.

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