Skip to content

Instantly share code, notes, and snippets.

@capoferro
Created January 18, 2011 01:09
Show Gist options
  • Save capoferro/783827 to your computer and use it in GitHub Desktop.
Save capoferro/783827 to your computer and use it in GitHub Desktop.
(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