-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
128 lines (128 loc) Β· 4.5 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
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissors = document.querySelector("#scissors");
const record = [];
var score = 0;
var cpuScore = 0;
var options = ["stone", "paper", "scissors"];
const scoreTable = document.querySelector("#scoreTable");
document.
querySelectorAll(".selection")
.forEach(
elem => elem.addEventListener("click", (e) => {
play(e.target.id);
scoreTable.innerHTML = "";
let revarray = record.reverse();
Array.prototype.forEach.call(revarray, element => scoreTable.appendChild(element));
{
const youscoreElem = document.querySelector("#youscore");
youscoreElem.innerHTML = `${score}`;
const cpuscoreElem = document.querySelector("#cpuscore");
cpuscoreElem.innerHTML = `${cpuScore}`;
}
setTimeout(undefined, 1000);
if (record.length >= 10) {
document.querySelector("#hid").style.display = "initial";
document.querySelector("#show").style.display = "none";
document.querySelector("body").style.overflow = "hidden";
Array.prototype.forEach.call(document.querySelectorAll("#scoreTable td"), (td) => td.style.fontSize = "1.345em");
const info = document.querySelector(".info");
const player = document.querySelector("#player");
const emoji = document.querySelector("#emoji");
if (score > cpuScore) {
player.innerText = "You WIN";
emoji.innerText = "π";
} else if (score < cpuScore) {
player.innerText = "Computer wins";
emoji.innerText = "π";
}
else if (score == cpuScore) {
player.innerText = "It's a draw";
emoji.innerText = "π";
}
document.querySelector("#score").innerText = `You : ${score} Computer : ${cpuScore}`;
info.style.display = "initial";
}
}));
function play(id) {
options.splice(options.indexOf(id), 1);
cpuChoice = options[Math.floor(Math.random() * options.length)];
switch (id) {
case cpuChoice:
addToList(id, cpuChoice, "draw");
case "stone":
{
if (cpuChoice == "paper") addToList(id, cpuChoice, false);
else if (cpuChoice == "scissors") addToList(id, cpuChoice, true);
}
break;
case "paper":
{
if (cpuChoice == "scissors") addToList(id, cpuChoice, false);
else if (cpuChoice == "stone") addToList(id, cpuChoice, true);
}
break;
case "scissors":
{
if (cpuChoice == "stone") addToList(id, cpuChoice, false);
else if (cpuChoice == "paper") addToList(id, cpuChoice, true);
}
break;
}
}
function addToList(user, cpu, win) {
//scoreTable.appendChild(document.createElement("tr").appendChild(document.createElement("td")));
let uDis, cDis, uElem, cElem;
uElem = document.createElement("td");
cElem = document.createElement("td");
switch (user) {
case "stone":
uDis = "β";
break;
case "paper":
uDis = "β";
break;
case "scissors":
uDis = "β";
break;
}
switch (cpu) {
case "stone":
cDis = "β";
break;
case "paper":
cDis = "β";
break;
case "scissors":
cDis = "β";
break;
}
switch (win) {
// case "draw": {
// uElem.style.fontSize = "2em";
// uElem.style.opacity = "50%";
// cElem.style.fontSize = "2em";
// cElem.style.opacity = "50%";
// break;
// }
case true: {
cElem.style.fontSize = "2em";
cElem.style.opacity = "50%";
score++;
break;
}
case false: {
uElem.style.fontSize = "2em";
uElem.style.opacity = "50%";
cpuScore++;
break;
}
}
var cTr = document.createElement("tr");
uElem.innerHTML = uDis;
cElem.innerHTML = cDis;
cTr.appendChild(uElem);
cTr.appendChild(cElem);
record.push(cTr);
options = ["stone", "paper", "scissors"];
}