311 lines
10 KiB
HTML
311 lines
10 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<meta name="author" content="Felix W. Dekker" />
|
|
<meta name="application-name" content="Dice probabilities" />
|
|
<meta name="description" content="Calculates the probability of rolling a value given a combination of dice." />
|
|
<meta name="theme-color" content="#0033cc" />
|
|
|
|
<title>Dice probabilities | FWDekker</title>
|
|
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"
|
|
crossorigin="anonymous" />
|
|
<link rel="stylesheet" href="https://static.fwdekker.com/css/milligram-bundle.min.css" crossorigin="anonymous" />
|
|
</head>
|
|
<body>
|
|
<main class="wrapper">
|
|
<!-- Header -->
|
|
<header class="header">
|
|
<section class="container">
|
|
<h1>Dice probabilities</h1>
|
|
<noscript>
|
|
<span style="color: red; font-weight: bold;">
|
|
This website does not function if JavaScript is disabled.
|
|
Please check the <a href="https://www.enable-javascript.com/">
|
|
instructions on how to enable JavaScript in your web browser</a>.
|
|
</span>
|
|
</noscript>
|
|
<blockquote>
|
|
<p><em>Calculates the probability of rolling a value given a combination of dice.</em></p>
|
|
</blockquote>
|
|
</section>
|
|
</header>
|
|
|
|
|
|
<!-- Input -->
|
|
<section class="container">
|
|
<div class="row">
|
|
<div class="column">
|
|
<form>
|
|
<fieldset>
|
|
<table id="dieSettings">
|
|
<thead>
|
|
<tr>
|
|
<th>Sides per die</th>
|
|
<th>Number of rolls</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>
|
|
<button id="addDieRowButton" class="button-outline" type="button">Add dice
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<button id="submit" type="button">Recalculate</button>
|
|
</fieldset>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<!-- Output -->
|
|
<section class="container">
|
|
<h2>Probabilities</h2>
|
|
<div class="row">
|
|
<div class="column">
|
|
<canvas id="probChart"></canvas>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<!-- Footer -->
|
|
<footer class="footer">
|
|
<section class="container">
|
|
Made by <a href="https://fwdekker.com/">Felix W. Dekker</a>.
|
|
Licensed under the
|
|
<a href="https://git.fwdekker.com/FWDekker/dice/src/branch/master/LICENSE">MIT License</a>.
|
|
Source code available on <a href="https://git.fwdekker.com/FWDekker/dice/">git</a>.
|
|
|
|
<div style="float: right;">v1.0.8</div>
|
|
</section>
|
|
</footer>
|
|
</main>
|
|
|
|
|
|
<!-- Scripts -->
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"
|
|
integrity="sha256-oSgtFCCmHWRPQ/JmR4OoZ3Xke1Pw4v50uh6pLcu+fIc=" crossorigin="anonymous"></script>
|
|
<script src="https://static.fwdekker.com/js/common.js" crossorigin="anonymous"></script>
|
|
<script>
|
|
//////
|
|
///
|
|
/// Helper functions
|
|
///
|
|
//////
|
|
|
|
const repeat = (value, length) => {
|
|
const zeroArray = [];
|
|
for (let i = 0; i < length; i++)
|
|
zeroArray.push(value);
|
|
return zeroArray;
|
|
};
|
|
|
|
const rangeExclusive = (from, to) => {
|
|
const rangeArray = [];
|
|
for (let i = from; i < to; i++)
|
|
rangeArray.push(i);
|
|
return rangeArray;
|
|
};
|
|
|
|
const rangeInclusive = (from, to) => {
|
|
const rangeArray = rangeExclusive(from, to);
|
|
rangeArray.push(to);
|
|
return rangeArray;
|
|
};
|
|
|
|
const iterateNodeList = (nodeList, fun) => {
|
|
for (let i = 0; i < nodeList.length; i++) {
|
|
const node = nodeList.item(i);
|
|
fun(node);
|
|
}
|
|
};
|
|
|
|
|
|
//////
|
|
///
|
|
/// Input
|
|
///
|
|
//////
|
|
|
|
const inputTable = {};
|
|
|
|
|
|
// Functions
|
|
inputTable.getTable = () => $("#dieSettings tbody");
|
|
|
|
inputTable.dieRowCount = () => inputTable.getTable().querySelectorAll(".dieEyes").length;
|
|
|
|
inputTable.highestDieRowIndex = () => {
|
|
const table = inputTable.getTable();
|
|
|
|
let highestDieRowIndex = -1;
|
|
iterateNodeList(table.getElementsByTagName("tr"), (node) => {
|
|
if ("index" in node.dataset)
|
|
highestDieRowIndex = Math.max(highestDieRowIndex, +node.dataset.index);
|
|
});
|
|
|
|
return highestDieRowIndex;
|
|
};
|
|
|
|
inputTable.addDieRow = () => {
|
|
const createNumberInput = (index, className, value) => {
|
|
const input = document.createElement("input");
|
|
input.id = className + index;
|
|
input.className = className;
|
|
input.type = "number";
|
|
input.min = "1";
|
|
input.step = "1";
|
|
input.value = value;
|
|
input.addEventListener("keypress", (e) => {
|
|
if (e.key === "Enter")
|
|
outputChart.updateProbGraph();
|
|
});
|
|
input.focus();
|
|
return input;
|
|
};
|
|
|
|
const createRemoveLink = (index, className) => {
|
|
const link = document.createElement("button");
|
|
link.id = className + index;
|
|
link.className = className + " button-clear";
|
|
link.type = "button";
|
|
link.innerHTML = "Remove";
|
|
link.onclick = (() => inputTable.removeDieRow(index));
|
|
return link;
|
|
};
|
|
|
|
|
|
const table = inputTable.getTable();
|
|
const newIndex = inputTable.highestDieRowIndex() + 1;
|
|
|
|
const row = table.insertRow(inputTable.dieRowCount());
|
|
row.dataset.index = newIndex;
|
|
|
|
row.insertCell().appendChild(createNumberInput(newIndex, "dieEyes", 6));
|
|
row.insertCell().appendChild(createNumberInput(newIndex, "dieCount", 2));
|
|
row.insertCell().appendChild(createRemoveLink(newIndex, "dieRemove"));
|
|
};
|
|
|
|
inputTable.removeDieRow = index => {
|
|
if (inputTable.highestDieRowIndex() > 0) {
|
|
const table = inputTable.getTable();
|
|
const row = table.querySelector("tr[data-index=\"" + index + "\"]");
|
|
row.parentElement.removeChild(row);
|
|
}
|
|
};
|
|
|
|
inputTable.getDice = () => {
|
|
const dice = [];
|
|
|
|
const eyesInputs = document.getElementsByClassName("dieEyes");
|
|
const countInputs = document.getElementsByClassName("dieCount");
|
|
for (let i = 0; i < eyesInputs.length; i++) {
|
|
const count = parseInt(countInputs.item(i).value);
|
|
|
|
for (let j = 0; j < count; j++)
|
|
dice.push(parseInt(eyesInputs.item(i).value));
|
|
}
|
|
|
|
return dice;
|
|
};
|
|
|
|
|
|
// Handlers
|
|
$("#addDieRowButton").onclick = inputTable.addDieRow;
|
|
|
|
|
|
// Init
|
|
doAfterLoad(() => $("#addDieRowButton").click());
|
|
|
|
|
|
//////
|
|
///
|
|
/// Output
|
|
///
|
|
//////
|
|
|
|
const outputChart = {};
|
|
|
|
|
|
// Functions
|
|
outputChart.calculateDiceFrequencies = dice => {
|
|
if (dice.length === 0)
|
|
return [];
|
|
|
|
|
|
// Roll dice
|
|
let rollFreqs = [0].concat(repeat(1, dice[0]));
|
|
dice.slice(1).forEach(die => {
|
|
const dieRollFreqs = rollFreqs.concat(repeat(0, die));
|
|
rollFreqs = repeat(0, dieRollFreqs.length);
|
|
|
|
rangeInclusive(1, die).forEach(rollValue => {
|
|
rangeExclusive(1, dieRollFreqs.length - die).forEach(i => {
|
|
rollFreqs[rollValue + i] += dieRollFreqs[i];
|
|
});
|
|
});
|
|
});
|
|
rollFreqs.shift();
|
|
|
|
|
|
// Calculate frequencies
|
|
const totalRolls = rollFreqs.reduce((a, b) => a + b, 0);
|
|
rangeExclusive(0, rollFreqs.length).forEach(roll => {
|
|
rollFreqs[roll] = rollFreqs[roll] / totalRolls;
|
|
});
|
|
|
|
|
|
return rollFreqs;
|
|
};
|
|
|
|
outputChart.updateProbGraph = () => {
|
|
const dice = inputTable.getDice();
|
|
const rollFreqs = outputChart.calculateDiceFrequencies(dice);
|
|
|
|
probChart.data.labels = rangeInclusive(1, rollFreqs.length);
|
|
probChart.data.datasets = [{
|
|
data: rollFreqs,
|
|
backgroundColor: "rgb(0, 51, 204, 0.4)"
|
|
}];
|
|
probChart.update();
|
|
};
|
|
|
|
|
|
// Handlers
|
|
$("#submit").onclick = outputChart.updateProbGraph;
|
|
|
|
|
|
// Init
|
|
const ctx = $("#probChart").getContext("2d");
|
|
const probChart = new Chart(ctx, {
|
|
type: "line",
|
|
data: {},
|
|
options: {
|
|
legend: {
|
|
display: false
|
|
},
|
|
scales: {
|
|
yAxes: [{
|
|
display: true,
|
|
ticks: {
|
|
suggestedMin: 0
|
|
}
|
|
}]
|
|
}
|
|
}
|
|
});
|
|
|
|
doAfterLoad(() => $("#submit").click());
|
|
</script>
|
|
</body>
|
|
</html>
|