- Home /
Scoring system
I made a simple tic tac toe game out of a 3x3 grid of squares. The squares have a script that changes the texture on mouse down depending on whos turn it is. Each successful turn it changes to the other player.
I currently have two scripts one attached to an empty which basicly chooses a random player to start the game and manages some events. The other attached to the grid squares to change the texture.
What I'm having trouble with is a way to detrimine if a player has won or if its a tie. I was thinking of assigning a vaule to a naught of -1 or cross +1 and adding the values of each row, column and diagonally but im having a hard time putting it into code.
Im probably overthinking it but some input would be appreciated.
It's not a full answer, but what I found to be the best logic to quickly checking for a winner without a huge mess of ifs or loops and such.
Try something like this:
//initialize each element of the array Box to 0
//set up a player number system to iterate between 1 and 2
//take turns, assigning the current player number to an element in an array
int winner = Box[0] (Box[0] == Box[1] && Box[0] == Box[2]) + Box[0] (Box[0] == Box[3] && Box[0] == Box[6]) + Box[0] (Box[0] == Box[4] && Box[0] == Box[8]) + Box[1] (Box[1] == Box[4] && Box[1] == Box[7]) + Box[2] (Box[2] == Box[5] && Box[2] == Box[8]) + Box[2] (Box[2] == Box[4] && Box[2] == Box[6]) + Box[3] (Box[3] == Box[4] && Box[3] == Box[5]) + Box[6] (Box[6] == Box[7] && Box[6] == Box[8]);
if (winner == 1) { //player1 wins, game ends or goes to play again and such }
if (winner == 2) { //player2 wins, game ends or goes to play again and such }
The block looks odd, but what it does is evaluate to either 0, 1, 2 depending on the player. it's a bit mathy but works out.
Answer by Berenger · Jun 22, 2012 at 03:02 AM
I recently did a tic tac toe and to check if there was a victory, I did like this :
First, store the nine positions in an array (top left, top center, etc)
Second, there is 8 ways to win (3 rows, 3 columns, 2 diag). You can either create a class that hold 3 ints and then create an array of 8 of those, or an array of 8 * 3 int. Fill it with the correct indices.
Each time someone has played, check all those 8 possibilities to see if they contains 3 times the same thing.
I suggest you use an enum to know the status of each squares (Circle, Cross, Empty for instance).
Answer by Jessiechen · Jun 22, 2012 at 10:23 AM
I'd like to know how to ask questions? On this website, thank you
on top, there is a link as "Ask a Question". Click on that and then ask your question with proper title and tag. Thats it..