-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstone_Paper_Scissor.js
67 lines (57 loc) · 1.94 KB
/
stone_Paper_Scissor.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
let userScore = 0;
let compScore = 0;
const choices = document.querySelectorAll(".choice");
let msg = document.getElementById("msg");
let user = document.getElementById("user-score1");
let computer = document.getElementById("computer-score1");
const genCompChoice = () =>{
const options = ["ROCK","PAPER","SCISSOR"];
const index = Math.floor( Math.random()* 3);
return options[index];
}
const drawGame = ()=>{
// console.log("game was draw.");
msg.innerText = "game was draw.Play Again.";
msg.style.backgroundColor = "rgb(79 174 251)"
}
const showWinner = (userWin,userChoice,compChoice)=>{
if(userWin){
msg.innerText=`YOU WIN! Your ${userChoice} beats ${compChoice}`;
userScore++;
user.innerText = userScore;
msg.style.backgroundColor = "green";
// console.log("YOU WIN!");
}else{
// console.log("YOU LOSE,COMPUTER WIN!");
msg.innerText = `YOU LOSE! ${compChoice} beats Your ${userChoice}`;
compScore++;
computer.innerText = compScore;
msg.style.backgroundColor = "Red";
}
}
const playGame = (userChoice )=>{
// console.log("user choice = ",userChoice);
const compChoice = genCompChoice();
// console.log("computer choice = ",compChoice);
if(compChoice === userChoice){
drawGame();
}else{
let userWin = true;
if(userChoice === "ROCK"){
userWin = compChoice === "PAPER"? false : true;
}
if(userChoice === "PAPER"){
userWin = compChoice === "SCISSOR" ? false:true;
}
if(userChoice === "SCISSOR"){
userWin = compChoice === "ROCK" ? false:true;
}
showWinner(userWin,userChoice,compChoice);
}
}
choices.forEach((choice)=>{
choice.addEventListener("click",()=>{
const userChoice = choice.getAttribute("id");
playGame(userChoice);
});
});