-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
115 lines (98 loc) · 2.7 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const game = () => {
let pScore = 0;
let cScore = 0;
// Start Game
const gameStart = () => {
const play = document.querySelector('.play');
const match = document.querySelector('.match');
const intro = document.querySelector('.intro');
play.addEventListener('click', () => {
match.classList.remove('fade-out');
match.classList.add('fade-in');
intro.classList.add('fade-out');
});
};
// Play Match
const playMatch = () => {
const playerOptions = document.querySelectorAll('.options button');
const computerOptions = ['rock', 'paper', 'scissors'];
const playerHand = document.querySelector('.player-hand');
const computerHand = document.querySelector('.computer-hand');
const hands = document.querySelectorAll('.hands img');
hands.forEach((hand) => {
hand.addEventListener('animationend', function () {
this.style.animation = '';
});
});
playerOptions.forEach((option) => {
option.addEventListener('click', function () {
const randomNo = Math.floor(Math.random() * 3);
const computerChoice = computerOptions[randomNo];
setTimeout(() => {
compareHands(this.textContent, computerChoice);
playerHand.src = `./assets/${this.textContent}.png`;
computerHand.src = `./assets/${computerChoice}.png`;
}, 2000);
playerHand.style.animation = 'shakePlayer 2s ease';
computerHand.style.animation = 'shakeComputer 2s ease';
});
});
};
const updateScore = () => {
const playerScore = document.querySelector('.player-score');
const computerScore = document.querySelector('.computer-score');
playerScore.textContent = pScore;
computerScore.textContent = cScore;
};
const compareHands = (playerChoice, computerChoice) => {
const winner = document.querySelector('.winner');
console.log(computerChoice);
if (playerChoice === computerChoice) {
winner.textContent = 'Its a tie';
return;
}
if (playerChoice === 'rock') {
if (computerChoice === 'paper') {
winner.textContent = 'Player Wins';
pScore++;
updateScore();
return;
} else {
winner.textContent = 'Computer Wins';
cScore++;
updateScore();
return;
}
}
if (playerChoice === 'paper') {
if (computerChoice === 'scissors') {
winner.textContent = 'Computer Wins';
cScore++;
updateScore();
return;
} else {
winner.textContent = 'Player Wins';
pScore++;
updateScore();
return;
}
}
if (playerChoice === 'scissors') {
if (computerChoice === 'rock') {
winner.textContent = 'Computer Wins';
cScore++;
updateScore();
return;
} else {
winner.textContent = 'Player Wins';
pScore++;
updateScore();
return;
}
}
};
// Functions call
gameStart();
playMatch();
};
game();