-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
102 lines (95 loc) · 2.46 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
var gameChoises = ["rock", "paper", "scissors"];
var userChoise, computerChoise;
document.querySelector(".user.rock").addEventListener("click", user_rock_click);
document.querySelector(".user.paper").addEventListener("click", user_paper_click);
document.querySelector(".user.scissors").addEventListener("click", user_scissors_click);
document.querySelector("td.play-again").addEventListener("click", playAgain);
function user_rock_click() {
userChoise = gameChoises[0];
play();
}
function user_paper_click() {
userChoise = gameChoises[1];
play();
}
function user_scissors_click() {
userChoise = gameChoises[2];
play();
}
function getRandomChoise() {
return gameChoises[(Math.random()*2).toFixed()];
}
function play() {
computerChoise = getRandomChoise();
document.querySelector(".user." + userChoise).style.background = "DodgerBlue";
document.querySelector(".computer." + computerChoise).style.background = "DodgerBlue";
var winner = getWinner(userChoise, computerChoise);
if (winner == "user") {
document.querySelector("th.user").style.background = "LightGreen";
document.querySelector("th.computer").style.background = "Tomato";
setTimeout(function () {
alert("Выиграл пользователь!");
}, 10);
}
if (winner == "computer") {
document.querySelector("th.user").style.background = "Tomato";
document.querySelector("th.computer").style.background = "LightGreen";
setTimeout(function () {
alert("Выиграл компьютер!");
}, 10);
}
if (winner == "draw") {
setTimeout(function () {
alert("Ничья!");
}, 10);
}
}
function getWinner(userChoise, computerChoise) {
switch(userChoise) {
case "rock":
switch(computerChoise) {
case "rock":
return "draw";
break;
case "paper":
return "computer";
break;
case "scissors":
return "user";
break;
}
break;
case "paper":
switch(computerChoise) {
case "rock":
return "user";
break;
case "paper":
return "draw";
break;
case "scissors":
return "computer";
break;
}
break;
case "scissors":
switch(computerChoise) {
case "rock":
return "computer";
break;
case "paper":
return "user";
break;
case "scissors":
return "draw";
break;
}
break;
}
}
function playAgain() {
var elements = document.querySelectorAll(".user, .computer");
for (var i = 0; i < elements.length; i++) {
elements[i].style.background = "rgba(0,0,0,0)";
}
}