From 24b9b03ad2bb0f3fbd5263a373233a79eb7d7a29 Mon Sep 17 00:00:00 2001 From: vignesh Date: Sat, 19 Oct 2024 10:34:54 +0530 Subject: [PATCH] Update button roll script.js --- DiceGame/script.js | 75 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/DiceGame/script.js b/DiceGame/script.js index 2784de25d..e68d80b05 100644 --- a/DiceGame/script.js +++ b/DiceGame/script.js @@ -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.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; + } }