From a843b201ba86804329e92639406bf26f021a09f7 Mon Sep 17 00:00:00 2001 From: Christian Romney Date: Fri, 19 Aug 2011 00:17:52 -0400 Subject: [PATCH] Added total function and wrote some high-level tests --- docs/uberdoc.html | 8 ++++--- project.clj | 2 +- src/prisoners/core.clj | 6 +++++- test/prisoners/test/core.clj | 41 +++++++++++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/docs/uberdoc.html b/docs/uberdoc.html index b2737aa..f2b2e71 100644 --- a/docs/uberdoc.html +++ b/docs/uberdoc.html @@ -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

diff --git a/project.clj b/project.clj index e7c84df..59ef2f4 100644 --- a/project.clj +++ b/project.clj @@ -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"] diff --git a/src/prisoners/core.clj b/src/prisoners/core.clj index aaeceaa..c4823bb 100644 --- a/src/prisoners/core.clj +++ b/src/prisoners/core.clj @@ -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] diff --git a/test/prisoners/test/core.clj b/test/prisoners/test/core.clj index 63df7f0..ff29bb1 100644 --- a/test/prisoners/test/core.clj +++ b/test/prisoners/test/core.clj @@ -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")))