0

I'm completely stuck. I don't understand what I'm doing wrong! My won functions correctly checks the game and allows for reordering of the entire board when checking with 3x3.txt but when it gets to the end it exists with "illegal move". I understand that something is wrong when checking the last tile, but I don't understand what. I tried so many different things now and nothing is working. Please help!

bool won(void)
{
for(int i = 0; i < d; i++)
 {
   for(int j = 0; i <d; j++)
      {
         if(board[i][j] != board[i][j-1] || board[i][j] != board[i+1][j-2])
         {
        return false;
         }
       if(board[d-1][d-2] != d*d - 1 && board[d-1][d-1] != 0)
       {
         return false;
       }
    }
 }
return true;
}

Another attempt:

bool won(void)
{
   int counter = 1;

   for(int i = 0; i < d; i++)
   {
     for(int j = 0; j < d; i++)
       {
          if(board[d-1][d-1] != 0)
            {
              return false;
             }
             if(board[i][j] != counter)
               {
                  return false;
                  counter++;
              }
       }
    }
   return true;
}

Per suggestions below, I made the following changes, but it's still not working! I'm really ready to give up on this. It's so upsetting :( Alway been able to figure out my errors before.

bool won(void)
{
int counter = 1;

for(int i = 0; i < d; i++)
{
 for(int j = 0; j < d; i++)
   {
    if(counter == d*d)
        {
            counter = 0;
        }

         if(board[i][j] != counter)
           {
            return false;
          }
    counter++;
   }
}
return true;
}
1

The following line has serious issues:

     if(board[i][j] != board[i][j-1] || board[i][j] != board[i+1][j-2])

If j == 0, then it's going to try to check board[i][-1], an invalid tile. This will produce unpredictable results. Same logic applies when i == d-1 and the second clause. If i == d-1, then board[d][j-2] is invalid. Same again for j-2.

Next, no two tiles should ever be equal, so the whole statement is kinda pointless. This means that won() will always return false. So, ... well, you just edited your question and added a major revision to the code, so I'll assume you threw out the first version completely. Good choice. ;-)

The new code is much better, but not complete. What happens when it gets to the last tile? The value should be 0, not counter. The code needs to check this.

You're almost done. Happy coding!

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

| improve this answer | |
  • 1
    Polina, please also pay attention to this block: { return false; counter++; } Increment seems to be in a wrong place. – paul Oct 9 '16 at 20:51
  • thanks for your help! – Polina A Oct 9 '16 at 21:14
  • I made the following changes per your suggestions, but it's still not working! :( bool won(void) { int counter = 1; for(int i = 0; i < d; i++) { for(int j = 0; j < d; i++) { if(counter == d*d) { counter = 0; } if(board[i][j] != counter) { return false; } counter++; } } return true; } – Polina A Oct 9 '16 at 21:15
  • Here's a clue: You have a typo in a for loop. – Cliff B Oct 9 '16 at 21:29
  • Oh my god!! You're right! So silly! Thanks very much! – Polina A Oct 10 '16 at 1:26

You must log in to answer this question.

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