Created
December 4, 2013 22:16
-
-
Save cubicleDowns/7796602 to your computer and use it in GitHub Desktop.
Check for a tic-tac-toe winner with rays. This is referenced on my blog, blog.tempt3d.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// loop through all the rays and look to see if all of their collisions objects show the same values. | |
// | |
// essentially, a ray will intersect all faces in one particular direction. 3 cubes = 6 faces = 6 intersections | |
// if all of the intersections show a 'selection' has take place, it'll check the selection types (ttt property) | |
function checkForTTT(){ | |
var i,j, | |
collisions, | |
ticUser1, | |
ticUser2; | |
for(i = 0; i < rays.length; i++){ | |
collisions = rays[i].intersectObjects(_demo.collisions); | |
ticUser1 = 0; | |
ticUser2 = 0; | |
for(j = 0; j < collisions.length; j++){ | |
if(collisions[j].object.ttt === 'user1'){ | |
ticUser1++; | |
} else if (collisions[j].object.ttt === 'user2'){ | |
ticUser2++; | |
} | |
} | |
if(ticUser1 === collisions.length){ | |
console.log("Tic Tac Toe - User 1"); | |
$('#winner').append("User 1 (black) - Winner"); | |
gameOver = true; | |
} | |
if (ticUser2 === collisions.length){ | |
console.log("Tic Tac Toe - User 2"); | |
$('#winner').append("User 2 (red) - Winner"); | |
gameOver = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment