-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuit-jawa2.0.js
124 lines (103 loc) · 3.61 KB
/
suit-jawa2.0.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
const suit = ['batu', 'gunting', 'kertas']
const computerSuit = function(){
switch(Math.round(Math.random()*2)){
case 0:
return suit[0];
case 1:
return suit[1];
case 2:
return suit[2];
}
}
const logicSuit = function(playerSuit, computerSuit){
console.log(`${playerSuit} : ${computerSuit}`);
//jika pilihan player == pilihan computer, maka 'SERI'
if(playerSuit === computerSuit){
return 'SERI';
//jika pilihan player batu
} else if(playerSuit === suit[0]){
//result = jika komputer 'kertas' maka player kalah, jika tidak menang
return (computerSuit === suit[2]) ? false : true;
//jika pilihan player gunting
} else if(playerSuit === suit[1]){
//result = jika komputer 'batu' maka player kalah, jika tidak menang
return (computerSuit === suit[0]) ? false : true;
//jika pilihan player kertas
} else if(playerSuit === suit[2]){
// result - jika komputer 'gunting' maka player kalah, jika tidak menang
return (computerSuit === suit[1]) ? false : true;
}
}
function randomImage(image = ''){
const computerImage = document.querySelector('.computer');
const imageSuit = ['batu', 'gunting', 'kertas'];
const startTime = new Date().getTime();
let i = 0;
let intervalRandom = setInterval(function(){
if(new Date().getTime() - startTime > 1000){
clearInterval(intervalRandom);
computerImage.setAttribute('src', 'assets/' + image + '.png');
return null;
} else {
computerImage.setAttribute('src', 'assets/' + imageSuit[i++] + '.png');
if(i == imageSuit.length) i = 0;
}
}, 100)
return image;
}
var playerChoice = document.querySelectorAll('.player');
playerChoice.forEach(function(element,number,nodeElement){
element.addEventListener('click', function(){
var computerChoice = randomImage(computerSuit());
console.log(computerChoice);
console.log(element.getAttribute('alt'));
for(let i = 0; i < nodeElement.length; i++){
if(nodeElement[i] == element){
continue;
} else {
nodeElement[i].style.display = 'none';
}
}
element.style.cssText = 'width: 150px; height: 150px';
setTimeout(function(){
console.log(
winCondition(
logicSuit(element.getAttribute('alt'), computerChoice)
)
);
}, 1000)
});
});
function winCondition(winner){
const winnerImage = document.querySelector('.winner');
const popUpWinner = document.querySelector('.modal-container');
if(winner.toString() === 'true'){
winnerImage.setAttribute('src', 'assets/menang.png');
} else if(winner.toString() === 'false') {
winnerImage.setAttribute('src', 'assets/kalah.png');
} else {
winnerImage.setAttribute('src', 'assets/seri.png');
}
popUpWinner.style.display = 'block';
modalConfirmOrClose();
return winner;
}
function modalConfirmOrClose(){
const btnConfirm = document.querySelector('.modal-close');
const btnClose = document.querySelector('.modal-btn');
btnConfirm.addEventListener('click', function(){
modalBox();
return 0;
});
btnClose.addEventListener('click', function(){
modalBox();
return 0;
});
}
function modalBox(){
const modalContainer = document.querySelector('.modal-container');
modalContainer.style.display = 'none';
setTimeout(function(){
window.location.reload();
}, 250);
}