JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<h1>MH Simulator</h1>
<div id="stubborn-holder">
<h2>Stay</h2>
<div id="stubborn"></div>
</div>
<div id="smart-holder">
<h2>Change</h2>
<div id="smart"></div>
</div>
<div id="controls">Run
<input type='text' value='1000' id='sim-runs'>times
<button id='run-sim'>GO</button>
</div>
</body>
</html>
CSS
#stubborn-holder {
float: left;
}
#smart-holder {
float: right;
}
#controls {
clear: both;
}
JavaScript
// Main MH function
function MH() {
this.reset();
return;
}
MH.prototype.reset = function () {
this.iterations = 0;
this.fail = 0;
this.success = 0;
}
MH.prototype.run = function (actionFunction) {
var doors = 3;
// Choose a random door that wins
var winDoor = Math.floor(Math.random() * doors);
// Ask function to choose a door
var firstChoice = actionFunction('FIRST');
// Reveal a non winning door at random
var reveal = false;
while (reveal === false || reveal == firstChoice || reveal == winDoor) {
reveal = Math.floor(Math.random() * doors);
}
// Tell function about the revealed door, get a new choice from function
var secondChoice = actionFunction('CHANGE', reveal);
this.iterations++;
if (secondChoice == winDoor) {
this.success++;
} else {
this.fail++;
}
return;
}
MH.prototype.status = function () {
return {
iterations: this.iterations,
successes: this.success,
fails: this.fail
};
}
// END Main MH function
// Stubborn function
function StubbornAction() {
var doors = 3;
var choice = Math.floor(Math.random() * doors);
this.action = function (request, revealed) {
if (request == 'FIRST') {
return choice;
} else if (request == 'CHANGE') {
return choice;
}
return;
}
return;
}
// Smarter function
function SmarterAction() {
var doors = 3;
var originalChoice;
this.action = function (request, revealed) {
if (request == 'FIRST') {
originalChoice = Math.floor(Math.random() * doors);
return originalChoice;
} else if (request == 'CHANGE') {
// Return not the original choice, not the open door, but the unopened door
for (var x = 0; x < doors; x++) {
if (x != originalChoice && x != revealed) {
return x;
}
}
}
...