I'm trying to create a simple dice game using JavaScript. I want to simulate a Yahtzee game with 5 dice, and after each dice roll, every dice showing 6 should be put aside and the code should roll the remaining dice until all dice are put aside.
Here's what I've done so far:
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<style>
div.dice{
float:left;
width:32px;
background:#F5F5F5;
border:#999 1px solid;
padding:10px;
font-size:24px;
text-align:center;
margin:5px;
}
</style>
<script>
function rollDice1(){
var die1 = document.getElementById("die1");
var d1 = Math.floor(Math.random() * 6) + 1;
die1.innerHTML = d1;
}
function rollDice2(){
var die2 = document.getElementById("die2");
var d2 = Math.floor(Math.random() * 6) + 1;
die2.innerHTML = d2;
}
function rollDice3(){
var die3 = document.getElementById("die3");
var d3 = Math.floor(Math.random() * 6) + 1;
die3.innerHTML = d3;
var status3 = d3;
}
function rollDice4(){
var die4 = document.getElementById("die4");
var d4 = Math.floor(Math.random() * 6) + 1;
die4.innerHTML = d4;
}
function rollDice5(){
var die5 = document.getElementById("die5");
var d5 = Math.floor(Math.random() * 6) + 1;
die5.innerHTML = d5;
}
function allDice() {
rollDice1(); rollDice2(); rollDice3(); rollDice4(); rollDice5();
}
</script>
</head>
<body>
<div id="die1" class="dice">0</div>
<div id="die2" class="dice">0</div>
<div id="die3" class="dice">0</div>
<div id="die4" class="dice">0</div>
<div id="die5" class="dice">0</div>
<button onclick="allDice()">Roll Dice</button>
</body>
</html>
I've managed to create my 5 dice, and they also get assigned a new value with each roll. However, I've yet to manage to find a way to stop rolling the specific dice that show the value of six.
Ideally I would just be able to keep pressing the button labelled "Roll Dice" until all five dice show the value 6.
I've tried many methods, including putting all 5 roll function into an array and then removing the functions responsible for the specific dice that end up showing 6 after each roll. However, creating arrays seem to somehow affect my previous code. I've gotten error messages such as 'die1' is 'null' or that the function 'allDice' is undefined.
If needed I can add in some of the other versions of the program I've tried making work; I chose not to add them initially as this post felt way too long.
So, I've reached a point where I'm trying to create a function that rolls all 5 dice with each press of the button and I'm then looking for a way to remove each of these functions whenever their respective dice shows 6, or remove the variable that overrides the number shown on the dice from each of the rolling functions. If this is not possible or another solution is more suitable, then please write them below.