Created
January 18, 2011 01:09
-
-
Save capoferro/783827 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(test/with-test | |
(defn- swap-duel-probabilities | |
([probabilities] | |
{:lead (:follow probabilities) :follow (:lead probabilities)})) | |
(test/is (= (swap-duel-probabilities {:lead 0.5 :follow 0.6}) {:lead 0.6 :follow 0.5}))) | |
(defn aggregate-probability | |
([probabilities duelist] | |
(reduce | |
(fn [sum probability] | |
(+ sum (duelist probability))) 0 probabilities))) | |
(defn perform-duel | |
([lead follow number_of_rounds] (perform-duel lead follow number_of_rounds 1)) | |
([lead follow number_of_rounds remaining_chance] | |
(cond (= (:w follow) 0) {:lead remaining_chance :follow 0} | |
(= (:w lead) 0) {:lead 0 :follow remaining_chance} | |
(or (= number_of_rounds 0) (= remaining_chance 0)) {:lead 0 :follow 0} | |
:else (let [eventual_kill_probabilities (map (fn [target_wounds] | |
(swap-duel-probabilities (perform-duel | |
(assoc follow :w (- (:w follow) target_wounds) | |
lead) | |
(- number_of_rounds 1) | |
(* remaining_chance (to-score-exact-wounds lead follow target_wounds))) | |
)) (range 0 (:w follow))) | |
immediate_kill_probability (* (to-kill lead follow) remaining_chance)] | |
{:lead (+ immediate_kill_probability (aggregate-probability eventual_kill_probabilities :lead)) | |
:follow (aggregate-probability eventual_kill_probabilities :follow)})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment