<!doctype html>
<html>
<head>
<title>Run for It Dice Game - RJM Programming - March, 2018</title>
<script type='text/javascript'>
// https://www.w3schools.com/charsets/ref_utf_punctuation.asp

// ․ 8228 2024 ONE DOT LEADER
// ⁖ 8278 2056 THREE DOT PUNCTUATION
// ⁘ 8280 2058 FOUR DOT PUNCTUATION
// ⁙ 8281 2059 FIVE DOT PUNCTUATION
// ⁚ 8282 205A TWO DOT PUNCTUATION
// ⁛ 8283 205B FOUR DOT MARK
// …8230 2026 … HORIZONTAL ELLIPSIS

// Thanks to http://www.math.hawaii.edu/~ramsey/Probability/TwoDice.html
var probabilities=[0,0,1,2,3,4,5,6,5,4,3,2,1]; // score will be 7-guessValue
var score=[0,0], goes=[0,0], names=['Player1','Player2'], sels=['',''], stillin=[true,true];
var cactiontd='';
var numplayers=2, khem=0;
var theader='', tgame='';
var d1=7, d2=7, d3=7, d4=7, d5=7, d6=7, ccomma='', lastwinners='';

function andthen() {
khem=0;
document.getElementById('actiontd').innerHTML=cactiontd.replace('Each Player', names[0]);
document.getElementById('tdr').innerHTML='Six Dice Roll';
for (var hem=1; hem<=numplayers; hem++) {
document.getElementById('gplayer' + hem).innerHTML=sels[eval(-1 + hem)];
document.getElementById('gplayer' + hem).value='06';
stillin[eval(-1 + hem)]=true;
lastwinners='';
}
}

function roll() {
var hem=khem;
//for (var hem=0; hem<numplayers; hem++) {

d1=7;
d2=7;
d3=7;
d4=7;
d5=7;
d6=7;
ccomma='';
fixinstone(numplayers);
if (cactiontd == '') {
cactiontd=document.getElementById('actiontd').innerHTML;
document.getElementById('actiontd').innerHTML=cactiontd.replace('Each Player', names[0]);
}
if (hem < numplayers) {
document.getElementById('actiontd').innerHTML='';
postroll(hem);
} else {
khem=0;
}
//}
}

function postroll(hem) {
document.getElementById('actiontd').innerHTML=''; //cactiontd.replace('Each Player', names[hem]);
document.getElementById('tdr').innerHTML='Six Dice Rolls for ' + names[hem] + ' are ';
d1=7;
while (d1 == 7) {
d1=eval(Math.floor(Math.random() * 6) + 1);
}
document.getElementById('actiontd').innerHTML+='<div style="margin-left:40%;">' + document.getElementById('h' + d1).outerHTML.replace(' id="h' + d1 + '"','') + '</div>';
d2=7;
while (d2 == 7) {
d2=eval(Math.floor(Math.random() * 6) + 1);
}
document.getElementById('actiontd').innerHTML+='<div style="margin-left:40%;">' + document.getElementById('h' + d2).outerHTML.replace(' id="h' + d2 + '"','') + '</div>';
//d1+=d2;

d3=7;
while (d3 == 7) {
d3=eval(Math.floor(Math.random() * 6) + 1);
}
document.getElementById('actiontd').innerHTML+='<div style="margin-left:40%;">' + document.getElementById('h' + d3).outerHTML.replace(' id="h' + d3 + '"','') + '</div>';
//d1+=d3;

d4=7;
while (d4 == 7) {
d4=eval(Math.floor(Math.random() * 6) + 1);
}
document.getElementById('actiontd').innerHTML+='<div style="margin-left:40%;">' + document.getElementById('h' + d4).outerHTML.replace(' id="h' + d4 + '"','') + '</div>';
//d1+=d4;

d5=7;
while (d5 == 7) {
d5=eval(Math.floor(Math.random() * 6) + 1);
}
document.getElementById('actiontd').innerHTML+='<div style="margin-left:40%;">' + document.getElementById('h' + d5).outerHTML.replace(' id="h' + d5 + '"','') + '</div>';
//d1+=d5;

d6=7;
while (d6 == 7) {
d6=eval(Math.floor(Math.random() * 6) + 1);
}
document.getElementById('actiontd').innerHTML+='<div style="margin-left:40%;">' + document.getElementById('h' + d6).outerHTML.replace(' id="h' + d6 + '"','') + '</div>';
//d1+=d6;



document.getElementById('tdr').innerHTML+=ccomma + d1;
ccomma=', ';
document.getElementById('tdr').innerHTML+=ccomma + d2;
ccomma=', ';
document.getElementById('tdr').innerHTML+=ccomma + d3;
ccomma=', ';
document.getElementById('tdr').innerHTML+=ccomma + d4;
ccomma=', ';
document.getElementById('tdr').innerHTML+=ccomma + d5;
ccomma=', ';
document.getElementById('tdr').innerHTML+=ccomma + d6;

analyze(hem);
ccomma='';
if (eval(1 + hem) == numplayers) {
setTimeout(andthen, 5000);
} else {
khem++;
setTimeout(ath, 1500);
//document.getElementById('actiontd').innerHTML=cactiontd.replace('Each Player', names[khem]);
//setTimeout(roll, 2000);
}
}

function ath() {
document.getElementById('actiontd').innerHTML=cactiontd.replace('Each Player', names[khem]);
}

function analyze(ih) {
var dd=7, idd, jana, kana, rew='', rews='';
var ana=document.getElementById('tdr').innerHTML.split(' are ')[1].replace(/\ /g,'').split(',');
ana.sort();

var ans=prompt(names[ih] + ", you rolled " + document.getElementById('tdr').innerHTML.split(' are ')[1].replace(/\ /g,'') + " ... can you make any sequences that start with 1,2 (and others longer than that such as 1,2,3 or 1,2,3,4)? Tell me what they are, including the commas.","");
if (ans != null) {
if (ans.replace(/\ /g,'').indexOf('1,2') != -1 && document.getElementById('tdr').innerHTML.split(' are ')[1].indexOf('1') != -1 && document.getElementById('tdr').innerHTML.split(' are ')[1].indexOf('2') != -1) {
//alert("Here with answer of " + ans);
var ansar=ans.replace(/\ /g,'').split(','), allokay=true, thisscore=0;
for (var ia=0; ia<ansar.length; ia++) {
if (document.getElementById('tdr').innerHTML.split(' are ')[1].indexOf(('' + ansar[ia])) == -1) allokay=false;
thisscore+=5;
}
if (allokay) {
goes[ih]++;
score[ih]+=thisscore;
document.getElementById('score' + eval(1 + ih)).innerHTML='Score: ' + score[ih] + '/' + goes[ih];
} else {
goes[ih]++;
document.getElementById('score' + eval(1 + ih)).innerHTML='Score: ' + score[ih] + '/' + goes[ih];
}
} else {
goes[ih]++;
document.getElementById('score' + eval(1 + ih)).innerHTML='Score: ' + score[ih] + '/' + goes[ih];
}
}
}


function checkt() {
if (sels[0] == '') {
sels[0]=document.getElementById('gplayer1').innerHTML;
}
if (sels[1] == '') {
sels[1]=document.getElementById('gplayer2').innerHTML;
}
if (theader == '') {
theader=document.getElementById('header').innerHTML;
}
if (tgame == '') {
tgame=document.getElementById('game').innerHTML;
}
if (cactiontd == '') {
cactiontd=document.getElementById('actiontd').innerHTML;
document.getElementById('actiontd').innerHTML=cactiontd.replace('Each Player', names[0]);
}
}

function namechange(tid,tval) {
names[eval(tid.replace('dname','') - 1)]=tval;
}

function fixinstone(ihow) {
document.getElementById('dnum').innerHTML='' + ihow;
if (numplayers < eval(ihow)) {
numplayers=eval(ihow);
var th='', tg='';
for (var jhow=3; jhow<=numplayers; jhow++) {
score.push(0);
goes.push(0);
stillin.push(true);
sels.push(sels[0]);
names.push('Player' + jhow);
th+="<th></th><th><div onchange='namechange(this.id,this.innerHTML);' id='dname" + jhow + "' contenteditable=true>Player " + jhow + "</div><br>Score</th>";
tg+="<td><select style='display:none;' id='gplayer" + jhow + "'><option value='06'>Choose your runforit value [6] ...</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option></select></td><td><div id='score" + jhow + "'>Score: 0/0</div></td>";
}
document.getElementById('header').innerHTML=theader + th;
document.getElementById('game').innerHTML=tgame.replace('Each Player', names[0]) + tg;
}
}
</script>
<style>
td { text-align: center; vertical-align: top; }
h4 { background-color: lightblue; font-size: 26px; text-align: center; width: 50px; height: 50px; padding: 10px 10px 10px 10px; border: 5px solid black; }
.six { padding-top: 2px; padding-bottom: 18px; }
</style>
</head>
<body onload="checkt();">
<h1>Run for It Dice Game for <div id='dnum' style='display:inline-block;'><select style='display:inline-block;' onchange='fixinstone(this.value);' id='snum'><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option><option value=6>6</option><option value=7>7</option><option value=8>8</option><option value=9>9</option></select></div> Players</h1>
<h3>RJM Programming - March, 2018</h3>
<div style='display:none;'>
<h4 id="h1" class="one">․</h4>
<h4 id="h2" class="two">⁚</h4>
<h4 id="h3" class="three">…</h4>
<h4 id="h4" class="four">⁘</h4>
<h4 id="h5" class="five">⁙</h4>
<h4 id="h6" class="six">…<br>…</h4>
</div>

<table style='width:100%;' border=20 cellpadding=5>
<tr id='header'><th><div id='dname1' contenteditable=true>Player 1</div><br>Score</th><th></th><th id='tdr'>Six Dice Roll</th><th></th><th><div id='dname2' contenteditable=true>Player 2</div><br>Score</th></tr>
<tr id='game'><td><div id='score1'>Score: 0/0</div></td><td><select style='display:none;' id='gplayer1'><option value='06'>Choose your runforit value [6] ...</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option></select></td><td id='actiontd'><input id='mybut' style='width:100%;background-color:yellow;height:150px;border:7px solid red;' type='button' onclick='roll();' value='Roll the Dice (and find 1,2 prefixed sequences) for Each Player'></input></td><td><select style='display:none;' id='gplayer2'><option value='06'>Choose your runforit value [6] ...</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option></select></td><td><div id='score2'>Score: 0/0</div></td></tr>
</table>

</body>
</html>