I'm making a pig game. The whole game and code is here: http://cisp362.info/johng/CISP362_Pig/Pig.html
my problem is that the computer player plays so fast you can't see the column color change and all the rolls look like they happen at once. I know setInterval and setTimeout are the only thing I can use to slow it down, but I can't figure out how to rearrange my code to make it work because it's in a loop that depends on the result of the variable thisRoll
. I tried using setTimeout, but it has unpredictable results because the rest of the code keeps running and doesn't wait for thisRoll
to be changed with rollDie()
. I tried using setInterval in place of the do while loop, but I can't figure out how to incorporate the conditional statements and/or how to run the clean up code afterward. I just want it to run slower. Maybe I've just been staring at this for too long.
function takeNPCTurn(){
do{
thisRoll = rollDie();
if(thisRoll===1){
document.getElementById("NPCRolls").innerHTML = "------------------------------------<br>You Rolled a 1 and lost your turn!<br>" + document.getElementById("NPCRolls").innerHTML
localStorage.whosTurnIsIt = "Player";
changeColumnColor('col1','lime');
changeColumnColor('col2','transparent');
runningTotal=0;
return;
}else{
runningTotal += thisRoll;
document.getElementById("NPCRolls").innerHTML = "You Rolled a " + thisRoll + ", totalling " + runningTotal+"<br>" + document.getElementById("NPCRolls").innerHTML;
}
}while(runningTotal < 20 && (parseInt(document.getElementById('NPCScore').textContent) + runningTotal) < 100);
//finish up NPC turn and switch back to player
document.getElementById('NPCScore').textContent = parseInt(document.getElementById('NPCScore').textContent) + runningTotal;
document.getElementById("NPCRolls").innerHTML = "------------------------------------<br>You held at " + runningTotal + ", bringing your score to " + document.getElementById('NPCScore').textContent + "<br>" + document.getElementById("NPCRolls").innerHTML;
runningTotal=0;
localStorage.whosTurnIsIt = "Player";
changeColumnColor('col1','lime');
changeColumnColor('col2','transparent');
if(parseInt(document.getElementById("NPCScore").textContent) >= 100){alert (losingMessage);}
}