Mateus Gauto
Mateus Gauto

Reputation: 11

Problems with If-statement condition

I'm learning C, and my teacher asked my class to make a Tic Tac Toe game. I managed to make most of it, but I'm kind of stuck. I've made a function with the purpose of checking the lines, one by one, and if it finds out all the line numbers are equal to 1 or 2, the game loop receives the victory number and stops. But there seems to be something wrong with the condition I've put on the if inside the function, and I can't figure out what it is. It does not return a number to the game loop even when a line is complete with a number. I've tried searching for if conditions but I couldn't find my error.

Here's the function:

int checkLines (int mat [3][3])
        {

            int i;
            int victory;

            for (i=0; i<3; i++)
            {
                      if ((mat [i][0] ==1) && (mat [i][1] ==1) && (mat [i][2] ==1))
                      {
                                 victory = 1;

                      } else if ((mat [i][0]==2) && (mat [i][1]==2) && (mat [i][2]==2))
                      {
                                 victory = 2;

                      }else
                      {
                           victory = 0;
                      }

            }
        return victory;
        }

Upvotes: 1

Views: 71

Answers (5)

LPs
LPs

Reputation: 16243

Your loop always go througt all your matrix. You have to break your loop when victory is != 0. With a while instead of for you can do it.

int checkLines (int mat [3][3])
{
   int i=0;
   int victory = 0;
   while ((victory == 0) && (i<3))
   {
      if ((mat [i][0] ==1) && (mat [i][1] ==1) && (mat [i][2] ==1))
      {
         victory = 1;
      } 
      else if ((mat [i][0]==2) && (mat [i][1]==2) && (mat [i][2]==2))
      {
         victory = 2;
      }

      i++;
    }
    return victory;
}

Take notes that your code is not taking care about diagonal and vertical victories.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409442

It's because you don't break out of the loop once you set victory, which means the loop will continue and then reset victory to zero in the else part.

Actually, instead of setting victory and breaking out of the loop, just return directly, and after the loop you know you have no "victory" so you can always return 0 there.

Upvotes: 2

Novak007
Novak007

Reputation: 436

int checkLines (int mat [3][3])
        {

            int i;
            int victory; /* You get it then you are happy to leave */

            for (i=0; i<3; i++)
            {
                      if ((mat [i][0] ==1) && (mat [i][1] ==1) && (mat [i][2] ==1))
                      {
                                 victory = 1; break;

                      } else if ((mat [i][0]==2) && (mat [i][1]==2) && (mat [i][2]==2))
                      {
                                 victory = 2; continue;

                      }else
                      {
                           victory = 0; continue; 
                      }

            }
        return victory;
        }

Upvotes: 0

Paul R
Paul R

Reputation: 213170

As @JoachimPileborg has already pointed out, you need to break out of the for loop if you find a victory.

Change:

if ((mat [i][0] ==1) && (mat [i][1] ==1) && (mat [i][2] ==1))
{
    victory = 1;
} else if ((mat [i][0]==2) && (mat [i][1]==2) && (mat [i][2]==2))
{
    victory = 2;
}

to:

if ((mat [i][0] ==1) && (mat [i][1] ==1) && (mat [i][2] ==1))
{
    victory = 1;
    break;  // found a victory - break out of loop
} else if ((mat [i][0]==2) && (mat [i][1]==2) && (mat [i][2]==2))
{
    victory = 2;
    break;  // found a victory - break out of loop
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

In your code, even after satisfying a victory condition, the for loop executes 3 times, essentially marking the value of victory to 0 in the end.

You need to stop looping once you have a match in either of the if statement. You can make use of a break; statement at the end of if and else if blocks.

Upvotes: 0

Related Questions