Basic game layout. It has no checks for winners/losers, dice sum or other fancy displays.
Might simplify your convoluted (to me) logic and make it easier to modify and add effects.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Yahtzee Game</title>
</head>
<body>
<h1> Yahtzee Game </h1>
<button onClick="startGame()">New Player</button><p>
<table border="1">
<tr>
<th> <br>Dice <p> Check to save </th>
<th> <img src="" id="die0"> <br> <input type="checkbox" id="cb0"> </th>
<th> <img src="" id="die1"> <br> <input type="checkbox" id="cb1"> </th>
<th> <img src="" id="die2"> <br> <input type="checkbox" id="cb2"> </th>
<th> <img src="" id="die3"> <br> <input type="checkbox" id="cb3"> </th>
<th> <img src="" id="die4"> <br> <input type="checkbox" id="cb4"> </th>
</tr>
</table>
<p>
<button onclick="throwDie()">Next Roll</button>
<script type="text/javascript">
// Extensive modifications from: http://www.webdeveloper.com/forum/newreply.php?do=postreply&t=268019
var baseURL = "http://people.bridgewater.edu/~vcapacci/Classes/2012-Fall-CSCI330/PA%27s/PA2/Dice/";
var dieImg = ['die1.png','die2.png','die3.png','die4.png','die5.png','die6.png'];
var dieNum = [0,0,0,0,0];
var dieValue = [1,2,3,4,5,6];
function startGame() {
dieNum = [0,0,0,0,0];
for (var i=0; i<dieNum.length; i++) { document.getElementById('cb'+i).checked = false; }
throwDie();
}
function throwDie() {
for (var d=0; d<dieNum.length; d++) {
var randthrow = Math.floor((Math.random()*dieValue.length));
if (document.getElementById('cb'+d).checked) { } else { dieNum[d] = randthrow; }
}
var str = '';
for (var d=0; d<dieNum.length; d++) {
var tmp = baseURL+dieImg[dieNum[d]];
document.getElementById('die'+d).src = tmp;
str += document.getElementById('die'+d).src+'\n';
} // alert(str); // alert(dieNum.join(','));
}
</script>
</body>
</html>
Post back if you have questions about set-up or ideas about going forward.
Bookmarks