Verification who wins in tic tac toe game.

yo everyone. Now im doing this tic tac toe practice for my school... so i have a problem with the verification, the program is working but my teacher said "I dont want you to define the exactly position of the token, find a way -smarter- to define who wins.. " and actually im feeling really stupid, cuz i cant figure out how to do it...

1
2
3
4
5
6
7
8
9
10
matriz[i][j]='X';
for(y=0; y<3; y++){
	if(matriz[0][y]=='X'&&matriz[1][y]=='X'&&matriz[2][y]=='X' ||
		matriz[y][0]=='X'&&matriz[y][1]=='X'&&matriz[y][2]=='X'){
			p1=1;
		}
}
if(matriz[0][0]=='X'&&matriz[1][1]=='X'&&matriz[2][2]=='X' ||
	matriz[2][0]=='X'&&matriz[1][1]=='X'&&matriz[0][2]=='X'){
		p1=1;
You are effectivly storing every single solution in your code... For something as simple as tic-tac-toe, works, but expand that to something like say, connect four, and suddenly that becomes extremely inefficient. Instead, look for an algorithm to identify three 'X's or 'O's in a row.

...Try nesting two for loops to scan the entire board. Then, every time you come across an 'X' (or 'O'), check the adjacent positions. If any match, check the next in line....
the thing is... that i dont have an idea of the algorithm... im a noob...
Whether you are, as you say, a 'noob' or not, algorithms are unrelated. The idea is to think of how you would do it, work it out step by step on a piece of paper, and then turn it into code. We won't do it for you - try and do it yourself.
In mathematics and computer science, an algorithm (Listeni/ˈælɡərɪðəm/ al-gə-ri-dhəm) is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning. source: http://en.wikipedia.org/wiki/Algorithm


As NT3 said, work it out on paper, then translate it to code.
Last edited on
im asking for help, because that code was the only i could imagine to make it work... I tried some other methods and didnt work, for real... i tried...
The code you posted above only checks is X is the winner. Obviously you have to do the same thing for O. The point I'm making here is that you should be doing this in a generalized way.
 
bool check_for_winner (char p)  // where p is 'X' or 'O' 


Tic-tac-toe is such a simple game that a human can look at the board and instantly know if there is a winnder and if so who it it. But for a computer, you have to break it down into the same steps the human mind does instantly. The code you have above is correct for checking X, but it's not using an algorithm as your teacher is asking. An algorithm is simply a series of steps. What are the steps the human mind goes through to determine if there is a winner?
1) The mind checks each row for a winner
2) The mind checks each column for a winner
3) The mind checks each diagonal for a winner.
You code does this, but not in an algorithmic way. Can you envision how do do this using loops and functions?

thanks @AbstractionAnon

what about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool Ganador(){
//rows
	for(int i = 0; i<3;i++){
        if( (matriz[i][0] == matriz[i][1]) && matriz[i][2] == matriz[i][0] && matriz[i][0] != '_'){
            cout<<"Gana!" <<winner <<endl<<"\n";
            return true;
			}
		}
	//columns
	for(int i = 0; i<3;i++){
        if( (matriz[0][i] == matriz[1][i]) && matriz[2][i] == matriz[0][i] && matriz[0][i] != '_'){
        cout<<"Gana!"<<winner<<endl<<"\n";
        return true;
        }
    }
}


it is working... but not sure if this is what my teacher wants... either if this time, im using "an algorithm way"

thanks in advance for your help =)
Last edited on
You're getting there. Try something like...
1
2
3
4
5
6
7
8
9
10
11
for (int x = 0; x<3; x++) {
	for (int y = 0; y<3; y++) {
		if (matriz[x][y] == '_') {
			if (x-2 >= 0 && matriz[x-1][y] == '_' && matriz[x-2][y] == '_') return true; //Check row left from coordinate x,y
			... //Check column up from coordinate x,y
			...
			/*x-2 >= 0: ensures coordinates are within bounds of array*/
			/*Check... row left, column up, row right, column down, diagonal up left, diagonal up right, diagonal down left, diagonal down right*/
		}
	}
}
Last edited on
That's the idea. You're missing the checks for diagonals though.

Also, that function only checks that there is a winner. It doesn't tell you who the winner is. In my previous post, I suggested you pass in the player (p) that you're checking to see if they're the winner.

You're also missing a return false; after line 15.

Not sure where the variable winner comes from in lines 5 and 12.
Thanks for your help =) I already made it work, with the winner and stuff... I'm gonna try your code rssair, looks better than mine =P

Also, I was thinking in this code to validate rows...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char jugadorEncontrado = tablero[0][0];
     for(int i =0 ; i < 3; i++){
             for(int j = 0 ; j < 3 ; j++){
                     if(tablero[i][j] == '_'){
                          break; break;
                     }
                     if(jugadorEncontrado != tablero[i][j]){
                          break; break;
                                        
                     }
                     jugadorEncontrado = tablero[i][j];
                     if(j == 2){
                          printf("Ha ganado %c", jugadorEncontrado);
                          return true;                          
                     }
              }             
             
     }


What's wrong? Why this only verify the row 0?

Btw... Anybody knows if I can access this forum through tapatalk for Android?

Thanks again.
Last edited on
Topic archived. No new replies allowed.