Created
April 10, 2014 07:56
-
-
Save stianeikeland/10353608 to your computer and use it in GitHub Desktop.
Maxi yahtzee scoring - Bergen Coding Dojo - 10th of April - Stian and Anneli
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 yathzee.core | |
(:require [clojure.set :refer :all])) | |
(defn- sum [list] | |
(reduce + list)) | |
(defn score-singles [num roll] | |
(sum (filter #(= num %) roll))) | |
(def score-1s (partial score-singles 1)) | |
(def score-2s (partial score-singles 2)) | |
(def score-3s (partial score-singles 3)) | |
(def score-4s (partial score-singles 4)) | |
(def score-5s (partial score-singles 5)) | |
(def score-6s (partial score-singles 6)) | |
(defn ultimate-generalization-of-n-count-x-touples [toup n roll] | |
(let [pair-scores (->> (frequencies roll) | |
(filter #(>= (val %) toup)) | |
(map first) | |
(sort) | |
(reverse) | |
(map #(* toup %)))] | |
(if (< (count pair-scores) n) 0 | |
(sum (take n pair-scores))))) | |
(def score-x-pairs (partial ultimate-generalization-of-n-count-x-touples 2)) | |
(def score-x-kind #(ultimate-generalization-of-n-count-x-touples %1 1 %2)) | |
(def score-pair (partial score-x-pairs 1)) | |
(def score-2-pair (partial score-x-pairs 2)) | |
(def score-3-pair (partial score-x-pairs 3)) | |
(def score-3-kind (partial score-x-kind 3)) | |
(def score-4-kind (partial score-x-kind 4)) | |
(def score-5-kind (partial score-x-kind 5)) | |
(defn score-straight [straight roll] | |
(if (= (intersection (set roll) straight) | |
straight) | |
(sum straight) 0)) | |
(def score-small-straight (partial score-straight #{1 2 3 4 5})) | |
(def score-large-straight (partial score-straight #{2 3 4 5 6})) | |
(def score-full-straight (partial score-straight #{1 2 3 4 5 6})) | |
(defn- house-scorer [a b] | |
(+ (* 3 a) | |
(* 2 b))) | |
(defn score-full-house [roll] | |
(let [list-2-or-more (->> (frequencies roll) | |
(filter #(>= (val %) 2)) | |
(sort-by last) | |
(reverse))] | |
(if (< (count list-2-or-more) 2) 0 | |
(let [[a b] list-2-or-more] | |
(cond (= (second a) (second b) 3) (apply house-scorer (-> [(first a) (first b)] sort reverse)) | |
(>= (second a) 3) (house-scorer (first a) (first b)) | |
:else 0))))) | |
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 yathzee.core-test | |
(:use midje.sweet) | |
(:require [yathzee.core :refer :all])) | |
(fact "score ones" | |
(score-1s [1 2 3 4 5 6]) => 1 | |
(score-1s [1 1 1 1 1 1]) => 6 | |
(score-1s [2 3 4 5 6 7]) => 0) | |
(fact "score twos" | |
(score-2s [1 2 3 4 5 6]) => 2 | |
(score-2s [2 2 2 2 2 2]) => 12 | |
(score-2s [1 3 4 5 6 7]) => 0) | |
(fact "score 3..6" | |
(score-3s [1 2 3 3 4 5]) => 6 | |
(score-4s [1 2 3 4 4 5]) => 8 | |
(score-5s [1 2 3 5 5 6]) => 10 | |
(score-6s [1 2 3 6 6 5]) => 12) | |
(fact "score pair" | |
(score-pair [1 3 2 3 4 5]) => 6 | |
(score-pair [1 1 6 6 2 3]) => 12 | |
(score-pair [6 6 6 1 1 2]) => 12 | |
(score-pair [1 2 3 4 5 6]) => 0) | |
(fact "score two-pair" | |
(score-2-pair [3 3 4 4 5 6]) => 14 | |
(score-2-pair [6 5 4 4 5 6]) => 22 | |
(score-2-pair [1 2 3 4 5 6]) => 0 | |
(score-2-pair [1 1 2 3 4 5]) => 0) | |
(fact "score three-pair" | |
(score-3-pair [1 1 2 2 3 3]) => 12) | |
(fact "3 of a kind" | |
(score-3-kind [1 1 1 2 3 4]) => 3 | |
(score-3-kind [1 2 3 4 5 6]) => 0 | |
(score-3-kind [1 1 1 6 6 6]) => 18) | |
(fact "4 of a kind" | |
(score-4-kind [1 1 1 1 2 3]) => 4 | |
(score-4-kind [1 1 1 1 1 2]) => 4) | |
(fact "5 of a kind" | |
(score-5-kind [1 1 1 1 2 3]) => 0 | |
(score-5-kind [1 1 1 1 1 2]) => 5) | |
(fact "small straight" | |
(score-small-straight [1 2 3 4 5 6]) => 15 | |
(score-small-straight [1 2 3 4 6 6]) => 0) | |
(fact "large straight" | |
(score-large-straight [2 3 4 5 6 1]) => 20 | |
(score-large-straight [6 4 5 3 2 2]) => 20 | |
(score-large-straight [1 1 1 1 1 1]) => 0) | |
(fact "full straight" | |
(score-full-straight [1 2 3 4 5 6]) => 21) | |
(fact "full house" | |
(score-full-house [2 2 2 5 5 1]) => 16 | |
(score-full-house [1 2 3 4 5 6]) => 0 | |
(score-full-house [1 1 1 2 2 2]) => 8 | |
(score-full-house [2 2 2 1 1 1]) => 8) |
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
(defproject yathzee "0.1.0-SNAPSHOT" | |
:description "Yathzee - Bergen coding dojo" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.6.0"]] | |
:profiles {:dev {:plugins [[lein-midje "3.1.3"]] | |
:dependencies [[midje "1.6.3" :exclusions [org.clojure/clojure]]]}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment