1

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;
        }
1
  • Instead of testing each row for 1, then 2, you could simplify the test that all elements in the row are equal, then return one of the row elements: if( mat[i][0] == mat[i][1] == mat[i][2] ) { victory = mat[i][0] }. Then the else ifblock is redundant (and could be used instead for the column and diagonal tests perhaps). Commented Jun 17, 2015 at 8:40

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Yup, fixed it! Thanks very much! =D
0

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
}

Comments

0
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;
        }

Comments

0

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.

1 Comment

I fixed it =D and I made similar functions for the diagonal and vertical victories, and they are working too now. Thanks very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.