2012年2月23日木曜日

Codecademyの課題をClojureで解いてみよう - その3

今回はDice Game
rand-intを使えば、最初から整数として返してくれるので楽チン。
サイコロの出目と点数を返す関数とそれを表示する関数にわけてもいいかもしれない。


(defn dice-game []
(let [die1 (+ 1 (rand-int 6))
die2 (+ 1 (rand-int 6))]
(printf "you rolled a %d and a %d for a score of %d\n"
die1
die2
(cond (or (= 1 die1) (= 1 die2)) 0
(= die1 die2) (* 2 (+ die1 die2))
:else (+ die1 die2)))))
view raw dice-game.clj hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿