0

I'm trying to write a Tic-Tac-Toe program in C, and have a while loop that is supposed to check if the game board is filled. If so, the loop will break and the result will be a cat's game.

I also have a function boardFull that takes my 2D game board as an argument and checks each cell for a -1, which indicates that the space is empty.

For some reason, I enter in the loop when the program runs, but after filling up one space, the loop breaks and my program ends. Am I using while correctly?

Code segment:

    while(boardFull(board) != -1)
    {
       if((turn % 2) == 0)
       {
        printf("Player One's turn.\n");
        marker = 1;
       }
       else
       {
        printf("Player Two's turn.\n");
        marker = 0;
       }   

       printf("Which row? ");
       scanf("%i", &row);

       printf("Which column? ");
       scanf("%i", &column);

       if(board[row][column] != -1)
       {
           while(board[row][column] != -1)
           {
            printf("Space already taken. Try again.\n");

            printf("Which row? ");
            scanf("%i", &row);

            printf("Which column? ");
            scanf("%i", &column);

            printf("\n");
           }
       }
       else
        board[row][column] = marker;

       drawBoard(board);

       turn++;
     }

And here is my boardFull method...

int boardFull(int board[3][3])
{
   int i, j;

   for(i = 0; i < 3; i++)
   {
       for(j = 0; j < 3; j++)
       {
           if(board[i][j] != -1)
               return 0;
       }
   }

   return -1;
}
Delfino
  • 967
  • 4
  • 21
  • 46
  • 1
    You ignore the return value of `scanf()` and you don't check the scanned value. Suppose `row >= 3`, problem! Suppose the user inputs `asdas`, again, problem!. – Iharob Al Asimi Feb 20 '15 at 21:50
  • 2
    Your `boardFull()` isn't checking whether the entire board is full, but rather if any square is full. As soon as it runs into any square which isn't -1, it returns. – Tom Hunt Feb 20 '15 at 21:52
  • i might suggest that instead of using two returns in your board full function you only use 1 bool value with an and operator, assume the board is true and do value = value&&board[i][j] != 1, but i don't think that is what is causing your problem – theDarse Feb 20 '15 at 21:54
  • 1
    @TomHunt It will return 0 if any of the 9 squares not `-1`, i.e. free. If all of them not `-1`, it will return `-1`, which is full. Which is correct in the program's logic. – Eugene Sh. Feb 20 '15 at 21:55
  • 1
    @EugeneSh. If -1 signifies a square being empty, then `boardFull()` returns 0 immediately after finding any _non_-empty square. If the condition was `board[i][j] == -1` it would be correct (assuming 0 means 'not full'). – Tom Hunt Feb 20 '15 at 22:13
  • @TomHunt You are right, my bad. I guess it is the answer. – Eugene Sh. Feb 20 '15 at 22:16

1 Answers1

1
  1. You should use boolean return value from boardFull function (see this post). It is more readable and understandable.
  2. Your boardFull function returns 0 (assume that this value stands for false) when it finds first non-empty cell. This is wrong. You should return 0 if any cell is empty.
  3. Your while loop condition looks good for me. But if you use boolean return value it looks nicer: while (!boardFull(...))
Community
  • 1
  • 1
Chemik
  • 1,459
  • 13
  • 23