#include #include #include #define FLUSH while (getchar() != '\n') /*Prototype Declaration*/ void drawBoard(void); void printBoard(char matrix[3][3],int drawCount,int win1Count,int win2Count,char PlayerA[15],char PlayerB[15]); void checkWin(char matrix[3][3],int*winner,char PlayerA[15],char PlayerB[15],int choice); void checkReturnLocation(int location,char matrix[3][3],int occupy,char Player[15],int PlayerFlag,int level); int checkBestMove(char matrix[3][3],int level); void resetValue(char matrix[3][3],int*winner,int*turn); int main(void) { /*Local Definition*/ char PlayerA[15]; char PlayerB[15]={'C','o','m','p','u','t','e','r'}; char matrix[3][3]= { {' ',' ',' '}, {' ',' ',' '}, {' ',' ',' '} }; char rematch; int turn=0; int winner=0; //0=draw game, 1=player1 won, 2=player2 won, 3=player3 won int checkmove=0; int location; int occupy; //occupy=0 -> the location chosen is not occupied int choice; //choice=1 -> player versus player choice=2 -> player versus computer int level; //level=1 ->Intermedia level=2 -> Expert int drawCount=0; //times of draw game int win1Count=0; //times of player1 win game int win2Count=0; //times of player2 or computer win game int PlayerFlag; //1=Player1, 2=Player2, 3=Player3 /*Request Game Mode 1. Player VS Player 2. Player VS Computer*/ do { printf("Please Choose\n"); printf("1. Player VS Player\n2. Player VS Computer\n"); scanf("%d",&choice); }while(choice != 1 && choice != 2); if(choice==1) { printf("Player 1 Please Enter Your Name:"); scanf("%s",&PlayerA); FLUSH; printf("Player 2 Please Enter Your Name:"); scanf("%s",&PlayerB); FLUSH; printf("\n"); printf("Hello %s and %s, welcome! Let's start the game.\n",PlayerA,PlayerB); printf("\n"); printf("\n"); drawBoard(); printf("\n"); } else { printf("Please Enter Your Name:"); scanf("%s",&PlayerA); FLUSH; /* If Is Player VS Computer Request Computer Level 1. Intermedia 2.Expert */ do { printf("Please Choose Computer Level:\n"); printf("1.Intermediate\n"); printf("2.Expert\n"); scanf("%d",&level); }while(level !=1 && level !=2); printf("\n"); printf("Hello %s , welcome! Let's start the game.\n",PlayerA); printf("\n"); printf("\n"); drawBoard(); printf("\n"); } do { /*START OF MAIN PART OF TIC TAE TOE*/ while(turn<9 && winner==0) { /*START of 1st Player Moving Script*/ while(turn<9 && winner==0 && checkmove==0) { PlayerFlag=1; checkReturnLocation(location,matrix,occupy,PlayerA,PlayerFlag,level); printBoard(matrix,drawCount,win1Count,win2Count,PlayerA,PlayerB); checkWin(matrix,&winner,PlayerA,PlayerB,choice); turn++; checkmove=1; } checkmove=0;//reset /*END of 1st Player Moving Script*/ /*START of 2nd Player Moving Script*/ while(turn<9 && winner==0 && checkmove==0 && choice ==1) { PlayerFlag=2; checkReturnLocation(location,matrix,occupy,PlayerB,PlayerFlag,level); printBoard(matrix,drawCount,win1Count,win2Count,PlayerA,PlayerB); checkWin(matrix,&winner,PlayerA,PlayerB,choice); turn++; checkmove=1; } checkmove=0;//reset /*END of 2nd Player Moving Script*/ /*START of Computer Choose Location*/ while(turn<9 && winner==0 && checkmove==0 && choice==2) { PlayerFlag=3; checkReturnLocation(location,matrix,occupy,PlayerB,PlayerFlag,level); printBoard(matrix,drawCount,win1Count,win2Count,PlayerA,PlayerB); checkWin(matrix,&winner,PlayerA,PlayerB,choice); turn++; checkmove=1; } checkmove=0;//reset /*ENDT of Computer Choose Location*/ }/*END OF MAIN PART OF TIC TAE TOE*/ switch(winner) { case 0:printf("No Winner, Draw Game.\n");drawCount++; break; case 1:if(choice==1){printf("%s Lost\n",PlayerB);} win1Count++; break; case 2:printf("%s Lost\n",PlayerA);win2Count++; break; case 3:printf("%s , You Are Defeated =)\n",PlayerA);win2Count++; break; } FLUSH; do { printf("Do you want to repeat this game?(Y/N)"); scanf("%c",&rematch); FLUSH; }while(rematch !='Y' && rematch !='y' && rematch !='n' && rematch !='N'); resetValue(matrix,&winner,&turn); }while(rematch=='Y' || rematch=='y'); return 0; }/*main*/ /* START OF 1st Function This Function Is Just Draw Out The Sample Of the Tic Tac Toe In The Beginning Of The Game*/ void drawBoard(void) { printf(" 1 || 2 || 3 \n"); printf("======================\n"); printf(" 4 || 5 || 6 \n"); printf("======================\n"); printf(" 7 || 8 || 9 \n"); printf("\n"); }//END OF 1st Function /* START OF 2st Function This Function Will Request Player For The Location, If A Location Is Occupied, It Will Re-Request Again, If A Location Is Avaible, It Will Return Location For The Purpose Of PrintBoard PlayerFlag Is Using In This Function, In Order To Differentiate, PlayerA(1), PlayerB(2) and Computer(3), */ void checkReturnLocation(int location,char matrix[3][3],int occupy,char Player[15],int PlayerFlag,int level) { do { if(PlayerFlag==1 || PlayerFlag==2) { if(occupy==1) { printf("Please Choose Another Place\n"); occupy=0; } do { printf("%s, your turn:",Player); scanf("%d",&location); }while(location<0 || location >9); } else if(PlayerFlag==3) { occupy=0; location=checkBestMove(matrix,level); } switch(location) { case 1: if(matrix[0][0]==' ' && PlayerFlag==1) matrix[0][0]='O'; else if(matrix[0][0]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[0][0]='X'; else occupy=1; break; case 2: if(matrix[0][1]==' ' && PlayerFlag==1) matrix[0][1]='O'; else if(matrix[0][1]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[0][1]='X'; else occupy=1; break; case 3: if(matrix[0][2]==' ' && PlayerFlag==1) matrix[0][2]='O'; else if(matrix[0][2]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[0][2]='X'; else occupy=1; break; case 4: if(matrix[1][0]==' ' && PlayerFlag==1) matrix[1][0]='O'; else if(matrix[1][0]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[1][0]='X'; else occupy=1; break; case 5: if(matrix[1][1]==' ' && PlayerFlag==1) matrix[1][1]='O'; else if(matrix[1][1]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[1][1]='X'; else occupy=1; break; case 6: if(matrix[1][2]==' ' && PlayerFlag==1) matrix[1][2]='O'; else if(matrix[1][2]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[1][2]='X'; else occupy=1; break; case 7: if(matrix[2][0]==' ' && PlayerFlag==1) matrix[2][0]='O'; else if(matrix[2][0]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[2][0]='X'; else occupy=1; break; case 8 :if(matrix[2][1]==' ' && PlayerFlag==1) matrix[2][1]='O'; else if(matrix[2][1]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[2][1]='X'; else occupy=1; break; case 9: if(matrix[2][2]==' ' && PlayerFlag==1) matrix[2][2]='O'; else if(matrix[2][2]==' ' && (PlayerFlag==2 || PlayerFlag==3)) matrix[2][2]='X'; else occupy=1; break; } }while(occupy==1); }//END OF 2nd Function /* START OF 3rd Function Everytime Player Choose His Location Or Computer Produce Its Location, It Will Print The Board Again To, Show The Movement Of Player And Computer */ void printBoard(char matrix[3][3],int drawCount,int win1Count,int win2Count,char PlayerA[15],char PlayerB[15]) { printf(" %c || %c || %c \t\t 1 || 2 || 3 \tScore Board\n",matrix[0][0],matrix[0][1],matrix[0][2]); printf("======================\t\t======================\n"); printf(" %c || %c || %c \t\t 4 || 5 || 6 \t%s Win : %d\n",matrix[1][0],matrix[1][1],matrix[1][2],PlayerA,win1Count); printf("======================\t\t======================\t%s Win : %d\n",PlayerB,win2Count); printf(" %c || %c || %c \t\t 7 || 8 || 9 \tDraw Game: %d\n",matrix[2][0],matrix[2][1],matrix[2][2],drawCount); printf("\n"); } /* START OF 4th Function This Function Is To Check The Win Condition, If There Is, It Will Return The Winner */ void checkWin(char matrix[3][3],int*winner,char PlayerA[15],char PlayerB[15],int choice) { /* Check Diagonal Win */ if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} /* Check Vertical Win */ if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} /* Check Horizontal Win */ if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Yeah! Congratulations to %s\n",PlayerA);*winner=1;} if(choice ==1) { /* Check Diagonal Win */ if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} /* Check Vertical Win */ if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} /* Check Horizontal Win */ if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {printf("Yeah! Congratulations to %s\n",PlayerB);*winner=2;} } else if(choice ==2) { /* Check Diagonal Win */ if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {*winner=3;} if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {*winner=3;} /* Check Vertical Win */ if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {*winner=3;} if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {*winner=3;} if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {*winner=3;} /* Check Horizontal Win */ if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {*winner=3;} if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {*winner=3;} if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {*winner=3;} } }//END OF 4th Function /* START OF 5th Function In The Player Vs Computer Mode, The Computer Will Choose The Best Location In Order To Win The Game Or Draw The Game If The Player Choose Expert Level, This Following Function Will Be Triggered In Advance Mode. */ int checkBestMove(char matrix[3][3],int level) { int location=99; int testLocation=99; int testLocation2=99; int testLocation3=99; int testLocation4=99; srand(time(NULL)); location=99; /*Start Detect Winning Step*/ if (matrix[1][0]=='X' && matrix[2][0]=='X' && matrix[0][0]==' ') {location=1;} else if (matrix[1][1]=='X' && matrix[2][2]=='X' && matrix[0][0]==' ') {location=1;} else if (matrix[0][1]=='X' && matrix[0][2]=='X' && matrix[0][1]==' ') {location=1;} else if (matrix[1][1]=='X' && matrix[2][1]=='X' && matrix[0][1]==' ') {location=2;} else if (matrix[0][0]=='X' && matrix[0][2]=='X' && matrix[0][1]==' ') {location=2;} else if (matrix[0][0]=='X' && matrix[0][1]=='X' && matrix[0][2]==' ') {location=3;} else if (matrix[1][2]=='X' && matrix[2][2]=='X' && matrix[0][2]==' ') {location=3;} else if (matrix[1][1]=='X' && matrix[2][0]=='X' && matrix[0][2]==' ') {location=3;} else if (matrix[1][1]=='X' && matrix[1][2]=='X' && matrix[1][0]==' ') {location=4;} else if (matrix[0][0]=='X' && matrix[2][0]=='X' && matrix[1][0]==' ') {location=4;} else if (matrix[0][0]=='X' && matrix[2][2]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='X' && matrix[1][2]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[2][0]=='X' && matrix[0][2]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[0][1]=='X' && matrix[2][1]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='X' && matrix[1][1]=='X' && matrix[1][2]==' ') {location=6;} else if (matrix[0][2]=='X' && matrix[2][2]=='X' && matrix[1][2]==' ') {location=6;} else if (matrix[0][0]=='X' && matrix[1][0]=='X' && matrix[2][0]==' ') {location=7;} else if (matrix[2][1]=='X' && matrix[2][2]=='X' && matrix[2][0]==' ') {location=7;} else if (matrix[0][2]=='X' && matrix[1][1]=='X' && matrix[2][0]==' ') {location=7;} else if (matrix[2][0]=='X' && matrix[2][2]=='X' && matrix[2][1]==' ') {location=8;} else if (matrix[0][1]=='X' && matrix[1][1]=='X' && matrix[2][1]==' ') {location=8;} else if (matrix[0][0]=='X' && matrix[1][1]=='X' && matrix[2][2]==' ') {location=9;} else if (matrix[0][2]=='X' && matrix[1][2]=='X' && matrix[2][2]==' ') {location=9;} else if (matrix[2][0]=='X' && matrix[2][1]=='X' && matrix[2][2]==' ') {location=9;} else if (matrix[1][0]=='X' && matrix[2][0]=='X' && matrix[0][0]==' ') {location=1;} else if (matrix[1][1]=='X' && matrix[2][2]=='X' && matrix[0][0]==' ') {location=1;} else if (matrix[0][1]=='X' && matrix[0][2]=='X' && matrix[0][1]==' ') {location=1;} else if (matrix[1][1]=='X' && matrix[2][1]=='X' && matrix[0][1]==' ') {location=2;} else if (matrix[0][0]=='X' && matrix[0][2]=='X' && matrix[0][1]==' ') {location=2;} else if (matrix[0][0]=='X' && matrix[0][1]=='X' && matrix[0][2]==' ') {location=3;} else if (matrix[1][2]=='X' && matrix[2][2]=='X' && matrix[0][2]==' ') {location=3;} else if (matrix[1][1]=='X' && matrix[2][0]=='X' && matrix[0][2]==' ') {location=3;} else if (matrix[1][1]=='X' && matrix[1][2]=='X' && matrix[1][0]==' ') {location=4;} else if (matrix[0][0]=='X' && matrix[2][0]=='X' && matrix[1][0]==' ') {location=4;} else if (matrix[0][0]=='X' && matrix[2][2]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='X' && matrix[1][2]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[2][0]=='X' && matrix[0][2]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[0][1]=='X' && matrix[2][1]=='X' && matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='X' && matrix[1][1]=='X' && matrix[1][2]==' ') {location=6;} else if (matrix[0][2]=='X' && matrix[2][2]=='X' && matrix[1][2]==' ') {location=6;} else if (matrix[0][0]=='X' && matrix[1][0]=='X' && matrix[2][0]==' ') {location=7;} else if (matrix[2][1]=='X' && matrix[2][2]=='X' && matrix[2][0]==' ') {location=7;} else if (matrix[0][2]=='X' && matrix[1][1]=='X' && matrix[2][0]==' ') {location=7;} else if (matrix[2][0]=='X' && matrix[2][2]=='X' && matrix[2][1]==' ') {location=8;} else if (matrix[0][1]=='X' && matrix[1][1]=='X' && matrix[2][1]==' ') {location=8;} else if (matrix[0][0]=='X' && matrix[1][1]=='X' && matrix[2][2]==' ') {location=9;} else if (matrix[0][2]=='X' && matrix[1][2]=='X' && matrix[2][2]==' ') {location=9;} else if (matrix[2][0]=='X' && matrix[2][1]=='X' && matrix[2][2]==' ') {location=9;} /*If No Possible Winning Step, Start Detect Blocking Step*/ else if (matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='O' && matrix[2][0]=='O' && matrix[0][0]==' ') {location=1;} else if (matrix[1][1]=='O' && matrix[2][2]=='O' && matrix[0][0]==' ') {location=1;} else if (matrix[0][1]=='O' && matrix[0][2]=='O' && matrix[0][1]==' ') {location=1;} else if (matrix[1][1]=='O' && matrix[2][1]=='O' && matrix[0][1]==' ') {location=2;} else if (matrix[0][0]=='O' && matrix[0][2]=='O' && matrix[0][1]==' ') {location=2;} else if (matrix[0][0]=='O' && matrix[0][1]=='O' && matrix[0][2]==' ') {location=3;} else if (matrix[1][2]=='O' && matrix[2][2]=='O' && matrix[0][2]==' ') {location=3;} else if (matrix[1][1]=='O' && matrix[2][0]=='O' && matrix[0][2]==' ') {location=3;} else if (matrix[1][1]=='O' && matrix[1][2]=='O' && matrix[1][0]==' ') {location=4;} else if (matrix[0][0]=='O' && matrix[2][0]=='O' && matrix[1][0]==' ') {location=4;} else if (matrix[0][0]=='O' && matrix[2][2]=='O' && matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='O' && matrix[1][2]=='O' && matrix[1][1]==' ') {location=5;} else if (matrix[2][0]=='O' && matrix[0][2]=='O' && matrix[1][1]==' ') {location=5;} else if (matrix[0][1]=='O' && matrix[2][1]=='O' && matrix[1][1]==' ') {location=5;} else if (matrix[1][0]=='O' && matrix[1][1]=='O' && matrix[1][2]==' ') {location=6;} else if (matrix[0][2]=='O' && matrix[2][2]=='O' && matrix[1][2]==' ') {location=6;} else if (matrix[0][0]=='O' && matrix[1][0]=='O' && matrix[2][0]==' ') {location=7;} else if (matrix[2][1]=='O' && matrix[2][2]=='O' && matrix[2][0]==' ') {location=7;} else if (matrix[0][2]=='O' && matrix[1][1]=='O' && matrix[2][0]==' ') {location=7;} else if (matrix[2][0]=='O' && matrix[2][2]=='O' && matrix[2][1]==' ') {location=8;} else if (matrix[0][1]=='O' && matrix[1][1]=='O' && matrix[2][1]==' ') {location=8;} else if (matrix[0][0]=='O' && matrix[1][1]=='O' && matrix[2][2]==' ') {location=9;} else if (matrix[0][2]=='O' && matrix[1][2]=='O' && matrix[2][2]==' ') {location=9;} else if (matrix[2][0]=='O' && matrix[2][1]=='O' && matrix[2][2]==' ') {location=9;} /*If The Player Choose Expert Mode*/ else if (level==2) { if ((matrix[0][0]=='O' && matrix[2][2]=='O' && matrix[1][1]=='X') || (matrix[0][2]=='O' && matrix[2][0]=='O' && matrix[1][1]=='X')&&(matrix[0][1] ==' ' ||matrix[1][0] ==' ' ||matrix[1][2] ==' ' ||matrix[2][1] ==' ' )) { testLocation=rand()%4; switch(testLocation) { case 0:location=2; break; case 1:location=4; break; case 2:location=6; break; case 3:location=8; break; } }/*To check double win position when player location at 1 and 9 or 3 and 7, computer will always put at 2 , 4 , 6 ,8*/ else if(matrix[1][1] =='O') { testLocation3=rand()%4; switch(testLocation3) { case 0:location=1; break; case 1:location=3; break; case 2:location=7; break; case 3:location=9; break; } }/*To check if player location is 5, computer always put at corner, 1,3,7,9(AFTER First Step)*/ /*To check if player location is 3&4, 7&2 , 1&6 , 9&2 , 1&8, 9&4, 3&8 , 6&7 */ else if(((matrix[0][2] =='O' && matrix[1][0] =='O') || (matrix[2][0] =='O' && matrix[0][1] =='O')) && matrix[0][0]==' ') {location=1;} else if(((matrix[0][0] =='O' && matrix[1][2] =='O') || (matrix[2][2] =='O' && matrix[0][1] =='O')) && matrix[0][2]==' ') {location=3;} else if(((matrix[0][0] =='O' && matrix[2][1] =='O') || (matrix[2][2] =='O' && matrix[1][0] =='O')) && matrix[2][1]==' ') {location=7;} else if(((matrix[0][2] =='O' && matrix[2][1] =='O') || (matrix[1][2] =='O' && matrix[2][0] =='O')) && matrix[2][2]==' ') {location=9;} /*To check if player location is 2&4, 2&6, 4&8, 6&8 */ else if((matrix[0][1] =='O' && matrix[1][0] =='O') && matrix[0][0]==' ') {location=1;} else if((matrix[0][1] =='O' && matrix[1][2] =='O') && matrix[0][2]==' ') {location=3;} else if((matrix[1][0] =='O' && matrix[2][1] =='O') && matrix[2][0]==' ') {location=7;} else if((matrix[1][2] =='O' && matrix[2][1] =='O') && matrix[2][2]==' ') {location=9;} /* If no others step */ else if(matrix[0][0]==' ') {location=1;} else if(matrix[0][1]==' ') {location=2;} else if(matrix[0][2]==' ') {location=3;} else if(matrix[1][0]==' ') {location=4;} else if(matrix[1][1]==' ') {location=5;} else if(matrix[1][2]==' ') {location=6;} else if(matrix[2][0]==' ') {location=7;} else if(matrix[2][1]==' ') {location=8;} else if(matrix[2][2]==' ') {location=9;} } /* If no others step --> random move*/ else if(location==99) { location = rand() % 9+1; } return location; }//END OF 5th Function //START OF 6th Function void resetValue(char matrix[3][3],int*winner,int*turn) { *winner=0; *turn=0; matrix[0][0]=' '; matrix[0][1]=' '; matrix[0][2]=' '; matrix[1][0]=' '; matrix[1][1]=' '; matrix[1][2]=' '; matrix[2][0]=' '; matrix[2][1]=' '; matrix[2][2]=' '; }//END OF 6th Function