Last active
April 5, 2022 22:53
-
-
Save mdwhatcott/77a1e1ae31f98e34c4f945882cbb662f to your computer and use it in GitHub Desktop.
Bowling Game Kata (in Racket)
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
#lang racket | |
(require rackunit) | |
(require "bowling.rkt") | |
(define (roll-many times roll) | |
(for/list ([i times]) roll)) | |
(test-equal? "Gutter Game" 0 (score (roll-many 20 0))) | |
(test-equal? "All Ones" 20 (score (roll-many 20 1))) | |
(test-equal? "Spare" 15 (score (append '(5 5 2 1) (roll-many 16 0)))) | |
(test-equal? "Strike" 21 (score (append '(10 3 2 1) (roll-many 15 0)))) | |
(test-equal? "Perfection" 300 (score (roll-many 12 10))) |
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
#lang racket | |
(define (take-n n coll) (if (< (length coll) n) coll (take coll n))) | |
(define (strike? rolls) (= 10 (apply + (take-n 1 rolls)))) | |
(define (spare? rolls) (= 10 (apply + (take-n 2 rolls)))) | |
(define (->frames rolls) | |
(cond [(empty? rolls) empty] | |
[(strike? rolls) (cons (take-n 3 rolls) (->frames (cdr rolls)))] | |
[(spare? rolls) (cons (take-n 3 rolls) (->frames (cddr rolls)))] | |
[else (cons (take-n 2 rolls) (->frames (cddr rolls)))])) | |
(define (score rolls) | |
(apply + (flatten (take-n 10 (->frames rolls))))) | |
(provide score) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment