need to create a Tic-Tac-Toe Program in plain C, not C++ or C#. It must allow fo
ID: 3558661 • Letter: N
Question
need to create a Tic-Tac-Toe Program in plain C, not C++ or C#. It must allow for the the following :1. Two players. They enter their names, which are saved. 2. One uses X, one uses O. (option to allow players to select their own marks) 3. Display the playing board (3x3 array) 4. Players take alternate turns placing their mark. Display whose turn it is. 5. Cannot take another playerâs square. 6. Use a function to check for a winner. 7. Display winner when you have determined that someone has won. 8. Decide how to handle draws (tie). 9. Keep track of games won by each player. 10. Ask to play again. If yes, play again, changing who starts. 11. If no, display won/lost statistics, pause, then exit.
Explanation / Answer
#include <stdio.h>
int main(void)
{
int i = 0;
int player = 0;
int go = 0;
int row = 0;
int column = 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 the number of the square "
"where you want to place your %c: ", player,(player==1)?'X':'O');
scanf("%d", &go);
row = --go/3;
column = go%3;
}
while(go<0 || go>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 ++)
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(" Draw ");
else
printf(" player %d, YOU WON! ", winner);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.