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