I have to write a tic tac toe program between two players where they have to ent
ID: 3760506 • Letter: I
Question
I have to write a tic tac toe program between two players where they have to enter the row and column number so they can enter an X and the second player has to enter row and column number to print an O. It also needs to determine when a player has won or tie has occured. If a tie occurs the program should say there was a tie and end. I have this code that I have built so far and I am using Eclipse with C programing but the program wont run, what is the mistake? What do I add to make it do the rest?
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main()
{
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);
return 0;
}