A simple JavaScript dice game where two players roll dice and the higher number wins.
- Open the
index.html
file in your web browser. - The left side of the page displays the image of the dice rolled by Player 1, and the right side displays the image of the dice rolled by Player 2.
- The game will automatically determine the winner or declare a draw based on the dice rolls.
- The result will be displayed at the top of the page, indicating which player won or if it was a draw.
The game is implemented using JavaScript. Here's a brief overview of the code:
// Generate a random number between 1 and 6 for Player 1's dice roll
var randomNumber = Math.floor(Math.random() * 6 + 1);
var randomSourceImage = "images/dice" + randomNumber + ".png";
document.getElementsByClassName("img1")[0].setAttribute("src", randomSourceImage);
// Generate a random number between 1 and 6 for Player 2's dice roll
var randomNumber1 = Math.floor(Math.random() * 6 + 1);
var randomSourceImage = "images/dice" + randomNumber1 + ".png";
document.getElementsByTagName("img")[1].setAttribute("src", randomSourceImage);
// Determine the winner or declare a draw based on the dice rolls
var heading = document.querySelector("h1");
if (randomNumber > randomNumber1) {
heading.innerHTML = "🚩 Player 1 Wins";
} else if (randomNumber1 > randomNumber) {
heading.innerHTML = "Player 2 Wins 🚩";
} else {
heading.innerHTML = "Draw!";
}