Created
February 22, 2014 12:18
-
-
Save cades/9153082 to your computer and use it in GitHub Desktop.
Solution to lisp-koan' scoring-project, Greed dice game.
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
(defun score (dice) | |
(let ((dice (sort dice #'>))) | |
(flet ((is-1-or-5 (x) | |
(or (= x 1) (= x 5))) | |
(triple-p (dice) | |
(and (= (length dice) 3) (apply #'= dice))) | |
(triple-score (dice) | |
(if (= (first dice) 1) | |
1000 | |
(* 100 (first dice))))) | |
(cond ((null dice) 0) | |
;; when length = 1 | |
((equal dice '(5)) 50) | |
((equal dice '(1)) 100) | |
((= (length dice) 1) 0) | |
;; when length = 2 | |
((= (length dice) 2) | |
(+ (score (list (first dice))) | |
(score (rest dice)))) | |
;; when length = 3 | |
((= (length dice) 3) | |
(if (triple-p dice) | |
(triple-score dice) | |
(+ (score (subseq dice 0 1)) (score (subseq dice 1))))) | |
;; when length >= 4 | |
(t | |
(+ (score (subseq dice 0 1)) (score (subseq dice 1)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment