-
Notifications
You must be signed in to change notification settings - Fork 0
/
risk-dice.clj
120 lines (109 loc) · 2.83 KB
/
risk-dice.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(ns
my-prob-test
(:use [clojure.contrib.probabilities.finite-distributions
:only (uniform prob cond-prob join-with dist-m choose
normalize certainly cond-dist-m normalize-cond)])
(:use [clojure.contrib.monads
:only (domonad with-monad m-seq m-chain m-lift)])
(:require clojure.contrib.accumulators))
(defn make-die [low high]
"Create fair die"
(uniform (set (range low (+ 1 high)))))
(defn make-dice [low high num]
(loop [a-dice [] i num]
(if (zero? i)
a-dice
(recur (conj a-dice (make-die low high)) (dec i)))))
;; look up how to do comparisons in the source file
(defn roll-prob [an dn al ah dl dh]
"Calculates prob of rolls given ties go to the defender"
(let [a-die (make-die al ah)
d-die (make-die dl dh)]
(cond
(= an dn 1)
(one-v-one a-die d-die)
(= an 1)
(one-v-two a-die d-die)
(= an dn 2)
(two-v-two a-die d-die)
(= an 2)
(two-v-one a-die d-die)
(and (= dn 2) (= an 3))
(three-v-two a-die d-die)
(and (= dn 1) (= an 3))
(three-v-one a-die d-die))))
(defn any-v-any [an dn a-die d-die]
"Calculates rolls for any number of attackers or defenders"
(domonad dist-m
[]
))
(defn one-v-one [a-die d-die]
(domonad dist-m
[d1 a-die
d2 d-die]
(if (> d1 d2) "attacker wins"
"defender wins")))
(defn one-v-two [a-die d-die]
(domonad dist-m
[d1 a-die
d2 d-die
d3 d-die]
(if (> d1 d2 d3) "attacker wins"
"defender wins")))
(defn two-v-one [a-die d-die]
(domonad dist-m
[d1 a-die
d2 a-die
d3 d-die]
(if (or (> d1 d3) (> d2 d3)) "attacker wins"
"defender wins")))
(defn two-v-two [a-die d-die]
(domonad dist-m
[d1 a-die
d2 a-die
d3 d-die
d4 d-die]
(cond (or (and (> d1 d3) (> d2 d4))
(and (> d1 d4) (> d2 d3)))
"attacker wins"
(or (and (> d1 d3) (< d2 d4))
(and (> d1 d4) (< d2 d3))
(and (< d1 d3) (> d2 d4))
(and (< d1 d4) (> d2 d3)))
"draw"
1
"defender wins")))
(defn three-v-one [a-die d-die]
(domonad dist-m
[d1 a-die
d2 a-die
d3 a-die
d4 d-die]
(if (or (> d1 d4)
(> d2 d4)
(> d3 d4))
"attacker wins"
"defender wins")))
(defn three-v-two [a-die d-die]
(domonad dist-m
[d1 a-die
d2 a-die
d3 a-die
d4 d-die
d5 d-die]
(cond (or (and (> d1 d4) (> d2 d5))
(and (> d1 d5) (> d2 d4))
(and (> d1 d4) (> d3 d5))
(and (> d1 d5) (> d3 d4))
(and (> d2 d4) (> d3 d5))
(and (> d2 d5) (> d3 d4)))
"attacker wins"
(or (and (> d1 d4) (< d2 d5) (< d3 d5))
(and (> d1 d5) (< d2 d4) (< d3 d4))
(and (> d2 d4) (< d1 d5) (< d1 d5))
(and (> d2 d5) (< d1 d4) (< d3 d4))
(and (> d3 d4) (< d2 d5) (< d1 d5))
(and (> d3 d5) (< d2 d4) (< d1 d4)))
"draw"
1
"defender wins")))