In: Computer Science
I am currently working on creating a dice game. I have not figured out how to make it work? What should I do to make it work?
Here is what I have so far:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Dice Game</title>
<link rel="stylesheet" type="text/css" href="dice.css">
</head>
<body>
<div class="row" align="center">
<div class="col-4">
<h3>Your Dice</h3>
<img src="dice images/m1.png" width="100" height="100"
alt="roll: 1" id="mydice1"/>
<img src="dice images/m1.png" width="100" height="100"
alt="roll: 1" id="mydice2"/>
</div>
<div class="col-4">
<h3>Opponent's Dice</h3>
<img src="dice images/o1.png" width="100" height="100"
alt="roll: 1" id="opdice1"/>
<img src="dice images/o1.png" width="100" height="100"
alt="roll: 1" id="opdice2"/>
</div>
<div class="col-4">
<img src="dice images/goodluck.png" width="150" height="150"
alt="Good Luck" id="message"/>
<button class="roll" onlick="throwdice()">Roll
Dice</button>
</div>
</div>
<script>
function throwdice () {
// Create Random Number
var rand1 = Math.round(Math.random()*5) + 1;
var rand2 = Math.round(Math.random()*5) + 1;
var rand3 = Math.round(Math.random()*5) + 1;
var rand4 = Math.round(Math.random()*5) + 1;
// Set Images scr
document.getElementById("mydice1").src = "images/m" + rand1 +
".png";
document.getElementById("mydice2").src = "images/m" + rand2 +
".png";
getElementById("opdice1").src = "images/o" + rand3 + ".png";
document.getElementById("opdice2").src = "images/o" + rand4 +
".png";
//Set Images alt
var side_alt = ["roll:1", "roll: 2", "roll: 3", "roll: 4", "roll:
5", "roll: 6",
document.getElementById('mydice1').alt = side_alt[rand1-1];
document.getElementById('mydice2').alt = side_alt[rand2-1];
document.getElementById('opdice1').alt = side_alt[rand3-1];
document.getElementById('opdice2').alt = side_alt[rand4-1];
}
</script>
</body>
</html>
Here is the code you can try running for dice game.
Steps to run the code :
index.html
<html>
<head>
<meta name='viewport' content='width=device-width,
initial-scale=1.0'>
<meta charset="UTF-8">
<title>Dice Game</title>
<link rel="stylesheet" type="text/css"
href="style.css">
</head>
<body>
<div class="row" align="center">
<div class="col-4">
<h3>Your Dice</h3>
<img src="images/d1.png" id="mydice1">
<img src="images/d1.png" id="mydice2">
</div>
<div class="col-4">
<h3>Enemy's Dice</h3>
<img src="images/e1.png" id="hisdice1">
<img src="images/e1.png" id="hisdice2">
<div id="enemy_score"></div>
</div>
<div class="col-4" align="center">
<p id="message"> Good Luck!</p>
<button class="greenBut"
onClick="throwdice()">Play!</button>
</form>
</div>
</div>
<script>
// var sides = ["d1.png", "d2.png", "d3.png", "d4.png", "d5.png",
"d6.png"];
var side_alt = ["roll: 1", "roll: 2", "roll: 3", "roll: 4", "roll:
5", "roll: 6"];
function throwdice(){
//create a random integer between 1 and 6
var rand1 = Math.round(Math.random()*5) + 1;
var rand2 = Math.round(Math.random()*5) + 1;
var rand3 = Math.round(Math.random()*5) + 1;
var rand4 = Math.round(Math.random()*5) + 1;
// Set Image src
document.getElementById("mydice1").src = "images/d" + rand1 +
".png";
document.getElementById("mydice2").src = "images/d" + rand2 +
".png";
document.getElementById("hisdice1").src = "images/e" + rand3 +
".png";
document.getElementById("hisdice2").src = "images/e" + rand4 +
".png";
// Set Image alt
document.getElementById("mydice1").alt = side_alt[rand1];
document.getElementById("mydice2").alt = side_alt[rand2];
document.getElementById("hisdice1").alt = side_alt[rand3];
document.getElementById("hisdice2").alt = side_alt[rand4];
who_won(rand1,rand2,rand3,rand4);
}
function who_won(rand1,rand2,rand3,rand4){
let player_points = rand1 + rand2 + 2;
let enemy_points = rand3 + rand4 + 2;
let result = winner(player_points,enemy_points);
document.getElementById("message").innerHTML = result;
}
function winner(player, enemy) {
if (player < enemy) {
return "looser";
}
if (enemy < player) {
return "winner"
}
if (player == enemy) {
return "equal"
}
}
</script>
</body>
</html>
body {
background-color: lightblue;
color: darkblue;
}
/* All elements width includes borders and padding*/
* {
box-sizing: border-box;
}
/* Rows*/
.row::after {
content: "";
clear: both;
display: table;
}
/* Button */
.greenBut {
color: darkblue;
padding: 12px; /* Some padding */
background-color: yellow;
font-size: 24px;
border: 1px solid #ccc; /* Gray border */
border-radius: 6px; /* Rounded borders */
margin: 10px;
}
img {
max-width: 100%;
margin: 10px;
}
button:hover {
opacity: .5;
}
Save both the files and run the file index.html using a web browser
Below is the output of the code.