Skip to content

Instantly share code, notes, and snippets.

@davidkaste
Created March 15, 2012 16:11
Show Gist options
  • Save davidkaste/2045016 to your computer and use it in GitHub Desktop.
Save davidkaste/2045016 to your computer and use it in GitHub Desktop.
RPS game based on nested vectors. Written in Clojure
(defn rpswinner
[match]
(if (= ((match 0) 1) ((match 1) 1))
(match 0)
(if (and (= ((match 0) 1) "R") (= ((match 1) 1) "P"))
(match 1)
(if (and (= ((match 0) 1) "P") (= ((match 1) 1) "S"))
(match 1)
(if (and (= ((match 0) 1) "S") (= ((match 1) 1) "R"))
(match 1)
(match 0)
)
)
)
)
)
(defn rpstournament
[tournament]
(if (instance? String (tournament 0))
tournament
(rpswinner (vector (rpstournament (tournament 0)) (rpstournament (tournament 1))))
)
)
(println (rpstournament
[
[
[ ["Player1", "P"] ["Player2", "S"] ]
[ ["Player3", "R"] ["Player4", "S"] ]
]
[
[ ["Player5", "S"] ["Player6", "P"] ]
[ ["Player7", "R"] ["Player8", "P"] ]
]
]
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment