-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScript.js
More file actions
152 lines (127 loc) · 4.53 KB
/
Script.js
File metadata and controls
152 lines (127 loc) · 4.53 KB
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
146
147
148
149
150
151
152
const game = () => {
let pscore = 0;
let cscore = 0;
const changetheme= () => {
const changebutton = document.querySelector("#changebutton");
const backimg = document.querySelector(".backgroundimg img");
let changenum=1;
changebutton.addEventListener("click", () => {
if(changenum==1){
changenum=2;
}
else{
changenum=1;
}
backimg.src=(`./img/image${changenum}.png`);
} )
}
const start = () => {
const startbtn = document.querySelector("#start button");
const startbar = document.querySelector("#start");
const gplay = document.querySelector("#gplay");
startbtn.addEventListener("click", () => {
startbar.className += "fadeout";
gplay.className += "fadein";
})
}
const playmatch = () => {
const option = document.querySelectorAll(".options button");
console.log(option);
const playerHand = document.querySelector(".playerHand");
const computerHand = document.querySelector(".computerHand");
const hands = document.querySelectorAll(".hands img");
hands.forEach( hand =>{
hand.addEventListener("animationend", function(){
this.style.animation='';
})
})
const optionarr = ["Stone", "Paper", "Scissors"];
console.log(optionarr)
option.forEach(element => {
// console.log(element);
element.addEventListener("click", function () {
playerHand.src = `./img/Stone.png`;
computerHand.src = `./img/Stone.png`;
setTimeout(() => {
// computer choice
compnum = Math.floor(Math.random() * 3);
const compchoice = optionarr[compnum];
console.log(compchoice);
// player choice
const playerchoice = this.textContent;
//Calling comparison
comparison(compchoice, this.textContent);
// images
playerHand.src = `./img/${this.textContent}.png`;
computerHand.src = `./img/${compchoice}.png`;
}, 1000);
//shake
playerHand.style.animation = 'shakeplayer 1s ease ';
computerHand.style.animation = "shakecomputer 1s ease";
})
})
const updatescore = () => {
const playerscore = document.querySelector("#Pscore");
const computerscore = document.querySelector("#Cscore");
playerscore.textContent = pscore;
computerscore.textContent = cscore;
}
const comparison = (compchoice, playerchoice) => {
//updating the text
const winner = document.querySelector(".headgplay");
if (compchoice === playerchoice) {
winner.textContent = ("It's a tie!!");
return;
}
//stone
if (playerchoice === "Stone") {
if (compchoice === "Scissors") {
winner.textContent = ("You Win");
pscore++;
updatescore();
return;
}
else {
winner.textContent = ("You Lose");
cscore++;
updatescore();
return;
}
}
//paper
if (playerchoice === "Paper") {
if (compchoice === "Scissors") {
winner.textContent = ("You Lose");
cscore++;
updatescore();
return;
}
else {
winner.textContent = ("You Win");
pscore++;
updatescore();
return;
}
}
// scissors
if (playerchoice === "Scissors") {
if (compchoice === "Paper") {
winner.textContent = ("You Win");
pscore++;
updatescore();
return;
}
else {
winner.textContent = ("You Lose");
cscore++;
updatescore();
return;
}
}
}
}
changetheme();
start();
playmatch();
}
game();