-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththuattoan.js
95 lines (89 loc) · 2.98 KB
/
thuattoan.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
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function consoleMessage(message) {
msg.className = 'message';
msg.textContent = message;
app.appendChild(msg);
}
function consoleImg(result,com) {
switch(com) {
case 1:
result.src = 'https://res.cloudinary.com/phucthuhigh/image/upload/v1627473190/RockPaperScissorsGame/paper_ru3amg.png';
break;
case 2:
result.src = 'https://res.cloudinary.com/phucthuhigh/image/upload/v1627473190/RockPaperScissorsGame/scissors_k5vs2q.png';
break;
case 0:
result.src = 'https://res.cloudinary.com/phucthuhigh/image/upload/v1627473190/RockPaperScissorsGame/rock_pfk455.png';
break;
}
resultBox.appendChild(result);
}
function consoleResult(kq, com, player) {
switch(kq) {
case 1:
consoleImg(resultOfPlayer,player);
consoleImg(resultOfComputer,com);
consoleMessage('Bạn và máy hòa');
break;
case 2:
consoleImg(resultOfPlayer,player);
consoleImg(resultOfComputer,com);
consoleMessage('Chúc Mừng Bạn Đã Thắng!!');
break;
case 0:
consoleImg(resultOfPlayer,player);
consoleImg(resultOfComputer,com);
consoleMessage('Bạn thua! Hãy thử lại lần nữa');
break;
}
}
let rePlay = document.createElement('button');
let btnElements = document.querySelectorAll('#app #choose .btn');
let resultBox = document.querySelector('#app #result');
let msg = document.createElement('p');
msg.className = 'message';
let resultOfComputer = document.createElement('img');
resultOfComputer.className = 'resultComputer';
let resultOfPlayer = document.createElement('img');
resultOfPlayer.className = 'resultPlayer';
btnElements.forEach((btnElement) => {
btnElement.addEventListener('click',() => {
let computer = getRandomInt(3);
let kq;
if (computer == Number(btnElement.id)) {
kq = 1;
} else if (Number(btnElement.id) == 0) {
if (computer == 1) {
kq = 0;
} else {
kq = 2;
}
} else if (Number(btnElement.id) == 1) {
if (computer == 0) {
kq = 2;
} else {
kq = 0;
}
} else if (Number(btnElement.id) == 2) {
if (computer == 1) {
kq = 2;
} else {
kq = 0;
}
}
consoleResult(kq,computer,Number(btnElement.id));
rePlay.className = 'replay';
rePlay.textContent = 'Chơi lại';
app.appendChild(rePlay);
rePlay.addEventListener('click',() => {
location.reload();
})
for (let i in btnElements) {
btnElements[i].disabled = true;
btnElements[i].style.cursor = 'no-drop';
btnElements[i].style.opacity = 0.5;
}
})
})