Last active
July 1, 2020 12:28
-
-
Save viebel/bda4c4b0726f7604e98b4cd58be66a5a to your computer and use it in GitHub Desktop.
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
(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