-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
122 lines (104 loc) · 4.15 KB
/
game.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
var rps = document.getElementsByClassName('icon');
var grid = document.getElementsByClassName('col-sm');
var result = document.getElementById('result');
class Player {
getRandomChoice() {
var choice = Math.floor(Math.random() * 3) + 1;
return choice;
}
}
class Game {
constructor(hasResult) {
this.hasResult = hasResult;
}
isHasResult() {
return this.hasResult;
}
getWinner(player, com) {
if (com == 1) {
document.getElementById('enemy-batu').classList.add('selected');
document.getElementById('enemy-kertas').classList.remove('selected');
document.getElementById('enemy-gunting').classList.remove('selected');
} else if (com == 2) {
document.getElementById('enemy-kertas').classList.add('selected');
document.getElementById('enemy-batu').classList.remove('selected');
document.getElementById('enemy-gunting').classList.remove('selected');
} else {
document.getElementById('enemy-gunting').classList.add('selected');
document.getElementById('enemy-kertas').classList.remove('selected');
document.getElementById('enemy-batu').classList.remove('selected');
}
if (player == 'rock') {
document.getElementById('player-batu').classList.add('selected');
document.getElementById('player-kertas').classList.remove('selected');
document.getElementById('player-gunting').classList.remove('selected');
} else if (player == 'paper') {
document.getElementById('player-kertas').classList.add('selected');
document.getElementById('player-batu').classList.remove('selected');
document.getElementById('player-gunting').classList.remove('selected');
} else {
document.getElementById('player-gunting').classList.add('selected');
document.getElementById('player-kertas').classList.remove('selected');
document.getElementById('player-batu').classList.remove('selected');
}
if (player == 'rock' && com == 1) {
result.innerHTML = 'DRAW';
} else if (player == 'rock' && com == 2) {
result.innerHTML = 'COM WINS';
} else if (player == 'rock' && com == 3) {
result.innerHTML = 'PLAYER 1 WINS';
} else if (player == 'paper' && com == 1) {
result.innerHTML = 'PLAYER 1 WINS';
} else if (player == 'paper' && com == 2) {
result.innerHTML = 'DRAW';
} else if (player == 'paper' && com == 3) {
result.innerHTML = 'COM WINS';
} else if (player == 'scissors' && com == 1) {
result.innerHTML = 'COM WINS';
} else if (player == 'scissors' && com == 2) {
result.innerHTML = 'PLAYER 1 WINS';
} else {
result.innerHTML = 'DRAW';
}
if (result.innerHTML == 'DRAW') {
result.classList.remove('win-text');
result.classList.remove('vs-text');
result.className += ' text-style draw-text';
} else {
result.classList.remove('draw-text');
result.classList.remove('vs-text');
result.className += ' text-style win-text';
}
this.hasResult = true;
}
refreshGame() {
result.classList.remove('draw-text');
result.classList.remove('win-text');
result.classList.remove('text-style');
result.className += ' vs-text';
result.innerHTML = "VS";
var gameIcon = document.getElementsByClassName('rps');
for (var i = 0; i < gameIcon.length; i++) {
gameIcon[i].classList.remove('selected');
}
this.hasResult = false;
}
}
var game = new Game(false);
var player = new Player();
for (var i = 0; i < grid.length; i++) {
grid[i].className += ' d-flex justify-content-center align-items-center';
}
function goToHome() {
window.location.href = 'Main-page';
}
function rpsClicked(e) {
var human = e;
var com = player.getRandomChoice();
if (!game.isHasResult()) {
game.getWinner(human, com);
}
}
function refreshGame() {
game.refreshGame();
}