//Created by Eric Ovaska on June 9, 2003
//Craps simulation for the AiS Challenge Summer Teacher Institute

// Preloading the (6) die images ...

one = new Image();
one.src = 'images/1.gif';
two = new Image();
two.src = 'images/2.gif';
three = new Image();
three.src = 'images/3.gif';
four = new Image();
four.src = 'images/4.gif';
five = new Image();
five.src = 'images/5.gif';
six = new Image();
six.src = 'images/6.gif';

function shakeDice(roll) {

  if (roll == 'Start Over') {    // Reset the game ...
    window.document.die1.src = 'images/blank.gif'; 
    window.document.die2.src = 'images/blank.gif'; 
    window.document.point.src = 'images/blank.gif'; 
    window.document.results.src = 'images/blank2.gif'; 
    window.document.crapsForm.rollButton.value = 'Roll Dice!';
    return;
  }

  else {
    window.document.results.src = 'images/blank2.gif';
    stop = setInterval(" die1 = Math.ceil(Math.random() * 6); window.document.die1.src = 'images/' + die1 + '.gif'; die2 = Math.ceil(Math.random() * 6); window.document.die2.src = 'images/' + die2 + '.gif';", 10); // stop is global unless you use var ...
    rolling = roll; // make roll global so that setTimeout can see it!
    setTimeout('clearInterval(stop); setTimeout("finalRoll(rolling);", 50);', 1000); // have to wait on the final roll at least 10 ms for Interval to stop
  }

}

function finalRoll(firstOrPoint) {
  // have to do one final roll or pics might not match actual values from shake
  die1 = Math.ceil(Math.random() * 6);
  window.document.die1.src = 'images/' + die1 + '.gif';
  die2 = Math.ceil(Math.random() * 6);
  window.document.die2.src = 'images/' + die2 + '.gif';
  sum = die1 + die2;

  if (firstOrPoint == 'Roll Dice!') {    // Initial roll of the dice ...
    if ((sum == 7) || (sum ==11)) {
      window.document.results.src = 'images/youwin.gif';
      window.document.crapsForm.rollButton.value = 'Start Over';
      return;
    }
  
    else if ((sum == 2) || (sum == 3) || (sum ==12)) {
      window.document.results.src = 'images/youlose.gif';
      window.document.crapsForm.rollButton.value = 'Start Over';
      return;
    }

    else {
      point = sum; // note that point is global ... no "var"
      window.document.point.src = 'images/' + point + '.gif'; 
      window.document.results.src = 'images/keeprolling.gif';
      window.document.crapsForm.rollButton.value = 'Roll Again';
      return;
    }
  }

  if (firstOrPoint == 'Roll Again') {     // Going for point ...
    if (sum == point) {
      window.document.results.src = 'images/youwin.gif';
      window.document.crapsForm.rollButton.value = 'Start Over';
      return;
    }
    else if (sum == 7) {
      window.document.results.src = 'images/youlose.gif';
      window.document.crapsForm.rollButton.value = 'Start Over';
      return;
    }
    else {
      window.document.results.src = 'images/keeprolling.gif';
      return;
    }
  }
}

