-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
148 lines (88 loc) · 2.61 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const playerScore = document.querySelector('.Player');
const computerScore = document.querySelector('.Computer');
const c_choice = document.querySelector('.c_choice');
const winner = document.querySelector('.winner_msg');
const header = document.querySelector('.header');
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
const tab =["rock","paper","scissors"];
let computerPlay = ()=>{
return tab[getRandomInt(3)];
}
function c_add_score(){
playerScore.style.color = "black";
cScore+=1;
computerScore.innerHTML= "Computer: "+cScore;
computerScore.style.color = "white";
}
function p_add_score(){
computerScore.style.color = "black";
pScore+=1;
playerScore.innerHTML= "Player: "+pScore;
playerScore.style.color = "white";
}
let playRound= (player,computer=computerPlay())=>{
c_choice.innerHTML="Computer choice : "+computer;
console.log(`Player choice : ${player}`);
//Paper
if (player=="paper" ){
if (computer == "scissors"){
c_add_score();
}else if (computer == "rock"){
p_add_score();
}else return 3
}
//Rock
if (player=="rock" ){
if (computer == "scissors"){
p_add_score();
}else if (computer == "paper"){
c_add_score();
}else return 3
}
//Scissors
if (player=="scissors" ){
if (computer == "rock"){
c_add_score();
}else if (computer == "paper"){
p_add_score();
}else return 3
}
}
let pScore = 0 ;
let cScore = 0 ;
let exit = false ;
function reset(){
pScore = 0 ;
cScore = 0 ;
playerScore.innerHTML= "Player: "+pScore;
computerScore.innerHTML= "Computer: "+cScore;
setTimeout(()=>{
winner.innerHTML="";
},3000);
c_choice.innerHTML= "Computer choice :";
playerScore.style.color = "black";
computerScore.style.color = "black";
}
function Winner(){//Display winner and set score to zero.
if (pScore == 5){
winner.innerHTML ="Player WIN ! ";
winner.style.color="white";
winner.style.display="inline";
reset();
}
if (cScore == 5){
winner.innerHTML="Computer win :(";
winner.style.color="white";
winner.style.display="inline";
reset();
}
}
const buttons = document.querySelectorAll('img');
buttons.forEach(button => {
button.addEventListener('click',e =>{
playRound(e.target.id,computerPlay());
Winner();
});
});