Hexagonal two-type tiles
[zzandy.git] / dice.html
blobcf742fd2141f76e2a1ef4af4a7a2bd657f0ea43f
1 <!DOCTYPE html>
3 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta charset="utf-8" />
6 <style type="text/css">
7 .plot {
8 height: 500px;
9 display: inline-block;
10 margin: 0 2px;
12 .bar {
13 display:inline-block;
14 margin: 0 1px;
15 width: 1.2em;
16 vertical-align:bottom;
17 background-color: #cb1d0b;
18 font-size:80%;
19 text-align:center;
21 .label {
22 text-align: center;
24 </style>
25 <title></title>
26 </head>
27 <body>
28 <script type="text/javascript">
30 function upsert(arr, key, value) {
32 for (var i = 0; i < arr.length; ++i) {
34 if (arr[i][0] == key) {
35 arr[i][1].push(value);
36 return;
40 arr.push([key, [value]]);
43 function upsert2(arr, key, value, v2) {
45 for (var i = 0; i < arr.length; ++i) {
47 if (arr[i][0] == key) {
48 arr[i][1] += value;
49 arr[i][2].push(v2);
50 return;
54 arr.push([key, value, [v2]]);
57 function getCombinations(numDice, sideValues) {
58 var vals = [];
60 for (var i = 0; i < sideValues.length; ++i) {
61 vals.push([sideValues[i]]);
64 if (numDice > 1) {
65 var result = [];
66 var merge = getCombinations(numDice - 1, sideValues);
68 for (var i = 0; i < vals.length; ++i)
69 for (var j = 0; j < merge.length; ++j)
70 result.push(vals[i].concat(merge[j]));
72 return result;
75 return vals;
78 function max(arr, v) {
79 v = v || function (x) { return x }
80 return arr.map(v).reduce(function (a, b) { return a > b ? a : b }, -Infinity);
83 function min(arr, v) {
84 v = v || function (x) {return x }
85 return arr.map(v).reduce(function (a, b, i) { return a < b ? a : b }, Infinity);
88 function sum(arr) {
89 return arr.reduce(function (a, b) { return a + b });
92 function getProbabilities(numDice, sideValues, numDropLeast, numDropMost)
94 var comb = getCombinations(numDice, sideValues);
95 var prob = Math.pow(1 / sideValues.length, numDice);
96 var xxx = [];
98 comb.forEach(function (x) {
100 var z = x;
102 for (var j = 0; j < numDropLeast; ++j) {
103 var done = false;
104 z = z.filter(function (y) {
106 if (done || y > min(z)) return true;
108 done = true;
109 return false;
113 for (var j = 0; j < numDropMost; ++j) {
114 var done = false;
115 z = z.filter(function (y) {
116 if (done || y < max(z)) return true;
118 done = true;
119 return false;
123 upsert2(xxx, sum(z), prob, x);
127 xxx.sort(function (a, b) { return a[0] - b[0] });
129 xxx.label = numDice + 'd' + sideValues.length;
131 if (numDropLeast > 0 && numDropMost == 0)
132 xxx.label += ' pick ' + (numDice - numDropLeast) + ' best';
134 if (numDropLeast == 0 && numDropMost > 0)
135 xxx.label += ' drop ' + numDropMost + ' best';
137 if (numDropLeast > 0 && numDropMost > 0) {
138 xxx.label += ' drop ' + numDropLeast + ' worst and' + numDropMost + ' best';
142 return xxx;
145 function render()
147 var maxprob = 0;
149 for (var k = 0; k < arguments.length; ++k) {
151 var probabilities = arguments[k];
153 for (var i = 0; i < probabilities.length; ++i) {
154 maxprob = maxprob < probabilities[i][1] ? probabilities[i][1] : maxprob;
156 console.log(probabilities[i][0], probabilities[i][1]/*, probabilities[i][2].reduce(function (a, b) { return a + ' ' + b.join('+') }, '')*/);
161 for (var k = 0; k < arguments.length; ++k) {
163 var probabilities = arguments[k];
165 var e = document.createElement('div');
166 e.className = 'plot';
167 var code = [];
169 for (var i = 0; i < probabilities.length; ++i) {
170 code.push('<div class="bar" style="height: ' + (100 * probabilities[i][1] / maxprob) + '%">' + probabilities[i][0] + '</div>');
173 code.push('<div class="label">' + probabilities.label + '</div>');
174 e.innerHTML = code.join('');
175 document.body.appendChild(e);
181 document.body.appendChild(document.createElement('div'));
186 var numDice = 5;
187 var sideValues = [1, 2, 3, 4, 5, 6];
188 var numDropLeast = 2;
189 var numDropMost = 0;
191 render(getProbabilities(2, sideValues, 0, 0)
192 , getProbabilities(3, sideValues, 1, 0)
193 , getProbabilities(4, sideValues, 2, 0)
194 , getProbabilities(5, sideValues, 3, 0)
195 , getProbabilities(5, sideValues, 2, 1)
198 render(getProbabilities(3, sideValues, 0, 0)
199 , getProbabilities(4, sideValues, 1, 0)
200 , getProbabilities(5, sideValues, 2, 0)
203 </script>
204 </body>
205 </html>