Last active
August 29, 2015 14:04
-
-
Save halogenandtoast/1a4cfe9691429f1b0836 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 poker.core) | |
(def suits ['C 'D 'H 'S]) | |
(def values ['A '2 '3 '4 '5 '6 '7 '8 '9 'T 'J 'Q 'K]) | |
(def deck (map (fn [i] [(rem i 13) (quot i 13)]) (range 52))) | |
(defn format-card [card] | |
(apply str (map get [values suits] card))) | |
(defn low-card [hand] | |
(first (sort-by first hand))) | |
(defn high-card [hand] | |
(last (sort-by first hand))) | |
(defn highest-card [hand] | |
(or | |
(first (filter #(= 0 (first %)) hand)) | |
(high-card hand))) | |
(defn royal? [hand] | |
(= (sort (map first hand)) [0 9 10 11 12])) | |
(defn flush? [hand] | |
(let [suit (last (first hand))] | |
(every? #(= suit (last %)) hand))) | |
(defn royal-flush? [hand] | |
(and | |
(flush? hand) | |
(royal? hand))) | |
(defn straight? [hand] | |
(or | |
(royal? hand) | |
(= (sort (map first hand)) (range ((low-card hand) 0) (inc ((high-card hand) 0)))))) | |
(defn straight-flush? [hand] | |
(and | |
(straight? hand) | |
(flush? hand))) | |
(defn partition-hand [hand] | |
(partition-by first (sort-by first hand))) | |
(defn groups-of [hand number] | |
(filter #(= number (count %)) (partition-hand hand))) | |
(defn of-a-kind? [hand number] | |
(= 1 (count (groups-of hand number)))) | |
(defn four-of-a-kind? [hand] | |
(of-a-kind? hand 4)) | |
(defn three-of-a-kind? [hand] | |
(of-a-kind? hand 3)) | |
(defn two-pair? [hand] | |
(= 2 (count (groups-of hand 2)))) | |
(defn one-pair? [hand] | |
(of-a-kind? hand 2)) | |
(defn full-house? [hand] | |
(and | |
(of-a-kind? hand 2) | |
(of-a-kind? hand 3))) | |
(defn highest-hand [hand] | |
(condp #(%1 %2) hand | |
royal-flush? "Royal flush" | |
straight-flush? "Straight flush" | |
four-of-a-kind? "Four of a kind" | |
full-house? "Full house" | |
flush? "Flush" | |
straight? "Straight" | |
three-of-a-kind? "Three of a kind" | |
two-pair? "Two pair" | |
one-pair? "One pair" | |
(str "High card: " (format-card (highest-card hand))))) | |
(defn print-hand [hand] | |
(doseq [card hand] (println (format-card card))) | |
(println) | |
(println (highest-hand hand)) | |
(println)) | |
(defn print-player [player deck] | |
(println (str "Player " player)) | |
(println (apply str (repeat 8 "-"))) | |
(print-hand (take 5 deck))) | |
(defn -main [& args] | |
(loop [player 1 shuffled-deck (shuffle deck)] | |
(print-player player shuffled-deck) | |
(when (< player 4) (recur (inc player) (drop 5 shuffled-deck))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment