can you please explain the return int a little more???? thanks
Code:#include <stdio.h> #include <stdlib.h> void info() { printf("TIC-TAC-TOE\nWritten by MEEEE\n"); printf("Your Symbol 'x'"); printf("Computer Symbol 'o'"); } /*initialize board*/ void init(char cell[]) { int i; for (i=0; i<9; i++) cell[i] = ' '; } /*print gameboard */ 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]); } /*player moves*/ void userMove(char cell[]) { int i = 0; int cont = 1; do{ printf("Your Move (0-8): "); scanf("%d", &i); if((cell[i] != ' ') || (i < 0 || i > 8)) { printf("Invalid Input\n"); //invalid input } else { cell[i] = 'x'; cont--; //set while loop to false, break } } while(cont); } /*computer moves*/ void computerMove(char cell[]) { int i = 0; int cont = 1; do { if(cell[i] == ' ') { cell[i] = 'o'; cont--; } else { i++; } }while(cont); } char checkWinner(char cell[]) { int i; // Diagonals if (cell[4]!=0 && (cell[4]==cell[0] && cell[4]==cell[8] || cell[4]==cell[2] && cell[4]==cell[6])) return cell[4]; // Horizontals for (i=0; i<9; i+=3) if (cell[i]>' ' && cell[i]==cell[i+1] && cell[i]==cell[i+2]) return cell[i]; // Verticals for (i=0; i<3; i++) if (cell[i]>' ' && cell[i]==cell[i+3] && cell[i]==cell[i+6]) return cell[i]; // No match return ' '; } int main() { char cell[9]; int cont = 1; int ccode = 0; info(); init(cell); gameBoard(cell); while(cont) { userMove(cell); gameBoard(cell); //check if user wins //if user wins, print user won //and cont-- to exit loop computerMove(cell); gameBoard(cell); checkWinner(cell); //check if computer wins, print computer won //and cont-- to exit loop //if nobody won get another user input } return 0; }



1Likes
LinkBack URL
About LinkBacks


