-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
109 lines (96 loc) · 2.83 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
let userScore = 0;
let compScore = 0;
const choices = document.querySelectorAll(".choice");
const msg = document.querySelector("#msg");
let userScorePara = document.querySelector("#userScore");
let compScorePara = document.querySelector("#compScore");
let resetBtn = document.querySelector("button");
// Selecting user Choice
choices.forEach((choice) => {
choice.addEventListener("click", () => {
const userChoice = choice.getAttribute("id");
playGame(userChoice);
});
});
// Generating computer choice
const genCompChoice = () => {
const options = ["rock", "paper", "scissor"];
const randomIdx = Math.floor(Math.random() * 3);
return options[randomIdx];
};
// updating Draw condition
const drawGame = () => {
console.log("Game is draw");
msg.innerText = "Game is draw, Play again";
msg.style.backgroundColor = "black";
};
// Play Game
const playGame = (userChoice) => {
const compChoice = genCompChoice();
if (userChoice === compChoice) {
//Draw game
drawGame();
} else {
let userWin = true;
if (userChoice === "rock") {
//scissors, paper
userWin = compChoice === "paper" ? false : true;
} else if (userChoice === "paper") {
//rock, scissors
userWin = compChoice === "scissors" ? false : true;
} else {
// rock, paper
// userChoice === "scissors";
userWin = compChoice === "rock" ? false : true;
}
showWinner(userWin, userChoice, compChoice);
}
// Second options for choices
// if (userChoice === compChoice) {
// // Draw game
// drawGame();
// } else if (
// (userChoice === "rock" && compChoice === "scissor") ||
// (userChoice === "paper" && compChoice === "rock") ||
// (userChoice === "scissor" && compChoice === "paper")
// ) {
// // User wins
// showWinner(true, userChoice, compChoice);
// } else {
// // Computer wins
// showWinner(false, userChoice, compChoice);
// }
showResetBtn();
};
// Showing winner
const showWinner = (userWin, userChoice, compChoice) => {
if (userWin) {
userScore++;
userScorePara.innerText = userScore;
msg.innerText = `You win!! Your ${userChoice} beats ${compChoice}`;
msg.style.backgroundColor = "green";
} else {
compScore++;
compScorePara.innerText = compScore;
msg.innerText = `You lost!! ${compChoice} beats ${userChoice}`;
msg.style.backgroundColor = "red";
}
};
// Reset Button updations
const showResetBtn = () => {
if (msg.innerText === "Play your move") {
resetBtn.classList.add("hide");
} else {
resetBtn.classList.remove("hide");
}
};
resetBtn.addEventListener("click", () => {
userScore = 0;
compScore = 0;
userScorePara.innerText = 0;
compScorePara.innerText = 0;
msg.innerText = "Play your move";
resetBtn.classList.add("hide");
resetBtn.style.backgroundColor = "black";
msg.style.backgroundColor = "black";
});