Skip to content

Instantly share code, notes, and snippets.

@mh120888
Created June 28, 2016 00:12
Show Gist options
  • Save mh120888/a8888b87199b4d3e09fc7ac2c5d62fc0 to your computer and use it in GitHub Desktop.
Save mh120888/a8888b87199b4d3e09fc7ac2c5d62fc0 to your computer and use it in GitHub Desktop.
Let bindings
(defn get-winner
[board]
(let [num-of-rows (get-number-of-rows board)
spaces-on-board (range (count board))
horizontal-coords (partition num-of-rows spaces-on-board)
vertical-coords (apply map list horizontal-coords)
diagonal-coords (generate-diagonal-coords num-of-rows)
coords-to-check (concat horizontal-coords vertical-coords diagonal-coords)]
(loop [coords-to-check coords-to-check
row-to-check (first coords-to-check)
winner nil]
(if (or winner (empty? coords-to-check))
winner
(let [this-row-as-set (into #{} (vals (select-keys board (into [] (first coords-to-check)))))
is-this-a-win (and (seq (first this-row-as-set)) (= 1 (count this-row-as-set)))
winning-marker (or (and is-this-a-win (:marked (first this-row-as-set))) nil)]
(recur (rest coords-to-check) (first (rest coords-to-check)) winning-marker))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment