JSFiddle - React, Tailwind, and code Playground
HTML
<body>
Die A: <input type="text" id="die_a" length="300px" value="-10,8,14,15,16,17" /><br />
Die B: <input type="text" id="die_b" length="300px" value="6,9,9,11,11,14" /><br />
Simulations: <input type="number" id="sim_count" min=1 step=1 value=1000 />
<br />
<input type="button" onclick="runTest()" value="Simulate!" />
<pre id="results">
</pre>
</body>
JavaScript
function runTest() {
// Clear the result field
$('#results').empty();
// Load the text fields by splitting the numbers on commas and converting the values to integers
var die_a = $('#die_a').val().split(',').map(function(i) { return parseInt(i, 10); });
var die_b = $('#die_b').val().split(',').map(function(i) { return parseInt(i, 10); });
// Verify that the dice are of the correct number of sides
var a_len = die_a.length;
var b_len = die_b.length;
if (a_len != b_len || a_len != 6 || b_len != 6) {
$('#results').append("Error: both dice must have exactly 6 sides.\n");
$('#results').append("Die A has " + a_len + " sides\n");
$('#results').append("Die B has " + b_len + " sides\n");
return;
}
// Verify that the dice have the correct side sum
var a_sum = 0, b_sum = 0, i;
for (i = 0; i < 6; i++) {
a_sum += die_a[i];
b_sum += die_b[i];
}
if (a_sum != 60 || b_sum != 60) {
$('#results').append("Error: both dice must have a sum of 60.\n");
$('#results').append("Die A has a sum of " + a_sum + "\n");
$('#results').append("Die B has a sum of " + b_sum + "\n");
return;
}
// Verify that the side values are between -10 <= X <= 100
for (i = 0; i < 6; i++) {
if (die_a[i] < -10 || die_a[i] > 100 || die_b[i] < -10 || die_b[i] > 100) {
$('#results').append("Error: die side values must be between -10 and 100 inclusive\n");
return;
}
}
// The dice look good, time to simulate them against each other
compareDice(die_a, die_b);
}
// Compare all 36 equally possible outcomes
// from throwing both dice
function compareDice(die_a, die_b) {
var a_score = 0, b_score = 0;
var i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 6; j++) {
// If tied, neither gets points
if (die_a[i]...