-
Notifications
You must be signed in to change notification settings - Fork 0
/
game2.js
112 lines (95 loc) · 3.39 KB
/
game2.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
let userScore = 0;
let compScore = 0;
let choices = document.querySelectorAll(".choice");
let msg = document.querySelector("#msg");
let userScorePara = document.querySelector("#my");
let compScorePara = document.querySelector("#comp");
let showWinMsg = document.querySelector(".showWin");
let winnerMsg = document.querySelector("#winner");
let resetButton = document.querySelector("#reset");
let newGameButton = document.querySelector("#newGame");
let printYourChoice = document.querySelector("#ych");
let printCompChoice = document.querySelector("#cch");
const resetTheGame = () => {
enableChoices();
userScore = 0;
compScore = 0;
userScorePara.innerText = 0;
compScorePara.innerText = 0;
msg.innerText = "Play your move";
msg.style.backgroundColor = "rgb(6, 6, 37)";
msg.style.color = "bisque";
showWinMsg.classList.add("hide");
printYourChoice.innerText = "your choice";
printCompChoice.innerText = "computer choice";
}
const genCompChoice = () => {
const options = ["rock","paper","scissor"];
const randIdx = Math.floor(Math.random()*3);
return options[randIdx];
}
const drawGame = () => {
msg.innerText = "Game was draw! play again.";
msg.style.backgroundColor = "rgb(2, 20, 48)";
}
const disableChoices = () => {
for (let choice of choices) {
choice.disabled = true ;
}
}
const enableChoices = () => {
for (let choice of choices) {
// box.enabled = true ;
choice.disabled = false ;
}
}
const showMoveWinner = (userWin,userChoice,compChoice) => {
if(userWin){
userScore++;
userScorePara.innerText = userScore;
msg.innerText = `You won this move! Your ${userChoice} beats the ${compChoice}`;
msg.style.backgroundColor = "green";
if(userScore===5){
winnerMsg.innerText = "Congratulations! You won by 5 points.";
showWinMsg.classList.remove("hide");
disableChoices();
}
} else {
compScore++;
compScorePara.innerText = compScore;
msg.innerText = `You lose this move! ${compChoice} beats your ${userChoice}`;
msg.style.backgroundColor = "red";
if(compScore===5){
winnerMsg.innerText = "Oops! you lose! Computer won by 5 point.";
showWinMsg.classList.remove("hide");
disableChoices();
}
}
}
const playGame = (userChoice) => {
//generate computer choice
const compChoice = genCompChoice();
printYourChoice.innerText = `${userChoice}`;
printCompChoice.innerText = `${compChoice}`;
if(compChoice===userChoice) {
drawGame();
} else {
let userWin = true ;
if(userChoice==="rock"){
userWin = compChoice === "paper" ? false : true ;
} else if(userChoice==="paper"){
userWin = compChoice === "scissor" ? false : true;
} else {
userWin = compChoice === "rock" ? false : true;
}
showMoveWinner(userWin,userChoice,compChoice);
}
}
choices.forEach((choice) => {
choice.addEventListener("click",() => {
const userChoice = choice.getAttribute("id");
playGame(userChoice);
});
});
resetButton.addEventListener("click",resetTheGame);
newGameButton.addEventListener("click",resetTheGame);