Created
February 22, 2012 20:11
-
-
Save bodil/1886950 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
(ns lol | |
(:use clojure.test)) | |
(def board [nil :bomb nil nil | |
:bomb nil nil nil | |
nil nil :bomb nil | |
:bomb nil nil :bomb]) | |
(with-test | |
(defn bomb? | |
"Returns true if cell n contains a bomb, false otherwise." | |
[board n] | |
(if (or (< n 0) (>= n (count board))) false | |
(= :bomb (board n)))) | |
(is (true? (bomb? board 1))) | |
(is (false? (bomb? board 2))) | |
(is (false? (bomb? board -3))) | |
(is (false? (bomb? board 1337)))) | |
(with-test | |
(defn neighbours | |
"For a cell n, return a list of the cell's neighbours." | |
[n] | |
(map #(+ n %) | |
(remove nil? | |
(let [left (not (zero? (rem n 4))) | |
right (not (zero? (rem (inc n) 4))) | |
top (>= n 4) | |
bottom (< n 12)] | |
[ (when (and left top) -5) | |
(when top -4) | |
(when (and right top) -3) | |
(when left -1) | |
(when right 1) | |
(when (and left bottom) 3) | |
(when bottom 4) | |
(when (and right bottom) 5) ] | |
))) | |
) | |
(is (= [2 6 7] (neighbours 3))) | |
(is (= [8 9 13] (neighbours 12))) | |
(is (= [0 1 2 4 6 8 9 10] (neighbours 5)))) | |
(with-test | |
(defn bombs | |
"For a cell n, return the number of bombs adjacent to it." | |
[board n] | |
(count (filter #(bomb? board %1) (neighbours n)))) | |
(is (= 3 (bombs board 5))) | |
(is (= 0 (bombs board 3))) | |
(is (= 2 (bombs board 14)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment