Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Last active August 12, 2021 22:34
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/a6578b036e645dac561fdcbbdc037478 to your computer and use it in GitHub Desktop.
Save mdwhatcott/a6578b036e645dac561fdcbbdc037478 to your computer and use it in GitHub Desktop.
framed-bowling.clj
(ns bowling.core-spec
(:require [speclj.core :refer :all]))
(defn is-strike? [[first & _]] (= 10 first))
(defn is-spare? [[first second & _]] (= 10 (+ first second)))
(defn split-frame [rolls]
(cond (is-strike? rolls) [(take 3 rolls) (rest rolls)]
(is-spare? rolls) [(take 3 rolls) (drop 2 rolls)]
:else [(take 2 rolls) (drop 2 rolls)]))
(defn ->frames [rolls]
(if (empty? rolls) []
(let [[frame remaining] (split-frame rolls)]
(cons frame (->frames remaining)))))
(defn score [rolls]
(->> rolls ->frames (take 10) flatten (apply +)))
(describe "The 'Bowling Game' Kata"
(it "scores a gutter game" (->> (score (repeat 20 0)) (should= 0)))
(it "scores open frames" (->> (score (repeat 20 1)) (should= 20)))
(it "scores with spares" (->> (score [5 5 2 1]) (should= 15)))
(it "scores with strikes" (->> (score [10 3 2 1 0]) (should= 21)))
(it "scores perfection" (->> (score (repeat 12 10)) (should= 300))))
(describe "Frame Partitioning"
(it "partitions open frames" (->> (->frames [0 0 1 1]) (should= [[0 0] [1 1]])))
(it "partitions spare frame" (->> (->frames [5 5 2 1]) (should= [[5 5 2] [2 1]])))
(it "partitions strike frame" (->> (->frames [10 3 2 1 0]) (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