- Home /
Tic Tac Toe--how to check for win
I am making a 3D tic tac toe, where a 3x3x3 cube holds blue and red placemarkers instead of Xs and Os. The lines can go up, down, straight, diagonal, etc. I have code that checks whether three placemarkers of the same color are in a row. Each block checks for blocks around itself. If they are the same color, a vector is made with these two blocks, creating a line. Then I use this line to raycast for another block to make a threesome. I have managed to get the script to recognize two in a row, but not three. I'm guessing it has to do with either the vectors or the casts. Here is my code(C#):
public bool checkRow(string t){
Collider[] col = Physics.OverlapSphere(transform.position, 0.9F);
foreach (Collider c in col){
if (c != collider && c.CompareTag (t)){ //check if neighbor and same color
Debug.Log ("Found neighbor");
Vector3 oneDir = c.transform.position - transform.position;
oneDir.Normalize();
Ray r = new Ray(c.transform.position, oneDir);
RaycastHit hit;
if (c.Raycast(r, out hit, 0.9F)){
Vector3 twoDir = hit.transform.position - transform.position;
twoDir.Normalize();
if (Vector3.Angle(oneDir, twoDir) < 5 && hit.transform.CompareTag(t)) //if the row is straight and the last one has the same color)
return true;
}
}
}
return false;
}
What could it be missing?
Answer by Eric5h5 · May 21, 2013 at 01:20 AM
It would be much simpler just to use an array, instead of trying to get raycasting and so on to work for this. Then you just check the relevant positions in the array. You'd only use GameObjects to reflect the state of the array, so you can dispense with colliders, physics, etc.
^^ Exactly! Seems like you're over complicating things with the ray casts :p
You could even just have an array of int's to represent the board state:
000
000
000
0 would represent an empty square, 1 would be a square occupied by player one, and 2 by player 2.
102
210
001
The algorithm to deter$$anonymous$$e a win isn't too difficult. The only places you can win with a diagonal are from the corners, so if there was a piece there you'd check for a diagonal, and I'll let you figure out he rest :-)
Yep, actually I had an array of ints in $$anonymous$$d, thanks for clarifying that.
Let me see if I can clarify: this is not a 3x3 board like normal tic tac toe. This is a 3x3x3 cube; you can make a line on any of the five faces--the top and the four sides--of the cube, but you can also go through the cube to make a line. The array would have to be three-dimensional, which would be at least 50 possible wins. I did raycast because it was quicker than checking each case.
That doesn't really change the answer; an array would be simpler and faster regardless, nor would it involve any hard-coding.
Answer by Mentalist4006 · May 22, 2013 at 12:14 AM
Alright, I was finally inspired with the answer. The problem was that the raycast hit triggers that I thought would not be hit (but obviously did). So I used a layerMask
to avoid this (uses Physics.Raycast()
now) and increased the distance. Now it can get all the lines without having to have 50 cases for each possible line. Sorry about the confusion.
But, Eric was right; I made a three dimensional array, and had two for loops to check all the possibilities. It was way faster than my Raycast check. Thanks, Eric.