Created
June 28, 2018 16:57
-
-
Save bjjb/d8b868191eb2a2cd864dc3c88328f565 to your computer and use it in GitHub Desktop.
A Common-Lisp solution to Uncle Bob's Bowling Game kata
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
; A functional solution to Uncle Bob's Bowling Game | |
; https://programmingpraxis.com/2009/08/11/uncle-bobs-bowling-game-kata/ | |
(defun strike? (roll) | |
(and roll | |
(= 10 (first roll)))) | |
(defun spare? (roll) | |
(and roll | |
(rest roll) | |
(= 10 (+ (first roll) (second roll))))) | |
(defun bonus? (roll) | |
(and (third roll) | |
(or (strike? roll) | |
(spare? roll)))) | |
(defun bonus (roll) | |
(if (bonus? roll) | |
(or (third roll) 0) | |
0)) | |
(defun score-frame (frame) | |
(if frame | |
(+ (or (first frame) 0) | |
(or (second frame) 0) | |
(bonus frame)) | |
0)) | |
(defun score-game (game) | |
(if game | |
(+ (score-frame game) | |
(if (strike? game) | |
(score-game (rest game)) | |
(score-game (rest (rest game))))) | |
0)) | |
(assert (strike? '(10))) | |
(assert (strike? '(10 10))) | |
(assert (not (strike? '(4)))) | |
(assert (not (strike? '(9 10)))) | |
(assert (spare? '(1 9))) | |
(assert (spare? '(2 8 1))) | |
(assert (not (spare? '(1)))) | |
(assert (not (spare? '(1 7 1)))) | |
(assert (= 0 (score-frame '()))) | |
(assert (= 0 (score-frame '(0)))) | |
(assert (= 0 (score-frame '(0 0)))) | |
(assert (= 1 (score-frame '(0 1)))) | |
(assert (= 1 (score-frame '(0 1 0)))) | |
(assert (= 10 (score-frame '(9 1 0)))) | |
(assert (= 11 (score-frame '(9 1 1)))) | |
(assert (= 20 (score-frame '(9 1 10)))) | |
(assert (= 20 (score-frame '(9 1 10 10)))) | |
(assert (= 10 (score-frame '(10)))) | |
(assert (= 19 (score-frame '(10 8 1)))) | |
(assert (= 20 (score-frame '(10 10)))) | |
(assert (= 21 (score-frame '(10 10 1)))) | |
(assert (= 30 (score-frame '(10 10 10)))) | |
(assert (= 30 (score-frame '(10 10 10 10)))) | |
(assert (= 10 (score-game '(1 1 1 1 1 1 1 1 1 1)))) | |
(assert (= 21 (score-game '(10 1 1 1 1 1 1 1 1 1)))) | |
(assert (= 22 (score-game '(10 1 1 1 1 1 1 1 1 1 1)))) | |
(assert (= 30 (score-game '(9 1 10 0)))) | |
(assert (= 300 (score-game '(10 10 10 10 10 10 10 10 10 10 10)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby version is here.