Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a Tic Tac Toe game using C code. Be able to play the game easily by allo

ID: 3871294 • Letter: D

Question

Develop a Tic Tac Toe game using C code. Be able to play the game easily by allowing a human player to select a location for O. The winning behavior is the goal. To make it easy your program will always be X and go first. A human will always play O and assume is not a good player and will lose from time to time. Your game must never lose as X. It must not miss a winning move. It must recognize a win or draw status. Develop a Tic Tac Toe game using C code. Be able to play the game easily by allowing a human player to select a location for O. The winning behavior is the goal. To make it easy your program will always be X and go first. A human will always play O and assume is not a good player and will lose from time to time. Your game must never lose as X. It must not miss a winning move. It must recognize a win or draw status.

Explanation / Answer

#include <stdio.h>
#include<conio.h>
void main()
{
int i = 0;
int player = 0;
int g = 0;
int r = 0;
int c = 0;
int line = 0;
int winner = 0;
char board[3][3] = { {'1','2','3'} ,{'4','5','6'} ,{'7','8','9'}} ;
for( i = 0; i<9 && winner==0; i++)
{
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 number of the square " "where you want to place your %c: ",player,(player== 1)?'X':'0');
scanf("%d", &g);
scanf("%d", &g);
r = --g/3;
c = g%3;
}
while(g<0 || g>9 || board[r][c]>'9');
board[r][c] = (player == 1) ? 'X' : '0';
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 ++)
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 I %c ", board[2][0], board[2][1],board[2][2]);
if(winner == 0)
printf(" it is a draw ");
else
printf(" Congratulations, player %d, YOU WIN! ", winner);
getch();
}