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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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:
1
2
3
4
5
6
7
8
9
10
// 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..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//#include <stdbool.h>
/* 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.