Skip to content

Instantly share code, notes, and snippets.

@viebel
Last active July 1, 2020 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/bda4c4b0726f7604e98b4cd58be66a5a to your computer and use it in GitHub Desktop.
Save viebel/bda4c4b0726f7604e98b4cd58be66a5a to your computer and use it in GitHub Desktop.
(defn vec->matrix [n v]
(->> v
(partition n)
(mapv vec)))
(defn empty-vec [n]
(into [] (repeat n nil)))
(defn rand-numbers [n k]
(->> (range n)
shuffle
(take k)))
(defn create-board [n bombs]
(->> (rand-numbers (* n n) bombs)
(reduce (fn [acc pos]
(assoc acc pos :bomb))
(empty-vec (* n n)))
(vec->matrix n)))
(def board (create-board 5 12))
board
(defn neighbours [n x y]
(->> [[(inc x) y]
[(dec x) y]
[x (inc y)]
[(inc x) (inc y)]
[(dec x) (inc y)]
[x (dec y)]
[(inc x) (dec y)]
[(dec x) (dec y)]]
(filter (fn [[x y]]
(and (<= 0 x (dec n))
(<= 0 y (dec n)))))))
(count (filter #(= :bomb (get-in board %))
(neighbours 5 0 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment