Commit 19ad9008 authored by Hussain Khalil's avatar Hussain Khalil

Finish script

parent 95db9ca1
<!DOCTYPE html>
<html>
<head>
<title>
ChocoSim
</title>
</head>
<body>
<head>
<title>
ChocoSim
</title>
<style type="text/css">
body, html {
margin: 0;
overflow: none;
padding: 0;
width: 100%;
}
.main {
margin: 20pt;
}
input[type="number"] {
max-width: 50pt;
}
#results {
background-color: #cacaca;
margin: 20pt 0pt;
padding: 20pt;
}
</style>
</head>
<body>
<div class="main">
<h1>ChocoSim</h1>
Number of other players: <input type="number" value="5" id="numplayers"><br>
Range: <input type="number" value="0" id="rangemin"> - <input type="number" value="100" id="rangemax"><br>
Number of simulations: <input type="number" value="10000" id="numsims"><br>
<input type="button" value="Launch Simulations!" id="simulate">
<div id="results"></div>
<script type="text/javascript">
var $ = (id) => {
return document.getElementById(id);
},
randRange = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
results = $("results");
$("simulate").onclick = () => {
var others = $("numplayers").value,
range = {
min: $("rangemin").value,
max: $("rangemax").value
},
numSims = $("numsims").value;
results.innerHTML = "Simulating...";
var resultObj = {};
for(var i = range.min; i <= range.max; i++) {
var lost = 0;
for(var i2 = 0; i2 < numSims; i2++) {
var correct = randRange(range.min, range.max),
diff = Math.abs(correct - i),
used = [i2];
for(var i3 = 0; i3 < others; i3++) {
var playerGuess = randRange(range.min, range.max);
if(used.indexOf(playerGuess) !== -1) {
i3--;
continue;
}
used.push(playerGuess);
if(Math.abs(correct - playerGuess) < diff) {
lost++;
break;
}
}
<div id="results">Waiting for user input...</div>
</div>
<script type="text/javascript">
var $ = (id) => {
return document.getElementById(id);
},
randRange = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
results = $("results");
$("simulate").onclick = () => {
var timerStart = Date.now(),
others = $("numplayers").value,
range = {
min: $("rangemin").value,
max: $("rangemax").value
},
numSims = $("numsims").value;
results.innerHTML = "Simulating...";
setTimeout(()=>{
var resultObj = {};
for(var i = range.min; i <= range.max; i++) {
var lost = 0;
for(var i2 = 0; i2 < numSims; i2++) {
var correct = randRange(range.min, range.max),
diff = Math.abs(correct - i),
used = [i2];
for(var i3 = 0; i3 < others; i3++) {
var playerGuess = randRange(range.min, range.max);
if(used.indexOf(playerGuess) !== -1) {
i3--;
continue;
}
resultObj[i] = 100 - ((lost / numSims) * 100);
}
// Sort and return the result
var sorted = [];
for(var test in resultObj) {
if(!sorted.length) {
sorted.push([test, resultObj[test]]);
continue;
}
for(var i4 = 0; i4 < sorted.length; i4++) {
if(sorted[i4][1] < resultObj[test]) {
sorted.splice(i4, 0, [test, resultObj[test]]);
break;
}
if(i4 === (sorted.length - 1)) {
sorted.push([test, resultObj[test]]);
}
}
results.innerHTML = "RESULTS:<br>";
for(var i5 = 0; i5 < sorted.length; i5++) {
results.innerHTML += (i5 + 1) + ": " + sorted[i5][0] + "<br>&nbsp; Won " + sorted[i5][1] + " percent of the simulations.<br>";
used.push(playerGuess);
if(Math.abs(correct - playerGuess) < diff) {
lost++;
break;
}
}
}
resultObj[i] = 100 - ((lost / numSims) * 100);
}
// Sort and return the result
var sorted = [];
for(var test in resultObj) {
if(!sorted.length) {
sorted.push([test, resultObj[test]]);
continue;
}
for(var i4 = 0; i4 < sorted.length; i4++) {
if(sorted[i4][1] < resultObj[test]) {
sorted.splice(i4, 0, [test, resultObj[test]]);
break;
}
if(i4 === (sorted.length - 1)) {
sorted.push([test, resultObj[test]]);
break;
}
}
results.innerHTML = "<b><i>Results</b></i> (" + (Date.now() - timerStart) / 1000 + " seconds)<br><br><br>";
for(var i5 = 0; i5 < sorted.length; i5++) {
results.innerHTML += (i5 + 1) + ": " + sorted[i5][0] + "<br>&nbsp; Won " + sorted[i5][1] + " percent of the simulations.<br><br>";
}
};
</script>
</body>
</html>
\ No newline at end of file
}
}, 0);
};
</script>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment