-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
118 lines (97 loc) · 3.66 KB
/
script.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
116
117
118
/*
Rock Paper Scissors ... GAME
##################################### */
function getComputerChoice(){
const choices =['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * 3)];
}
/*
Getting variables from document
*/
const buttons = document.querySelectorAll('.btn');
const compChoiceDisplay = document.getElementsByClassName('choice')[0];
const userChoiceDisplay = document.getElementsByClassName('choice')[1];
const compPointsDisplay = document.getElementsByClassName('points')[0];
const userPointsDisplay = document.getElementsByClassName('points')[1];
const popUp = document.querySelector('.pop-up');
/*
Changing GUI Displays
*/
let compWin = 0;
let userWin = 0;
let compChoice;
function displays(choice, e){
//To make first letter uppercase
compChoiceDisplay.innerText = choice.slice(0,1).toUpperCase() + choice.slice(1);
userChoiceDisplay.innerText = e.target.innerText;
compPointsDisplay.innerText = compWin;
userPointsDisplay.innerText = userWin;
};
function playRound(compSelection, playerSelection){
if(compSelection === playerSelection){
popUp.innerText = `It's draw game the computer also played ${compSelection}`;
}
else {
switch (true){
//User win cases
case (compSelection === "rock" && playerSelection === "paper"):
case (compSelection === "paper" && playerSelection === "scissors"):
case (compSelection === "scissors" && playerSelection === "rock"):
popUp.innerHTML = `<p> You won!! ${playerSelection} beats ${compSelection}!! </p>`;
userWin++;
break;
//Computer win cases
case (compSelection === "rock" && playerSelection === "scissors"):
case (compSelection === "paper" && playerSelection === "rock"):
case (compSelection === "scissors" && playerSelection === "paper"):
popUp.innerHTML = `<p> You loose ${compSelection} beats ${playerSelection}!! </p>`;
compWin++;
break;
}
}
}
// FUNCTION TO PLAY 5 ROUNDS
function game(buttons){
buttons.forEach(btn => {
btn.addEventListener('click',e=>{
compChoice = getComputerChoice();
playRound(compChoice, e.target.innerText.toLowerCase());
displays(compChoice, e);
popUp.classList.remove('none');
setTimeout(()=> popUp.classList.add('none'),1000);
if(compWin === 5 || userWin === 5){
let last = document.createElement('div');
last.setAttribute('class','last flex');
let finalRes = document.createElement('div');
last.appendChild(finalRes);
finalRes.classList.add('finals');
document.body.appendChild(last);
let confButton = document.createElement('button');
confButton.classList.add('confirm');
confButton.innerText = 'Confirm';
confButton.onclick = ()=> location.reload();
let cancButton = document.createElement('button');
cancButton.classList.add('cancel');
cancButton.innerText = 'Cancel';
if(compWin == 5 && userWin < 5){
cancButton.onclick = ()=>{
last.style.display = 'none';
};
finalRes.innerHTML = '<p>You loose! Play Again?</p>';
finalRes.append(confButton, cancButton);
}
if(userWin == 5 && compWin < 5){
cancButton.onclick = ()=>{
last.style.display = 'none';
};
finalRes.innerHTML = '<p>Congratulation! You Won.. Play Again?</p>';
finalRes.append(confButton, cancButton);
} else {
finalRes.innerHTML = '<p>Play Again?</p>';
finalRes.appendChild(confButton);
}
};
});
});
}
game(buttons);