Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update button roll script.js #854

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 59 additions & 16 deletions DiceGame/script.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
var randomNo = Math.floor(Math.random()*6)+1;
var randomDiceImage = "images/dice"+randomNo+".png";
document.addEventListener("DOMContentLoaded", function() {
// Generate random numbers for each dice
var randomNo1 = Math.floor(Math.random() * 6) + 1;
var randomDiceImage1 = "images/dice" + randomNo1 + ".png";
document.querySelector(".img1").setAttribute("src", randomDiceImage1);

var randomNo2 = Math.floor(Math.random() * 6) + 1;
var randomDiceImage2 = "images/dice" + randomNo2 + ".png";
document.querySelector(".img2").setAttribute("src", randomDiceImage2);

// Determine the winner
var winnerDeclaration = document.querySelector("h1");
if (randomNo1 > randomNo2) {
winnerDeclaration.innerHTML = "🚩 Player 1 is the winner!";
} else if (randomNo1 < randomNo2) {
winnerDeclaration.innerHTML = "Player 2 is the winner! 🚩";
} else {
winnerDeclaration.innerHTML = "It's a draw! Try again!";
}
});
let randomNo1 = 0;
let randomNo2 = 0;
let player1Rolled = false;
let player2Rolled = false;

var img1 = document.querySelector(".img1").setAttribute("src",randomDiceImage);
// or// var img1 = document.querySelector(".img1").src=`${randomDiceImage}`;
document.addEventListener("DOMContentLoaded", function() {
// Roll Player 1 dice when button is clicked
document.getElementById("rollPlayer1").addEventListener("click", function() {
randomNo1 = Math.floor(Math.random() * 6) + 1;
const randomDiceImage1 = "images/dice" + randomNo1 + ".png";
document.querySelector(".img1").setAttribute("src", randomDiceImage1);
player1Rolled = true;
checkWinner();
});

var randomNo2 = Math.floor(Math.random()*6)+1;
var randomDiceImage2 = "images/dice"+randomNo2+".png";
var img2 = document.querySelector(".img2").setAttribute("src",randomDiceImage2);
// Roll Player 2 dice when button is clicked
document.getElementById("rollPlayer2").addEventListener("click", function() {
randomNo2 = Math.floor(Math.random() * 6) + 1;
const randomDiceImage2 = "images/dice" + randomNo2 + ".png";
document.querySelector(".img2").setAttribute("src", randomDiceImage2);
player2Rolled = true;
checkWinner();
});
});

var winnerDeclaration = document.querySelector("h1");
if(randomNo>randomNo2){
winnerDeclaration.innerHTML="🚩Player 1 is the winner!"
}
else if(randomNo<randomNo2){
winnerDeclaration.innerText="Player 2 is the winner🚩!"
}
else{
winnerDeclaration.innerText="Try again!"
function checkWinner() {
// Only check the winner when both players have rolled
if (player1Rolled && player2Rolled) {
const winnerDeclaration = document.getElementById("winnerDeclaration");

if (randomNo1 > randomNo2) {
winnerDeclaration.innerHTML = "🚩 Player 1 is the winner!";
} else if (randomNo1 < randomNo2) {
winnerDeclaration.innerHTML = "Player 2 is the winner! 🚩";
} else {
winnerDeclaration.innerHTML = "It's a draw! Try again!";
}

// Reset the game state for the next round
player1Rolled = false;
player2Rolled = false;
}
}