Created
December 14, 2010 18:38
-
-
Save abedra/740843 to your computer and use it in GitHub Desktop.
A quick whack at the bowling kata
This file contains hidden or 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 bowling.core | |
(:use clojure.test)) | |
(defn strike? [rolls] | |
(= 10 (first rolls))) | |
(defn spare? [rolls] | |
(= 10 (reduce + (take 2 rolls)))) | |
(defn rolls-to-advance [rolls frame] | |
(if (strike? rolls) | |
(if (= 10 frame) 3 1) 2)) | |
(defn items-in-frame [rolls] | |
(cond (strike? rolls) 3 | |
(spare? rolls) 3 | |
:else 2)) | |
(defn score [rolls] | |
(loop [total 0 | |
frame 1 | |
rolls rolls] | |
(if (not (empty? rolls)) | |
(recur (+ total (reduce + (take (items-in-frame rolls) rolls))) | |
(inc frame) | |
(drop (rolls-to-advance rolls frame) rolls)) | |
total))) | |
(deftest bowling | |
(testing "gutterball game" | |
(is (= 0 (score (repeat 20 0))))) | |
(testing "game of all one pins" | |
(is (= 20 (score (repeat 20 1))))) | |
(testing "one spare" | |
(is (= 22 (score '(5 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))))) | |
(testing "one strike" | |
(is (= 28 (score '(10 6 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))))) | |
(testing "perfect game" | |
(is (= 300 (score (repeat 12 10)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment