Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Last active August 7, 2021 19:39
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 mdwhatcott/8e2446652842546982badd67649905e9 to your computer and use it in GitHub Desktop.
Save mdwhatcott/8e2446652842546982badd67649905e9 to your computer and use it in GitHub Desktop.
minimal-bowling.clj
(ns bowling.core-spec
(:require [speclj.core :refer :all]))
(defn is-strike? [rolls] (= 10 (first rolls)))
(defn is-spare? [rolls] (= 10 (apply + (take 2 rolls))))
(defn ->frames [rolls]
(cond
(empty? rolls) []
(is-strike? rolls)
(cons (take 3 rolls) (->frames (rest rolls)))
(is-spare? rolls)
(cons (take 3 rolls) (->frames (drop 2 rolls)))
:else
(cons (take 2 rolls) (->frames (drop 2 rolls)))))
(defn score [rolls]
(->> rolls ->frames (take 10) flatten (apply +)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn roll-many [times pins]
(flatten (take times (cycle [pins]))))
(describe "The 'Bowling Game' Kata"
(it "scores a gutter game" (->> (roll-many 20 0) score (should= 0)))
(it "scores all ones " (->> (roll-many 20 1) score (should= 20)))
(it "scores a spare " (->> (concat [5 5] [2 1]) score (should= 15)))
(it "scores a strike " (->> (concat [10] [3 2] [1 0]) score (should= 21)))
(it "scores perfection " (->> (roll-many 12 10) score (should= 300))))
(describe "Partitioning of rolls into frames"
(it "simple frames" (->> (concat [0 0] [1 1]) ->frames (should= [[0 0] [1 1]])))
(it "spare frames " (->> (concat [5 5] [2 1]) ->frames (should= [[5 5 2] [2 1]])))
(it "strike frames" (->> (concat [10] [3 2] [1 0]) ->frames (should= [[10 3 2] [3 2] [1 0]]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment