#include void info() { printf("TIC-TAC-TOE\nWritten by John Doe\n"); printf("Your Symbol: 'x'\nComputer Symbol: 'o'"); } void init(char cell[]) { cell[0] = ' '; cell[1] = ' '; cell[2] = ' '; cell[3] = ' '; cell[4] = ' '; cell[5] = ' '; cell[6] = ' '; cell[7] = ' '; cell[8] = ' '; } void gameBoard (char cell[]) { printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]); printf("---|---|---\n"); printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]); printf("---|---|---\n"); printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]); } void userMove (char cell[]) { int you; do { do { printf("Your Move (0-8):\n"); scanf("%i", &you); if (you>=9) printf("Invalid Move\n"); } while (you>=9); if (cell[you] != ' ') printf("Position taken\n"); } while (cell[you] != ' '); cell[you]='x'; gameBoard(cell); } void computerMove (char cell[]) { int i=0; do { if(cell[i]==' ') { cell[i]='o'; break; } ++i; }while(i<=8); printf("Computer Plays:%i\n", i); gameBoard(cell); } char checkWinner(char cell[]) { char x; int i; if((cell[0]=='x' && cell[1]=='x' && cell[2]=='x') || (cell[0]=='x' && cell[3]=='x' && cell[6]=='x') || (cell[0]=='x' && cell[4]=='x' && cell[8]=='x') || (cell[1]=='x' && cell[4]=='x' && cell[7]=='x') || (cell[2]=='x' && cell[5]=='x' && cell[8]=='x') || (cell[2]=='x' && cell[4]=='x' && cell[6]=='x') || (cell[3]=='x' && cell[4]=='x' && cell[5]=='x') || (cell[6]=='x' && cell[7]=='x' && cell[8]=='x')) x = 'x'; else if ((cell[0]=='o' && cell[1]=='o' && cell[2]=='o') || (cell[0]=='o' && cell[3]=='o' && cell[6]=='o') || (cell[0]=='o' && cell[4]=='o' && cell[8]=='o') || (cell[1]=='o' && cell[4]=='o' && cell[7]=='o') || (cell[2]=='o' && cell[5]=='o' && cell[8]=='o') || (cell[2]=='o' && cell[4]=='o' && cell[6]=='o') || (cell[3]=='o' && cell[4]=='o' && cell[5]=='o') || (cell[6]=='o' && cell[7]=='o' && cell[8]=='o')) x = 'o'; else { x = 'd'; for (i=0; i<9; i++) if (cell[i] == ' ') { x = ' '; break; } } return x; } int main() { char cell[9], cW; info(cell); init(cell); gameBoard(cell); do { userMove(cell); cW = checkWinner(cell); if (cW != ' ') continue; computerMove(cell); cW = checkWinner(cell); if (cW != ' ') continue; } while (cW == ' '); if(cW == 'o') printf("Computer Wins! Better Luck Next Time\n"); else if(cW == 'x') printf("You Win!\n"); else if(cW == 'd') printf("Game Ends in a Tie!\n"); return 0; }