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...
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....
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.
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?
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] == '_') returntrue; //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*/
}
}
}
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 returnfalse; after line 15.
Not sure where the variable winner comes from in lines 5 and 12.