Last active
August 29, 2015 14:08
-
-
Save Shiggiddie/1dc9971727b24354fcec to your computer and use it in GitHub Desktop.
The tic-tac-toe project presented itself with a problem of "checking for a win state", this gist contains an elegant non-performant, and a less-elegant performant solution.
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
/* | |
Background: | |
- The Grid object has an array of Vector objects accessible in the Grid's "vectors" attribute | |
- A Vector object has a "value" attribute that contains "O", "X", or null if not set | |
- The checkForWin method on the Grid returns true if and only if there is a win state in tic-tac-toe | |
* The win state is determined by looping through all the various winCombos, where winCombos is an array of arrays, whose inner arrays represent the 3 indexes of the Grid's "vectors" array that refer to row, collumn, or diagonal on a tic-tac-toe board which -if all containing the same Vector "value" value- constitutes a win. | |
*/ | |
// Elegant, non-performant | |
Grid.prototype.checkForWin = function() { | |
//var surroundingVectors = this.getSurroundingVectors(index); | |
var winCombos = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]; | |
var self = this; | |
//The current win combo reduce needs to be refactored into it's own function | |
return winCombos.reduce(function(previousValue, currentWinCombo) { | |
return previousValue || currentWinCombo.reduce(function(w,el,i,arr){ | |
return w && (arr.length-1 == i || (self.vectors[el].value != null && self.vectors[el].value == self.vectors[arr[i+1]].value)) | |
}, true) | |
}, false) | |
} | |
// Less elegant, performant | |
Grid.prototype.checkForWin = function() { | |
//var surroundingVectors = this.getSurroundingVectors(index); | |
var winCombos = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]; | |
var self = this; | |
//The current win combo reduce needs to be refactored into it's own function | |
return winCombos.some( | |
function(currentWinCombo) { | |
if (currentWinCombo.every( | |
function(el, i, cwc) { | |
console.log("in currentWinCombo.every, el: " + el + ", i: " + i + ", cwc: " + cwc); | |
if (cwc.length-1 == i) { | |
console.log("COMPLETE MATCH FOUND ON CURRENTWINCOMBO!"); | |
return true; | |
} | |
else if (self.vectors[el].value == null || self.vectors[el].value != self.vectors[cwc[i+1]].value) { | |
console.log("NO MATCH ON CURRENTWINCOMBO! BREAKING"); | |
return false; | |
} | |
else { | |
console.log("MATCHED SO FAR ON CURRENTWINCOMBO! KEEP GOING!"); | |
return true; | |
} | |
})) { | |
console.log("FOUND A WIN!!! BREAKING!"); | |
return true; | |
} | |
else { | |
console.log("NO WIN FOUND!!! KEEP GOING!"); | |
return false; | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment