Skip to content

Instantly share code, notes, and snippets.

@corani
Created December 25, 2017 07:02
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 corani/550003dc6727ad17a7126ed7c4bc8f27 to your computer and use it in GitHub Desktop.
Save corani/550003dc6727ad17a7126ed7c4bc8f27 to your computer and use it in GitHub Desktop.
Bowling Game Kata in Scheme
#lang racket
(require
rackunit
rackunit/text-ui
)
(define (bowling rolls)
(define (spare? rolls)
(= (+ (first rolls)
(second rolls)
)
10
)
)
(define (strike? rolls)
(= (first rolls)
10
)
)
(define (no-bonus? rolls)
(< (length rolls) 3)
)
(define (bonus-frame? rolls)
(< (length rolls) 2)
)
(define (frame-score rolls)
(+ (first rolls)
(second rolls)
(bowling (list-tail rolls 2))
)
)
(define (spare-score rolls)
(+ (first rolls)
(second rolls)
(third rolls)
(bowling (list-tail rolls 2))
)
)
(define (strike-score rolls)
(cond
[(no-bonus? rolls)
0
]
[else
(+ (first rolls)
(second rolls)
(third rolls)
(bowling (list-tail rolls 1))
)
]
)
)
(cond
[(bonus-frame? rolls)
0
]
[(strike? rolls)
(strike-score rolls)
]
[(spare? rolls)
(spare-score rolls)
]
[else
(frame-score rolls)
]
)
)
(define bowling-tests (test-suite "Bowling Game Kata"
(check-equal? (bowling (make-list 20 '0))
0
"All `- -` Game"
)
(check-equal? (bowling (make-list 20 '1))
20
"All `1 1` Game"
)
(check-equal? (bowling (flatten (make-list 10 '(9 0))))
90
"All `9 -` Game"
)
(check-equal? (bowling (append '( 5 5 3) (make-list 17 '0)))
16
"One Spare Game"
)
(check-equal? (bowling (append '(10 5 3) (make-list 16 '0)))
26
"One Strike Game"
)
(check-equal? (bowling (make-list 21 '5))
150
"All `5 /` Game"
)
(check-equal? (bowling (make-list 12 '10))
300
"All `X` Game"
)
))
(run-tests bowling-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment