-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (106 loc) · 3.81 KB
/
index.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
let score = 0;
const hide = () => {
//hide rule block
const rb = document.getElementById("rules");
rb.style.display = "none";
};
const show = () => {
//show rule block
const rb = document.getElementById("rules");
rb.style.display = "grid";
};
const userPicked = (up) => {
//auto pick value for house
const hp = housePicked();
//select house and user value
const hs = document.getElementById("house-" + hp);
const us = document.getElementById("user-" + up);
//select game-block, result-text, move-selection
const gb = document.getElementById("game-block");
const rt = document.getElementById("result-text");
const ms = document.getElementById("move-selection");
const s = document.getElementById("score");
ms.style.display = "none"; //make move-selection invisiable
us.style.opacity = 1; //make user selection visiable
hs.style.opacity = 1; //make house selection visiable
result_change(gb);//make result block visiable
const result = winnerPicker(up, hp);//calculate result
if (result === "you win") {
console.log("hi");
us.classList.add("winner");
}
if (result === "you lose") {
console.log("bye");
hs.classList.add("winner");
}
rt.innerHTML = result;//inserting result value in result span in html
score < 0 ? score = 0 : score;
s.innerHTML = score;
};
const result_change = (gb) => {
//seting game-block from 3 col to 2 col to display result
gb.classList.add("game-block-result");
gb.classList.remove("game-block");
};
const housePicked = () => {
const pickable = ["rock", "paper", "scissors"];
//pick random from rock,paper,scissors
return (pickable[Math.floor(Math.random() * 3)]);
};
const playAgain = () => {
//reseting house and user selctions
//inline-style
document.getElementById("house-rock").removeAttribute("style");
document.getElementById("user-rock").removeAttribute("style");
document.getElementById("house-paper").removeAttribute("style");
document.getElementById("user-paper").removeAttribute("style");
document.getElementById("house-scissors").removeAttribute("style");
document.getElementById("user-scissors").removeAttribute("style");
//css file style
document.getElementById("house-rock").classList.remove("winner");
document.getElementById("user-rock").classList.remove("winner");
document.getElementById("house-paper").classList.remove("winner");
document.getElementById("user-paper").classList.remove("winner");
document.getElementById("house-scissors").classList.remove("winner");
document.getElementById("user-scissors").classList.remove("winner");
//reseting result and making move-selection visiable
document.getElementById("result-text").removeAttribute("style");
document.getElementById("move-selection").removeAttribute("style");
//reseting game-block from 3 col to 2 col to hide result
const gb = document.getElementById("game-block");
gb.classList.add("game-block");
gb.classList.remove("game-block-result");
};
const winnerPicker = (us, hs) => {
//draw
if (us === hs) {
score += 0;
return "its a draw";
}
//winning conditions
if (us === "rock" && hs === "scissors") {
score += 1;
return "you win";
}
if (us === "paper" && hs === "rock") {
score += 1;
return "you win";
}
if (us === "scissors" && hs === "paper") {
score += 1;
return "you win";
}
//losing conditions
if (us === "rock" && hs === "paper") {
score -= 1;
return "you lose";
}
if (us === "scissors" && hs === "rock") {
score -= 1;
return "you lose";
}
if (us === "paper" && hs === "scissors") {
score -= 1;
return "you lose";
}
};