Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit: Uncle Bob's example dropped into a 1.1 project

  • Loading branch information...
commit 1d5e752773718203b0cdd3b15ec432b8d44abdcc 0 parents
@stuarthalloway authored
1  bin/repl.sh
@@ -0,0 +1 @@
+java -cp src:test:lib/jline-0.9.94.jar:lib/clojure.jar:lib/clojure-contrib.jar jline.ConsoleRunner clojure.main -i src/repl.clj -r
1  bin/runtests.sh
@@ -0,0 +1 @@
+java -Xmx1G -cp test:src:lib/clojure.jar:lib/clojure-contrib.jar:lib/hsqldb.jar:lib/ant.jar:lib/ant-launcher.jar clojure.lang.Script test/test.clj
BIN  lib/clojure-contrib.jar
Binary file not shown
BIN  lib/clojure.jar
Binary file not shown
BIN  lib/jline-0.9.94.jar
Binary file not shown
31 src/bowling_game.clj
@@ -0,0 +1,31 @@
+(ns bowling-game)
+(defn new-game [] [])
+
+(defn next-two-balls [rolls]
+ (+ (rolls 0) (rolls 1)))
+
+(defn score-strike [rolls]
+ [1 (+ 10 (+ (rolls 1) (rolls 2)))])
+
+(defn score-spare [rolls]
+ [2 (+ 10 (rolls 2))])
+
+(defn score-no-mark [rolls]
+ [2 (next-two-balls rolls)])
+
+(defn score-next-frame [rolls]
+ (if (= 10 (first rolls))
+ (score-strike rolls)
+ (if (= 10 (next-two-balls rolls))
+ (score-spare rolls)
+ (score-no-mark rolls))))
+
+(defn score [game]
+ (loop [frame 1 rolls game score 0]
+ (if (> frame 10)
+ score
+ (let [frame-score (score-next-frame rolls)]
+ (recur (inc frame) (subvec rolls (frame-score 0)) (+ score (frame-score 1)))))))
+
+(defn roll [game pins]
+ (conj game pins))
5 src/repl.clj
@@ -0,0 +1,5 @@
+; making the REPL nicer
+(use '[clojure.contrib java-utils pprint seq-utils repl-utils duck-streams]
+ 'bowling-game
+ 'test.bowling-game
+ )
11 test/test.clj
@@ -0,0 +1,11 @@
+(ns test
+ (:use clojure.test))
+
+(def tests
+ ['test.bowling-game])
+
+(doseq [test tests] (require test))
+
+(apply run-tests tests)
+
+(shutdown-agents)
43 test/test/bowling_game.clj
@@ -0,0 +1,43 @@
+(ns test.bowling-game
+ (:use clojure.test)
+ (:use bowling-game))
+
+(defn roll-list [game list]
+ (if (empty? list)
+ game
+ (roll-list (roll game (first list)) (rest list))
+ ))
+
+(defn roll-many [game n pins]
+ (loop [i n g game]
+ (if (zero? i)
+ g
+ (recur (dec i) (roll g pins)))))
+
+(defn gutter-game [game] (roll-many game 20 0))
+
+(deftest can-create-game
+ (is (not (nil? (new-game)))))
+
+(deftest gutter-game-should-score-0
+ (is (= 0 (score (gutter-game (new-game))))))
+
+(deftest all-ones-should-score-20
+ (is (= 20 (score (roll-many (new-game) 20 1)))))
+
+(deftest one-spare
+ (is (= 16 (score
+ (roll-many
+ (roll-list (new-game) [5 5 3])
+ 17 0)))))
+
+(deftest one_strike
+ (is (= 24 (score
+ (roll-many
+ (roll-list (new-game) [10 3 4])
+ 16 0)))))
+
+(deftest perfect-game
+ (is (= 300 (score (roll-many (new-game) 12 10)))))
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.