Skip to content

Instantly share code, notes, and snippets.

@craftybones
Last active August 6, 2017 19:53
Show Gist options
  • Save craftybones/6e37cde8a1f42a7fbc7fe846b3d4b86d to your computer and use it in GitHub Desktop.
Save craftybones/6e37cde8a1f42a7fbc7fe846b3d4b86d to your computer and use it in GitHub Desktop.
(defn sqr [x] (* x x))
(defn winning-combinations [size-of-grid]
(let [indices (range 1 (inc (sqr size-of-grid)))
rows (partition size-of-grid indices)
cols (apply map list rows)
diagonal-1 (take-nth (inc size-of-grid) indices)
diagonal-2 (take-nth (dec size-of-grid)
(range size-of-grid (sqr size-of-grid)))]
(map set (concat rows cols [diagonal-1 diagonal-2]))))
(def is-superset? every?)
(defn has-won? [player-moves]
(some (partial is-superset? player-moves) (winning-combinations 3)))
var contains=function(list,element) {
return list.indexOf(element)>=0;
}
var isSubset=function(list1,list2) {
return list2.every(function(element){
return contains(list1,element);
})
}
var winningCombinations=[
[1,2,3],
[4,5,6],
[7,8,9],
[1,4,7],
[2,5,8],
[3,6,9],
[1,5,9],
[3,5,7]
];
var hasWon=function(playerMoves) {
return winningCombinations.some(function(combination){
return isSubset(playerMoves,combination)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment