### Help with Tic tac toe winner Search

I don't know it is working or not in proper way but Following is my code for winner search function: any help to fix it appreciate.
 ``12345678910111213141516171819202122232425262728`` ``````char Checker(){ int i; /* Row Checker */ for(i=0;i<3;i++) if(xarray[i][0] == xarray[i][1] && xarray[i][1] == xarray[i][2]){ return xarray[i][0]; } /* Column Checker */ for (i=0;i<3;i++){ if(xarray[0][i] == xarray[1][i] && xarray[1][i] == xarray[2][i]){ return xarray[0][i]; } } /* digonal Checker */ if(xarray[0][0] == xarray[1][1] && xarray[1][1] == xarray[2][2]){ return xarray[0][0]; } else if(xarray[0][2] == xarray[1][1] && xarray[1][1] == xarray[2][0]){ return xarray[0][2]; } else{ return 'n'; } }``````
Last edited on
I'll give you a scenario:
 ``` | | ----- X|X|X ----- O|O|```

You'll get returned whatever xarray[0][0] is before someone makes a move there since the first row is all equal to itself. You should pass a character to the function to check for after each move.

For example:
 ``12345678910`` ``````// Player 1 moves if (Checker(player_1_piece)) // Player 1 won ... bool Checker(char player) { for(i=0;i<3;i++) if(xarray[i][0] == player && xarray[i][0] == xarray[i][1] && xarray[i][1] == xarray[i][2]){ return true; } // ... ``````

The only thing that you need to add is the check to make sure that the row that is equal (or column or diagonal) has the players shape. If it does, it should return true, otherwise, your else statement should return false. I assume you have a player's turn variable used to check to see who's turn it is as well so that once someone wins, you can exit the loop and display this is the winner. If 9 moves have gone and no one wins, then it's a draw.

I'll leave the rest for you to implement though.
Would

`if(xarray[i][0] == player && xarray[i][1] == player && xarray[i][2] == player])`

be that little bit clearer (more literal)?

Andy

Last edited on
Here is my Whole Program it compiles and run but not the way it supposed to, and it is a tic-tac-toe that runs in between two computers randomly..
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148`` ``````#include #include #include #include //#include /* A Global variable to use everywhere */ char xarray[3][3]; /* Funtion Prototypes */ int Number_Generator(); void Print_Board(); void initialize_Board(); void Move_X(); void Move_O(); char GetWinner(); //void Full_Board(); //char Checker_X(); //char Checker_O(); int main(){ int count=0; //temp variable for testing char Player1[10],Player2[10],Value=NULL,User; bool test = false; srand(time(NULL)); initialize_Board(); do{ Move_X(); Print_Board(); Value = GetWinner(); if(Value == 'X'){ printf("X Won !!\n"); test = true; break; } else{ printf("Press Enter to Continue:"); scanf("%c",&User); } if(User == '\n'){ Move_O(); Print_Board(); } Value = GetWinner(); if(Value == 'O'){ printf("O Won !!\n"); test = true; break; } else{ printf("Press Enter to Continue:"); scanf("%c",&User); } }while( User == '\n' && test == false); return 1; } /* Function to Generate Locations on Board */ int Number_Generator(){ int rNumber; rNumber=rand()%3; return rNumber; } /* Function to Print Board */ void Print_Board(){ int i; printf("\n"); for(i=0;i<3;i++){ printf("%c | %c | %c \n",xarray[i][1],xarray[i][2],xarray[i][3]); if(i<2) printf("--+---+--\n"); } printf("\n"); } /* Function to initialize an Empty Board */ void initialize_Board(){ int i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) xarray[i][j]=NULL; } /* Function to Get 'X' on the board */ void Move_X(){ int x,y; x=Number_Generator(); y=Number_Generator(); if (xarray[x][y] == '\0'){ xarray[x][y]= 'X'; } else{ Move_X(); } } /* Function to Get 'O' on the board */ void Move_O(){ int x,y; x=Number_Generator(); y=Number_Generator(); if (xarray[x][y]=='\0'){ xarray[x][y]= 'O'; } else{ Move_O(); } } /* return winner ('X' or 'O', or ' ' if no winner) */ char GetWinner(){ // renamed... /* Row/Column Checker */ for(int i=0;i<3;i++){ if((xarray[i][0] == xarray[i][1]) && (xarray[i][1] == xarray[i][2]) && xarray[i][0] != NULL){ return xarray[i][0]; } if((xarray[0][i] == xarray[1][i]) && (xarray[1][i] == xarray[2][i]) && xarray[0][i] != NULL){ return xarray[0][i]; } } /* Diagonal Checkers */ if((xarray[0][0] == xarray[1][1]) && (xarray[1][1] == xarray[2][2]) && xarray[0][0] != NULL){ return xarray[0][0]; } if((xarray[0][2] == xarray[1][1]) && (xarray[1][1] == xarray[2][0]) && xarray[0][2] != NULL){ return xarray[0][2]; } //return ' '; // assumes ' ' is used for empty cells } ``````
I accidentally exited the page after I wrote up a good explanation for you, but I suggest changing NULL to ' ' since I don't believe that NULL is valid for type char.

I'd also suggest consolidating your move functions into one function and pass the character to print to the board.

Also, there really is no need for a random number function when the entire thing could just be replaced by `rand() % 3` so you would write `x = rand() % 3;` and it'll do the exact same thing.

One last thing to point out is that I suggest against using any global variables, but I noticed you don't have any functions that accept parameters. I'm guessing you're just starting with C++, or C, it's unclear which language you're preferring. What is the purpose of player1 and player2 in main? Main should also always return 0 unless there was an issue. If you'd like more assistance, private message me and I'll see what I can do for you.
Topic archived. No new replies allowed.