Created
March 24, 2010 03:34
-
-
Save bmabey/341958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns monte-carlo-solitaire.card | |
(use [clojure.contrib.str-utils :only [re-split]])) | |
(defn- inverse-map | |
"Inverses map m so that the vals become the keys, and the keys become vals." | |
[m] | |
(zipmap (vals m) (keys m))) | |
(def suits [:hearts :diamonds :clubs :spades]) | |
; TODO: swtich to unicode chars.. I tried using the unicode code with (char) but to no avail... | |
; ["♥" "♦" "♣" "♠"] -> [ | |
(def card-suit-chars | |
(zipmap suits [\H \D \C \S])) | |
(def card-chars-suit (inverse-map card-suit-chars)) | |
(def card-values [:two :three :four :five :six :seven :eight | |
:nine :ten :jack :queen :king :ace]) | |
(def card-value-chars | |
(zipmap card-values [\2 \3 \4 \5 \6 \7 \8 \9 \T \J \Q \K \A])) | |
(def card-chars-value (inverse-map card-value-chars)) | |
(defprotocol ICard | |
(color [card] "Returns the color of the card.") | |
(dec-value [card] "Returns the value of the card below this one. i.e :king -> :queen")) | |
(deftype Card [#^clojure.lang.Keyword value | |
#^clojure.lang.Keyword suit] | |
clojure.lang.IPersistentMap | |
ICard | |
(color [] (if (or (= suit :hearts) (= suit :diamonds)) :red :black)) | |
(dec-value [] (get card-values (dec (.indexOf card-values value)))) | |
Object | |
(toString [] | |
(str (get card-value-chars value "?") | |
(get card-suit-chars suit "?")))) | |
(defn str->card | |
"Converts a string represenation (i.e. \"5H\") of a card into a Card." | |
;[[val-char suit-char] card-str] ; For some reason I can't destructure this here. | |
[card-str] | |
(let [[val-char suit-char] card-str] | |
(Card (card-chars-value val-char) (card-chars-suit suit-char)))) | |
(defn str->cards | |
"Converts a string representation of a stack of cards into | |
actual Cards. An example string: \"3H KD QS\"" | |
[cards-str] | |
(map str->card (re-split #"\s+" cards-str))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.IllegalArgumentException: Can't define method not in interfaces: dec_value (card.clj:30) | |
at clojure.lang.Compiler.analyzeSeq(Compiler.java:5311) | |
at clojure.lang.Compiler.analyze(Compiler.java:5125) | |
at clojure.lang.Compiler.analyze(Compiler.java:5086) | |
at clojure.lang.Compiler.eval(Compiler.java:5362) | |
at clojure.lang.Compiler.eval(Compiler.java:5349) | |
at clojure.lang.Compiler.load(Compiler.java:5768) | |
at clojure.lang.RT.loadResourceScript(RT.java:328) | |
at clojure.lang.RT.loadResourceScript(RT.java:319) | |
at clojure.lang.RT.load(RT.java:397) | |
at clojure.lang.RT.load(RT.java:369) | |
at clojure.core$load__5036$fn__5043.invoke(core.clj:4312) | |
at clojure.core$load__5036.doInvoke(core.clj:4311) | |
at clojure.lang.RestFn.invoke(RestFn.java:409) | |
at clojure.core$load_one__4977.invoke(core.clj:4148) | |
at clojure.core$load_lib__4992.doInvoke(core.clj:4185) | |
at clojure.lang.RestFn.applyTo(RestFn.java:143) | |
at clojure.core$apply__3588.invoke(core.clj:480) | |
at clojure.core$load_libs__5008.doInvoke(core.clj:4211) | |
at clojure.lang.RestFn.applyTo(RestFn.java:138) | |
at clojure.core$apply__3588.invoke(core.clj:480) | |
at clojure.core$require__5030.doInvoke(core.clj:4279) | |
at clojure.lang.RestFn.invoke(RestFn.java:422) | |
at circumspec.watch$load_source_namespace__668.invoke(watch.clj:91) | |
at circumspec.watch$load_source_and_test_namespace__672.invoke(watch.clj:102) | |
at circumspec.watch$re_test__674$fn__675.invoke(watch.clj:117) | |
at circumspec.watch$re_test__674.invoke(watch.clj:115) | |
at circumspec.watch$run_watcher__686.invoke(watch.clj:142) | |
at circumspec.watch$agent_watch_fn__690.invoke(watch.clj:156) | |
at clojure.lang.AFn.applyToHelper(AFn.java:165) | |
at clojure.lang.AFn.applyTo(AFn.java:153) | |
at clojure.lang.Agent$Action.doRun(Agent.java:100) | |
at clojure.lang.Agent$Action.run(Agent.java:150) | |
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) | |
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) | |
at java.lang.Thread.run(Thread.java:637) | |
Caused by: java.lang.IllegalArgumentException: Can't define method not in interfaces: dec_value | |
at clojure.lang.Compiler$NewInstanceMethod.parse(Compiler.java:6525) | |
at clojure.lang.Compiler$NewInstanceExpr.build(Compiler.java:6105) | |
at clojure.lang.Compiler$NewInstanceExpr$DeftypeParser.parse(Compiler.java:5995) | |
at clojure.lang.Compiler.analyzeSeq(Compiler.java:5304) | |
... 34 more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment