Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Last active April 5, 2022 22:53
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/77a1e1ae31f98e34c4f945882cbb662f to your computer and use it in GitHub Desktop.
Save mdwhatcott/77a1e1ae31f98e34c4f945882cbb662f to your computer and use it in GitHub Desktop.
Bowling Game Kata (in Racket)
#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)))
#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