-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamescript.js
48 lines (38 loc) · 2 KB
/
gamescript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const gameContainer = document.querySelector(".game_box");
const resultField = document.querySelector('.result');
const userResult = document.querySelector('.user_result');
const cpuResult = document.querySelector('.cpu_result');
const optionImages = document.querySelectorAll('.option_image');
const choices = ['rock', 'paper', 'scissors'];
function getComputerChoice() {
const randomIndex = Math.floor(Math.random() * choices.length);
return choices[randomIndex];
}
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'Tie!';
} else if (
(userChoice === 'rock' && computerChoice === 'scissors') ||
(userChoice === 'paper' && computerChoice === 'rock') ||
(userChoice === 'scissors' && computerChoice === 'paper')
) {
return 'You Win!';
} else {
return 'You Lose!';
}
}
function handleOptionClick(event) {
const userChoice = event.target.nextElementSibling.textContent.toLowerCase();
const computerChoice = getComputerChoice();
userResult.innerHTML = `<i class="fa-regular fa-hand-${userChoice} fa-rotate-90 fa-2xl" style="color: #ff1c2f;"></i>`;
cpuResult.innerHTML = `<i class="fa-regular fa-hand-${computerChoice} fa-rotate-270 fa-2xl" style="color: #ff1c2f;"></i>`;
userResult.innerHTML = userChoice === 'scissors'
? `<i class="fa-regular fa-hand-scissors fa-rotate-180 fa-2xl" style="color: #ff1c2f;"></i>`
: `<i class="fa-regular fa-hand-${userChoice} fa-rotate-90 fa-2xl" style="color: #ff1c2f;"></i>`;
cpuResult.innerHTML = computerChoice === 'scissors'
? `<i class="fa-regular fa-hand-scissors fa-rotate-360 fa-2xl" style="color: #ff1c2f;"></i>`
: `<i class="fa-regular fa-hand-${computerChoice} fa-rotate-270 fa-2xl" style="color: #ff1c2f;"></i>`;
const winner = determineWinner(userChoice, computerChoice);
resultField.textContent = winner;
}
optionImages.forEach(optionImage => optionImage.addEventListener('click', handleOptionClick));