# christianromney/prisoners

Added total function and wrote some high-level tests

1 parent cec6a83 commit a843b201ba86804329e92639406bf26f021a09f7 committed Aug 19, 2011
Showing with 49 additions and 8 deletions.
1. +5 −3 docs/uberdoc.html
2. +1 −1 project.clj
3. +5 −1 src/prisoners/core.clj
4. +38 −3 test/prisoners/test/core.clj
 @@ -2526,7 +2526,7 @@ ul.css('margin-top', '0px') }) -prisoners -- Marginalia

prisoners

0.0.6-SNAPSHOT

A Prisoner's Dilemma simulation

+prisoners -- Marginalia

prisoners

0.0.7-SNAPSHOT

A Prisoner's Dilemma simulation

dependencies

org.clojure/clojure
1.2.1
org.clojure/clojure-contrib
1.2.0

dev dependencies

lein-marginalia
0.6.0

(this space intentionally left almost blank)

(ns prisoners.core
(:require clojure.string)
@@ -2617,9 +2617,11 @@
(last
(take (inc rounds)
(iterate play-round [{:name x :points [] :plays [] :opponent []}
-                           {:name y :points [] :plays [] :opponent []}]))))

Summarizes a strategy's score.

+ {:name y :points [] :plays [] :opponent []}]))))

Produce a total score as the sum of the points awarded during each round.

+
(defn total [strategy]
+  (reduce + (:points strategy)))

Summarizes a strategy's score.

(defn summarize [strategy]
-  (str (:name strategy) ": " (reduce + (:points strategy)) " points"))

Calculates the resulting scores for each of the strategies.

+ (str (:name strategy) ": " (total strategy) " points"))

Calculates the resulting scores for each of the strategies.

(defn report [strategies]
(clojure.string/join ", " (map summarize strategies)))

Running the Simulation

 @@ -1,4 +1,4 @@ -(defproject prisoners "0.0.6-SNAPSHOT" +(defproject prisoners "0.0.7-SNAPSHOT" :description "A Prisoner's Dilemma simulation" :dev-dependencies [[lein-marginalia "0.6.0"]] :dependencies [[org.clojure/clojure "1.2.1"]
 @@ -136,9 +136,13 @@ (iterate play-round [{:name x :points [] :plays [] :opponent []} {:name y :points [] :plays [] :opponent []}])))) +;; Produce a total score as the sum of the points awarded during each round. +(defn total [strategy] + (reduce + (:points strategy))) + ;; Summarizes a strategy's score. (defn summarize [strategy] - (str (:name strategy) ": " (reduce + (:points strategy)) " points")) + (str (:name strategy) ": " (total strategy) " points")) ;; Calculates the resulting scores for each of the strategies. (defn report [strategies]
 @@ -1,6 +1,41 @@ (ns prisoners.test.core - (:use [prisoners.core]) + (:require [prisoners.core :as pri]) (:use [clojure.test])) -(deftest replace-me ;; FIXME: write - (is false "No tests have been written.")) +;; Test Helpers + +(defn score-round [rounds x y] + (map (fn [m] [(:name m) (pri/total m)]) (pri/play-rounds rounds x y))) + +(defn score [x] + (last x)) + +;; Test Expected Outcomes + +(deftest test-cheat-sucker + (let [[x y] (score-round 1 :cheat :sucker)] + (is + (and + (= 5 (score x)) + (= 0 (score y))) ":cheat 5, :sucker 0"))) + +(deftest test-cheat-cheat + (let [[x y] (score-round 1 :cheat :cheat)] + (is + (and + (= 1 (score x)) + (= 1 (score y))) ":cheat 1, :cheat 1"))) + +(deftest test-sucker-sucker + (let [[x y] (score-round 1 :sucker :sucker)] + (is + (and + (= 3 (score x)) + (= 3 (score y))) ":sucker 3, :sucker 3"))) + +(deftest test-cheat-tit-for-tat + (let [[x y] (score-round 2 :cheat :tit-for-tat)] + (is + (and + (= 6 (last x)) + (= 1 (last y))) ":cheat 6, :tit-fot-tat 1")))