-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (54 loc) · 2.47 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var playerScore = document.getElementById("player_score");
var computerScore = document.getElementById("computer_score");
var playerContainer=document.getElementById("player_container");
var computerContainer=document.getElementById("computer_container");
var playerChoiceImg=document.getElementById("player_choice");
var computerChoiceImg= document.getElementById("computer_choice");
const choices = ["Rock", "Paper", "Scissors"];
const rock = document.getElementById("rock_pic");
const paper = document.getElementById("paper_pic");
const scissors = document.getElementById("scissors_pic");
rock.addEventListener("click", () => playGame(choices[0]));
paper.addEventListener("click", () => playGame(choices[1]));
scissors.addEventListener("click", () => playGame(choices[2]));
const button=document.getElementById("resetButton");
button.onclick=resetScore
function playGame(playerChoice) {
console.log("Player chose: "+ playerChoice)
computerChoice= choices[Math.floor(Math.random() * choices.length)];
computerChoiceImg.src= "Assets/" + computerChoice +".png";
playerChoiceImg.src = "Assets/" + playerChoice +".png";
determineWinner(playerChoice, computerChoice);
}
function determineWinner(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
flashResult(playerContainer,playerScore);
flashResult(computerContainer,computerScore);
} else if ((playerChoice === "Rock" && computerChoice === "Scissors") ||
(playerChoice === "Paper" && computerChoice === "Rock") ||
(playerChoice === "Scissors" && computerChoice === "Paper")) {
flashResult(playerContainer,playerScore);
} else {
flashResult(computerContainer,computerScore);
}
}
function flashResult(container,score){
var originalBackgroundColor="white";
var flashingColor = "green";
var flashDuration = 2000;
var flashInterval = 400;
var flashCount = 0;
var flashIntervalId = setInterval(function () {
container.style.backgroundColor = (flashCount % 2 === 0) ? flashingColor : originalBackgroundColor;
flashCount++;
if (flashCount >= flashDuration / flashInterval) {
clearInterval(flashIntervalId);
container.style.backgroundColor = originalBackgroundColor;
score.textContent=(parseInt(score.textContent) + 1).toString();
}
},flashInterval);
}
function resetScore(){
playerScore.textContent="0";
computerScore.textContent="0";
}