|
| 1 | +// Selected buttons and icons |
| 2 | +const rockBtn = document.querySelector('.rock-btn'); |
| 3 | +const paperBtn = document.querySelector('.paper-btn'); |
| 4 | +const scissorBtn = document.querySelector('.scissors-btn'); |
| 5 | +const userHandIcon = document.querySelector('.user.hand-icon'); |
| 6 | +const computerHandIcon = document.querySelector('.computer.hand-icon'); |
| 7 | +const result = document.querySelector('.result'); |
| 8 | +const userScore = document.querySelector('.user-score'); |
| 9 | +const computerScore = document.querySelector('.computer-score'); |
| 10 | +const handsIcons = document.querySelectorAll('.hand-icon'); |
| 11 | +const gameButtons = document.querySelectorAll( |
| 12 | + '.rock-btn, .paper-btn, .scissors-btn' |
| 13 | +); |
| 14 | + |
| 15 | +// PopupOverlay |
| 16 | +const popupOverlay = document.querySelector('.popup-overlay'); |
| 17 | +const popupTitle = document.querySelector('.popup-title'); |
| 18 | +const popupCloseBtn = document.querySelector('.popup-close-btn'); |
| 19 | + |
| 20 | +// Audio Elements |
| 21 | +const clickSound = document.querySelector('.click-sound'); |
| 22 | +const shakeSound = document.querySelector('.shake-sound'); |
| 23 | +const winSound = document.querySelector('.win-sound'); |
| 24 | +const loseSound = document.querySelector('.lose-sound'); |
| 25 | + |
| 26 | +// Icons |
| 27 | +const rockIcon = '✊'; |
| 28 | +const paperIcon = '🤚'; |
| 29 | +const scissorIcon = '✌️'; |
| 30 | + |
| 31 | +// play sound |
| 32 | +function playSound(sound) { |
| 33 | + sound.currentTime = 0; // Reset to start so it can |
| 34 | + sound.play(); // Play the sound |
| 35 | +} |
| 36 | + |
| 37 | +// Computer Choice Array |
| 38 | +const iconList = [rockIcon, paperIcon, scissorIcon]; |
| 39 | + |
| 40 | +function calculateResult(selectionIcon, winningIcon) { |
| 41 | + userHandIcon.innerText = '🤜'; |
| 42 | + computerHandIcon.innerText = '🤛'; |
| 43 | + result.innerText = '🙄'; |
| 44 | + |
| 45 | + //start shake animation |
| 46 | + userHandIcon.classList.add('shakeUserHands'); |
| 47 | + computerHandIcon.classList.add('shakeComputerHands'); |
| 48 | + |
| 49 | + setTimeout(() => { |
| 50 | + userHandIcon.classList.remove('shakeUserHands'); |
| 51 | + computerHandIcon.classList.remove('shakeComputerHands'); |
| 52 | + |
| 53 | + // show user choice |
| 54 | + userHandIcon.innerText = selectionIcon; |
| 55 | + const computerChoice = Math.floor(Math.random() * 3); |
| 56 | + computerHandIcon.innerText = iconList[computerChoice]; |
| 57 | + |
| 58 | + if (computerHandIcon.innerText === userHandIcon.innerText) { |
| 59 | + result.innerText = 'Draw'; |
| 60 | + } else if (computerHandIcon.innerText === winningIcon) { |
| 61 | + result.innerText = 'You won!!'; |
| 62 | + userScore.innerText = parseInt(userScore.innerText) + 1; |
| 63 | + } else { |
| 64 | + result.innerText = 'Sandeep Won!!'; |
| 65 | + computerScore.innerText = parseInt(computerScore.innerText) + 1; |
| 66 | + } |
| 67 | + |
| 68 | + checkScore(); |
| 69 | + }, 3000); |
| 70 | +} |
| 71 | + |
| 72 | +// check score |
| 73 | +function checkScore() { |
| 74 | + const userScoreValue = parseInt(userScore.textContent); |
| 75 | + const computerScoreValue = parseInt(computerScore.textContent); |
| 76 | + if (userScoreValue === 3) { |
| 77 | + // show popup |
| 78 | + showPopup('You Wins! 🎉'); |
| 79 | + playSound(winSound); |
| 80 | + } else if (computerScoreValue === 3) { |
| 81 | + // show popup |
| 82 | + showPopup('Sandeep Wins! 😒'); |
| 83 | + playSound(loseSound); |
| 84 | + } |
| 85 | +} |
| 86 | +function showPopup(message) { |
| 87 | + popupTitle.textContent = message; |
| 88 | + popupOverlay.classList.add('active'); |
| 89 | +} |
| 90 | +function hidePopup() { |
| 91 | + popupOverlay.classList.remove('active'); |
| 92 | +} |
| 93 | +function resetGame() { |
| 94 | + userScore.textContent = '0'; |
| 95 | + computerScore.textContent = '0'; |
| 96 | + hidePopup(); |
| 97 | +} |
| 98 | +popupCloseBtn.addEventListener('click', resetGame); |
| 99 | + |
| 100 | +handsIcons.forEach((hand) => { |
| 101 | + hand.addEventListener('animationstart', () => { |
| 102 | + playSound(shakeSound); // Play shake sound |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +gameButtons.forEach((btn) => { |
| 107 | + btn.addEventListener('click', () => { |
| 108 | + playSound(clickSound); // Play click sound |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +rockBtn.addEventListener('click', () => { |
| 113 | + calculateResult(rockIcon, scissorIcon); |
| 114 | +}); |
| 115 | +paperBtn.addEventListener('click', () => { |
| 116 | + calculateResult(paperIcon, rockIcon); |
| 117 | +}); |
| 118 | +scissorBtn.addEventListener('click', () => { |
| 119 | + calculateResult(scissorIcon, paperIcon); |
| 120 | +}); |
0 commit comments