-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
109 lines (96 loc) · 3.84 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
// Decaration of variables
var userScore = 0;
var computerScore = 0;
const userScore_span = document.getElementById("user-score");
const computerScore_span = document.getElementById("computer-score");
const sccoreBoard_div = document.querySelector(".score-board");
const result_p = document.querySelector(".result > p");
const rock_div = document.getElementById("r");
const paper_div = document.getElementById("p");
const scissors_div = document.getElementById("s");
// Creating a random function to generate computer choice
function computerChoice() {
const choices = ['r', 'p', 's'];
return choices[Math.floor(Math.random() * 3)];
}
// Here I have defined 3 function to for win , luse , draw
function win(userInput, compChoice) {
userScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
if (userInput === 'r' && compChoice === 's') {
result_p.innerHTML = `Computer chosed Scissors. You Win ✅🎉`;
}
else if (userInput === 'p' && compChoice === 'r') {
result_p.innerHTML = `Computer chosed Rock. You Win ✅🎉`;
}
else if (userInput === 's' && compChoice === 'p') {
result_p.innerHTML = `Computer chosed Paper. You Win ✅🎉`;
}
// Using a delay of 350ms after showing background green/red/black colour
document.getElementById(userInput).classList.add('win');
setTimeout(function () { document.getElementById(userInput).classList.remove('win'); }, 350);
}
function Lose(userInput, compChoice) {
computerScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
if (userInput === 'r' && compChoice === 'p') {
result_p.innerHTML = "Computer chosed Paper 📜 . You Lost ❌";
}
else if (userInput === 'p' && compChoice === 's') {
result_p.innerHTML = `Computer chosed Scissors ✂ . You Lost ❌`;
}
else if (userInput === 's' && compChoice === 'r') {
result_p.innerHTML = `Computer chosed Rock ⬛ . You Lost ❌`;
}
document.getElementById(userInput).classList.add('lose');
setTimeout(function () { document.getElementById(userInput).classList.remove('lose'); }, 350);
}
function Draw(userInput, compChoice) {
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
if (userInput === 'r' && compChoice === 'r') {
result_p.innerHTML = `Computer chosed Rock ⬛ . It's a Draw.`;
}
else if (userInput === 'p' && compChoice === 'p') {
result_p.innerHTML = `Computer chosed Paper 📜 . It's a Draw.`;
}
else if (userInput === 's' && compChoice === 's') {
result_p.innerHTML = `Computer chosed Scissors ✂ . It's a Draw.`;
}
document.getElementById(userInput).classList.add('draw');
setTimeout(function () { document.getElementById(userInput).classList.remove('draw'); }, 350);
}
// Getting called by main function with info of user choice
// Calls function Win , Lose and Draw .
function game(userInput) {
const compChoice = computerChoice();
const UserChoice = userInput + compChoice;
if (UserChoice === "rs" || UserChoice === "pr" || UserChoice === "sp") {
win(userInput, compChoice);
console.log("Win");
}
else if (UserChoice === "rp" || UserChoice === "ps" || UserChoice === "sr") {
Lose(userInput, compChoice);
console.log("Lose");
}
else if (UserChoice === "rr" || UserChoice === "pp" || UserChoice === "ss") {
Draw(userInput, compChoice);
console.log("Draw");
}
}
// Declaration of main function and calling it
// Also initialiser of game
function main() {
rock_div.addEventListener('click', function () {
game('r');
})
paper_div.addEventListener('click', function () {
game('p');
})
scissors_div.addEventListener('click', function () {
game('s');
})
}
main();