-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrockpaper.js
89 lines (76 loc) · 2.3 KB
/
rockpaper.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
let playerscore=0;
let computerscore=0;
const container = document.querySelector('#container');
let results = document.createElement('div');
results.classList.add('results');
container.appendChild(results);
let scorekeeper = document.createElement('h2');
scorekeeper.classList.add('scorekeeper');
container.appendChild(scorekeeper);
gamestart();
const buttons = document.querySelectorAll('button');
buttons.forEach((button)=>{
button.addEventListener('click',()=>{
playRound(button.id);
},false);
});
function getcomputerChoice(){
let compchoice = Math.floor(Math.random()*3);
return compchoice;
}
function playRound(playerChoice){
if(computerscore ==5 || playerscore ==5){
gamestart();
}
compChoice =getcomputerChoice();
let result;
if(compChoice ==playerChoice){
result ="Its a tie!";
}
else if(compChoice==0 && playerChoice ==1){
result="You Win! Paper beats rock. >:^)";
++playerscore;
}
else if(compChoice ==0 && playerChoice ==2){
result="Loss.... Rocks beats scissors >:^(";
++computerscore;
}
else if(compChoice ==1 && playerChoice ==0){
result="A terrible loss... Paper beats Rock :,(";
++computerscore;
}
else if(compChoice ==1 && playerChoice ==2){
result="A grand WIN! Scissors beats Paper :D";
++playerscore;
}
else if(compChoice ==2 && playerChoice ==0){
result="NICE! Rock beats Scissors :)";
++playerscore;
}
else if(compChoice ==2 && playerChoice ==1){
result="Pity :( Scissors cuts paper";
++computerscore;
}
scorekeeper.textContent = `Your Score: ` + playerscore +
` Computer Score: ` + computerscore;
results.textContent = result;
if(computerscore==5 || playerscore ==5 ){
gameend();
}
return result;
}
function gamestart(){
playerscore=0;
computerscore=0;
results.textContent = "Let the game BEGIN! Choose a button";
scorekeeper.textContent = `Your Score: ` + playerscore +
` Computer Score: ` + computerscore;
}
function gameend(){
if(computerscore >playerscore){
results.textContent = "You losee!! \Choose a button if you want redemption";
}
else{
results.textContent = "You win!!\n Choose a button if you wan to win again ;)";
}
}