Skip to content

Commit

Permalink
rock-paper-scissors draft complete
Browse files Browse the repository at this point in the history
update license to exact match clojure license
  • Loading branch information
stuarthalloway committed Mar 10, 2010
1 parent 96f0b66 commit c96dc78
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
13 changes: 0 additions & 13 deletions LICENSE

This file was deleted.

12 changes: 12 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# labrepl for Clojure

Copyright (c) Relevance, Inc. All rights reserved.
The use and distribution terms for this software are covered by the
Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
which can be found in the file epl-v10.html at the root of this distribution.
By using this software in any fashion, you are agreeing to be bound by
the terms of this license.
You must not remove this notice, or any other, from this software.

# Breaking News

The first use of the labrepl will be the March 15, 2010 run of the
Clojure Studio: (http://pragmaticstudio.com/clojure). If you are looking
at labrepl before that date, then QA is not complete. Expect a few oddities.

# Getting Started

Expand Down
2 changes: 1 addition & 1 deletion src/labrepl/lab.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[]
[:intro :names-and-places :its-all-data :project-euler
:mini-browser :unified-update-model :zero-sum :cellular-automata
:defstrict])
:defstrict :rock-paper-scissors])

(defn lab-url
[lab-name]
Expand Down
66 changes: 66 additions & 0 deletions src/labs/rock_paper_scissors.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
(ns #^{:skip-wiki true} labs.rock-paper-scissors
(:use labrepl.util solutions.rock-paper-scissors))

(defn overview
[]
[[:h3 "Overview"]
[:p "Clojure's protocols and types are a more powerful and flexible alternative to interfaces and classes. They solve the expression problem, allowing you to extend old things that you don't control, and allowing old code to easily use new, all with performance consistent with the fastest forms fof virtual invocation available on the host platform (e.g. Java)."]
[:p "We won't explore all the power at once. In this lab, you will get your feet wet using protocols and types to simulate players in the classic rock-paper-scissors game. I got the idea for this texercise from an episode of the " [:a {:href "http://www.rubyquiz.com/quiz16.html"} "Ruby Quiz"] "."]])

(defn the-rules
[]
[[:h3 "The Rules"]
[:ol
[:li "The rules are pretty simple. Rock beats scissors, scissors beats paper, and paper beats rock. Write an idiomatic Clojure function " (c dominates) " that returns the thing that dominates the argument passed in."
(showme dominates)]
[:li "Create a " (c choices) " set that contains the possible choices. Don't Repeat Yourself."
(showme choices)]
[:li "Create a " (c winner) " function that takes two players' choices, and returns the winner, or " (c nil) " for a tie:"
(showme winner)]
[:li "Create a " (c draw?) " predicate that takes two players' choices and returns true if they are a draw."
(showme draw?)]
[:li "Create an " (c iwon?) " predicate that takes two players' choices and returns true if the first player won."
(showme iwon?)]]])

(defn the-players
[]
[[:h3 "The Players"]
[:ol
[:li "All the players will conform to a " (c Player) " protocol. It should have two methods: "
[:ol
[:li (c choose) " takes a player and returns that player's choice"]
[:li (c update-strategy) " takes a player, that player's last choice, and the other player's last choice, returning the " (c Player) " for the next round"]]
(showme Player)]
[:li "Use " (c deftype) " to define a " (c Random) " player. " (c Random) " always picks at random, and never changes strategy based on what the other player is doing."
(showme Random)]
[:li "Create " (c Stubborn) ", who is initialized with a choice and sticks with it come hell or high water:"
(showme Stubborn)]
[:li "Create " (c Mean) ", who is a little more subtle. " (c Mean) "sticks with what worked last time, or plays at random following a loss:"
(showme Mean)]
[:li "Now let's play the " (c game) ", with three arguments: two players and a number of rounds. " (c game) " reads nicely as a loop with five arguments:"
[:ol
[:li "player 1"]
[:li "player 2"]
[:li "player 1's current score"]
[:li "player 2's current score"]
[:li "the number of rounds remaining"]]
"The game should return the two player's scores in a map."
(showme game)]
[:li "Try some games with the various players. Do the results match your intuition?"]]])

(defn bonus
[]
[[:h3 "Bonus"]
[:ol
[:li "Create some other strategies."]
[:li "Compare your solution with some of the " [:a {:href "http://www.rubyquiz.com/quiz16.html"}"Ruby Quiz solutions"] ". What are the salient differences?"]
[:li "How would you parallelize the code to spread games out to all your cores?"]
[:li "Extend the simulation to support " [:a {:href "http://www.samkass.com/theories/RPSSL.html"} "Rock Paper Scissors Spock Lizard"]]]])

(defn instructions
[]
(concat
(overview)
(the-rules)
(the-players)
(bonus)))
2 changes: 1 addition & 1 deletion src/solutions/rock_paper_scissors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:scissors :rock
:paper :scissors})

(def choices #{:rock :paper :scissors})
(def choices (set (keys dominates)))

(defn random-choice []
(nth (vec choices) (rand-int (count choices))))
Expand Down

0 comments on commit c96dc78

Please sign in to comment.