#include #include //Definitions #define BOARDSIZE 3 #define TRUE 1 #define FALSE 0 //Global declerations int Turn = 1; //Counts X players or O players turn //Function Prototype Declerations void Play(); void printBoard(); int isThereaWin(); int horizontalSearch(); int verticalSearch(); int diagonalSearch(); void resetBoard(); int Input(); int Randommer(); //Returns a random number between 0-2 for the computers random play int Randommer() { int r = rand() % 3; return r; } //Searchs the board for horizontal win conditions //returns 1 if there is a win int horizontalSearch(char bord[3][3]) { int Xcounter = 0; int Ocounter = 0; int win = FALSE; int i,j; for (i=0; i<3; i++) { Xcounter = 0; Ocounter = 0; for (j=0; j<3; j++) { if (bord[i][j] == 'X') Xcounter++; if (bord[i][j] == 'O') Ocounter++; } if (Xcounter == 3 | Ocounter == 3) win = TRUE; } return win; } //Searchs the board for vertical win conditions //returns 1 if there is a win int verticalSearch(char bord[3][3]) { int Xcounter = 0; int Ocounter = 0; int win = FALSE; int i,j; for (j=0; j<3; j++) { Xcounter = 0; Ocounter = 0; for (i=0; i<3; i++) { if (bord[i][j] == 'X') Xcounter++; if (bord[i][j] == 'O') Ocounter++; } if (Xcounter == 3 | Ocounter == 3) win = TRUE; } return win; } //Searchs the board for diagonal win conditions //returns 1 if there is a win int diagonalSearch (char bord[3][3]) { int Xcounter = 0; int Ocounter = 0; int win = FALSE; int i=0; int j=0; while (i<3 & j<3) { if (bord[i][j] == 'X') Xcounter++; if (bord[i][j] == 'O') Ocounter++; i++; j++; } if (Xcounter == 3 | Ocounter == 3) win = TRUE; else { i=0; j=2; Xcounter=0; Ocounter=0; while (i<3 & j>=0) { if (bord[i][j] == 'X') Xcounter++; if (bord[i][j] == 'O') Ocounter++; i++; j--; } if (Xcounter == 3 | Ocounter == 3) win = TRUE; } return win; } //Returns 1 if there is a win condition on the board int isThereaWin(char bord[3][3]) { int win = FALSE; int checkhorizon = FALSE; int checkvertic = FALSE; int checkdiagonal = FALSE; checkhorizon = horizontalSearch(bord); checkvertic = verticalSearch(bord); checkdiagonal = diagonalSearch(bord); if (checkhorizon == TRUE){ win = TRUE; printf("********horizontal-win*********\n"); printBoard(bord); } else if (checkvertic == TRUE){ win = TRUE; printf("********vertical-win**********\n"); printBoard(bord); } else if (checkdiagonal == TRUE){ win = TRUE; printf("*********diagonal-win*********\n"); printBoard(bord); } else{} return win; } //Game loop void Play (int NumofPlayers, char bord[3][3]) { int input = TRUE; int checkwin = 0; int Row; int Col; int Colnum; printf("%d player game...\n",NumofPlayers); if (NumofPlayers == 1) { checkwin = isThereaWin(bord); while (checkwin == FALSE & input == TRUE) { printBoard(bord); if (Turn >= 10){ resetBoard(); Turn = 1; } else if (Turn % 2 == 1){ printf("X players turn\n"); input = Input(bord); } else if (Turn %2 == 0){ printf("Computers turn\n"); Row = Randommer(); Col = Randommer(); while (bord[Row][Col] == 'X' | bord[Row][Col] == 'O'){ Row = Randommer(); Col = Randommer(); } bord[Row][Col] = 'O'; Turn++; } else {} checkwin = isThereaWin(bord); } } else { checkwin = isThereaWin(bord); while (checkwin == FALSE & input == TRUE) { printBoard(bord); if (Turn >= 10){ resetBoard(); Turn = 1; } else if (Turn % 2 == 1) printf("X players turn\n"); else if (Turn %2 == 0) printf("O players turn\n"); else {} input = Input(bord); checkwin = isThereaWin(bord); } } } //This function asks for an input from the user until //a correct input is put int Input (char bord[3][3]) { int input; //right or wrong input int Rownum=5; //initialize row input int Colnum=5; //initialize column input while (TRUE) { char RowCol[50]; //row and column input string int i=0; printf("(Q)=Quit,(R)=Reset... choose Row-Column: "); scanf("%s",RowCol); for (i=0; i < strlen(RowCol); i++){ RowCol[i] = tolower(RowCol[i]); } if (strcmp(RowCol,"q") == 0 | strcmp(RowCol, "Q") == 0){ input = 0; printf("GOOD BYE\n"); break; } else if (strcmp(RowCol,"r")==0 | strcmp(RowCol,"R")==0){ input = 1; resetBoard(bord); printf("Board is reset\n"); break; } else { //if the index of 'a' < string length, then there is an 'a' if (strcspn(RowCol,"a") < strlen(RowCol)){ Rownum = 0; input = 1; } //if the index of 'b' < string length, there is a b else if (strcspn(RowCol,"b") < strlen(RowCol)){ Rownum = 1; input = 1; } //........same thing for c else if (strcspn(RowCol,"c") < strlen(RowCol)){ Rownum = 2; input = 1; } //If there is no a,b or c in the input, the input is wrong else { printf("wrong input\n"); } //Check for column numbers 1,2 or 3 if (strcspn(RowCol,"1") < strlen(RowCol)){ Colnum = 0; input = 1; break; } else if (strcspn(RowCol,"2") < strlen(RowCol)){ Colnum = 1; input = 1; break; } else if (strcspn(RowCol,"3") < strlen(RowCol)){ Colnum = 2; input = 1; break; } else { printf("wrong input\n"); } } } //If the given input is full, print wrong input if (bord[Rownum][Colnum] == 'X' | bord[Rownum][Colnum] == 'O'){ printf("wrong input\n"); } //If it is an odd number, X's turn //Else O's turn else { if (Turn % 2 == 1) bord[Rownum][Colnum] = 'X'; else bord[Rownum][Colnum] = 'O'; Turn++; } return input; } //This function prints the current given board on the console void printBoard (char bord[3][3]) { int k,l; printf("\n"); printf(" 1 2 3\n"); printf(" -------\n"); for (k=0; k<3; k++) { if (k == 0) printf("A "); else if (k == 1) printf("B "); else printf("C "); for (l=0; l<3; l++) { printf("|%c",bord[k][l]); } printf("| \n"); printf(" -------\n"); } printf("\n"); } //Main Starts the game int main () { srand(time(0)); //random number seeder synchronized with time char NofPlayers[10]; //char array for inputting Number of players int NPlayers = 0; //Number of Players char board[BOARDSIZE][BOARDSIZE] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; //Tic-Tac-Toe board int Turn = 1; //Turn count for X's and O's printf(" WELCOME TO TIC-TAC-TOE!\n"); printf("\n"); printf("[1] player or [2] player game: "); while (NPlayers != 1 & NPlayers != 2){ scanf("%s",NofPlayers); if (strcmp(NofPlayers,"1") == 0){ NPlayers = 1; break; } else if (strcmp(NofPlayers,"2") == 0) { NPlayers = 2; break; } else printf("wrong input!\n"); printf("[1] player or [2] player game: "); } Play(NPlayers, board); system("pause"); return 0; } //This function resets the given board to an empty board void resetBoard(char bord[3][3]){ int i,j; for (i=0; i<3; i++) for (j=0; j<3; j++) bord[i][j]='\0'; }