I am writing this program to make a tic tac toe game but the do loop wont run, a
ID: 3763373 • Letter: I
Question
I am writing this program to make a tic tac toe game but the do loop wont run, any suggestions? Its in C programming.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int player=0;
int winner=0;
int choice =0;
int row=0;
int column=0;
int line=0;
char board [3][3]=
{
{'1', '2', '3'},
{'4','5','6'},
{'7', '8', '9'}
};
for (int i=0; i<9 && winner==0; i=i+1)
{
printf (" ");
printf ("%c |%c |%c ", board [0][0], board [0][1], board [0][2]);
printf ("--+--+-- ");
printf ("%c |%c |%c ", board [1][0], board [1][1], board [1][2]);
printf ("--+--+-- ");
printf ("%c |%c |%c ", board [2][0], board [2][1], board [2][2]);
player = i%2 +1;
do
{
printf (" Player %d, please enter the number of the square where you want to place your %c:", player, (player==1)? 'X': 'O');
scanf ("%d", &choice);
row= --choice/3;
column = choice%3;
} while (choice<0 || choice >9 || board [row][column]>'9');
board [row][column]= (player ==1) ? 'X': 'O';
if ((board [0][0]==board [1][1] && board [0][0]==board [2][2]) ||
(board [0][2]==board [1][1] && board [0][2]==board [2][0]))
winner=player;
else
for (line =0; line <=2; line=line +1)
if ((board [line][0]==board[line][1]&&
board [line][0]==board [line][2])||
(board [0][line]==board [1][line] &&
board[0][line]==board[2][line]))
winner= player;
}
printf (" ");
printf ("%c |%c |%c ", board [0][0], board [0][1], board [0][2]);
printf ("--+--+-- ");
printf ("%c |%c |%c ", board [1][0], board [1][1], board [1][2]);
printf ("--+--+-- ");
printf ("%c |%c |%c ", board [2][0], board [2][1], board [2][2]);
if (winner==0)
printf (" How boring, its a tie ");
else
printf (" Congratulations, player %d, YOU ARE A WINNER! ", winner);
return 0;
}
Explanation / Answer
Please make following two lines in do loop as a single line:
printf (" Player %d, please enter the number of the square where you want to place your %c:",
player, (player==1)? 'X': 'O');
Otherwise the program is working fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.