public
anonymous / overload.clj
Created

Day of Code

  • Download Gist
overload.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 
(def size 3)
(def empty-board (vec (repeat size (vec (repeat size {:color :none :num 0})))))
 
(defn get-winner
[board]
(let
[filtered-board (filter #(not (= :none %)) (flatten board))]
(if (and (seq filtered-board)
(every? #(= (:color (first filtered-board)) (:color %)) filtered-board))
(:color (first filtered-board))
:none)))
 
(defn adjacent-cells [x y]
(filter (fn [[x y]] (and (not (neg? y)) (not (neg? x)) (< x size) (< y size)))
(list [(dec x) y] [(inc x) y] [x (dec y)] [x (inc y)])))
 
(defn spill [x y board]
board)
 
(defn place-piece [color board x y]
(let [board (update-in board [x y :num]
#(if (= (+ 1 %) (overload-limit board x y)) 0 (inc %)))]
(if (= 0 (get-in board [x y :num]))
(spill x y board)
board)))
 
(defn overload-limit
[board x y]
(cond
(and (or (= x 0) (= x (- size 1)))
(or (= y 0) (= y (- size 1)))) 2
(or (or (= x 0) (= x (- size 1)))
(or (= y 0) (= y (- size 1)))) 3
:else 4))
 
;; tests
 
(def black-board (vec (repeat size (vec (repeat size {:color :black :num 1})))))
(def white-board (vec (repeat size (vec (repeat size {:color :white :num 1})))))
(def mixed-board (assoc-in white-board [1 1 :color] :black))
 
(describe "empty board"
(it "has no winner"
(should= :none (get-winner empty-board))))
 
(describe "board with black pieces"
(it "has winner - black"
(should= :black (get-winner black-board))))
 
(describe "board with white pieces"
(it "has winner - white"
(should= :white (get-winner white-board))))
 
(describe "board with mixed pieces"
(it "has no winner"
(should= :none (get-winner mixed-board))))
 
(describe "placing piece in empty square"
(it "has board with piece in square"
(should= 1 (get-in (place-piece :white empty-board 0 0) [0 0 :num]))))
 
(describe "placing piece in corner square with one"
(it "leaves empty square"
(should= 0 (get-in (place-piece :white (update-in empty-board [0 0 :num] inc) 0 0) [0 0 :num]))))
 
(describe "placing piece in edge square with two"
(it "leaves empty square"
(should= 0 (get-in (place-piece :white (assoc-in empty-board [0 1 :num] 2) 0 1) [0 1 :num]))))
 
(describe "adjacent squares function"
(it "returns adjacent squares"
(should= (list [1 0] [0 1]) (adjacent-cells 0 0))))
 
(describe "placing piece in centre square with three"
(it "leaves empty square"
(should= 0 (get-in (place-piece :white (assoc-in empty-board [1 1 :num] 3) 1 1) [1 1 :num]))))
 
(describe "corner"
(it "has overload limit of two"
(should= 2 (overload-limit empty-board 0 0))))
 
(describe "edge"
(it "has overload limit of three"
(should= 3 (overload-limit empty-board 0 1))))
 
(describe "middle"
(it "has overload limit of four"
(should= 4 (overload-limit empty-board 1 1))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.