FWIW (i.e. IMO the previous two functional solutions are better
examples) here is a more imperative style solution done sort of to
prove to myself that I could do such a thing in Clojure w/o too much
(arguable) fanfare. Maybe it will be interesting to others who are
learning Clojure too

 (defn score [rolls]
  (loop [roll-number 0 frame 0 score 0 frames []]
    (if (or (= frame 10) (= roll-number (count rolls)))
          frames
          (let [roll (nth rolls roll-number)]
            (if (= roll 10) ;; strike
                  (let [new-score (+ score roll (nth rolls (inc roll-number) 0) 
(nth
rolls (+ roll-number 2) 0))]
                        (recur (inc roll-number) (inc frame) new-score  (conj 
frames
{:score new-score :frame-type :strike})))
          (if-let [roll-2 (nth rolls (inc roll-number) nil)]
                        (if (= (+ roll roll-2) 10) ;;spare
                      (let [new-score (+ score roll roll-2 (nth rolls (+ 
roll-number
2) 0))]
                            (recur (+ roll-number 2) (inc frame) new-score 
(conj frames
{:score new-score :frame-type :spare})))
                      (let [new-score (+ score roll roll-2)]
                                (recur (+ roll-number 2) (inc frame) new-score 
(conj frames
{:score new-score :frame-type :underachieving?}))))
                        (let [new-score (+ score roll)]
                      (recur (inc roll-number) (inc frame) new-score (conj 
frames
{:score new-score :frame-type :incomplete})))))))))

(use 'clojure.test)

(deftest test-various-games
  (are [description expected-score game] (= expected-score (:score
(last (score game))))
       "gutter game" 0 (repeat 20 0)
       "all ones" 20 (repeat 20 1)
       "one spare" 16 (concat [5 5 3] (repeat 17 0))
       "one strike" 24 (concat [10 3 4] (repeat 17 0))
       "all nine pins first" 90 (conj (vec (interpose 9 (repeat 10
0) )) 9)
       "all spares w/ gutter ball first" 100 (conj (conj (vec
(interpose 10 (repeat 10 0))) 10) 0)
       "all spares w/ 1 pin first" 110 (conj (conj (vec (interpose 9
(repeat 10 1) )) 9) 1)
       "all spares w/ 9 pins first" 190 (conj (conj (vec (interpose 1
(repeat 10 9) )) 1) 9)
       "perfect game" 300 (repeat 12 10)
       ))

(run-tests)

On Jul 21, 8:37 pm, Mark Triggs <mark.h.tri...@gmail.com> wrote:
> An artifact of not running my code from a clean REPL before posting ;)
> It should just read `frames'.
>
> Cheers,
>
> Mark
>
> artg <artgittle...@gmail.com> writes:
> > What is "group-frames"?
>
> > --art
>
> > On Jul 21, 12:00 am, Mark Triggs <mark.h.tri...@gmail.com> wrote:
>
> [...]
>
> >>   (defn score-game [rolls]
> >>     (reduce + (map #(reduce + %)
> >>                    (take 10 (group-frames rolls)))))
>
> --
> Mark Triggs
> <mark.h.tri...@gmail.com>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to