-
Notifications
You must be signed in to change notification settings - Fork 1
/
tic-tac-toe.js
159 lines (128 loc) · 4.59 KB
/
tic-tac-toe.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class TicTacToe {
constructor(selector) {
this.parrentElement = document.querySelector(selector);
this.playerList = ['x', 'o'];
this.GameBoards = Array(9).fill('');
this.init();
this.switchPlayer(0);
}
init() {
this.buildGameUI();
}
getPlayerLabel() {
return this.playerList[this.currentPlayer];
}
buildCardPlayer(playerName, playerNumber) {
return `<div class="box-player">
<p class="player-label ${playerName}">
${playerName}
</p>
<p class="player-name">
Player ${playerNumber}
</p>
<p class="turn">Giliran Mu!</p>
</div>`
}
buildGameUI() {
//game info
const gameInfoEl = document.createElement('div');
gameInfoEl.className = 'game-info';
let playerCards = '';
this.playerList.forEach((player, i) => {
playerCards += this.buildCardPlayer(player, i + 1)
});
gameInfoEl.innerHTML = playerCards;
//button reset
const gameControl = document.createElement('div');
gameControl.className = 'game-control';
const btnReset = document.createElement('button');
btnReset.className = 'btn btn-reset';
btnReset.innerHTML = '<i class="fa-solid fa-rotate-right"></i> Reset Game';
btnReset.addEventListener('click', () => this.gameReset());
gameControl.appendChild(btnReset);
gameInfoEl.appendChild(gameControl);
//gameplay
const gamePlayEl = document.createElement('div');
gamePlayEl.className = 'game-play';
for (let i = 0; i < 9; i++) {
const btn = document.createElement('button');
btn.className = 'btn-tic-tac-tow';
btn.addEventListener('click', (e) => this.onCellClick(e, i))
gamePlayEl.appendChild(btn);
}
// append to parrent element
this.parrentElement.append(gameInfoEl, gamePlayEl);
this.gamePlayEl = gamePlayEl;
}
onCellClick(event, index) {
const btn = event.target;
btn.innerText = this.getPlayerLabel();
btn.classList.add(btn.innerText);
btn.disabled = true;
this.GameBoards[index] = btn.innerText;
this.checkWinner();
this.switchPlayer();
}
switchPlayer(currentPlayer = undefined) {
if (currentPlayer != undefined) {
this.currentPlayer = currentPlayer;
} else {
this.currentPlayer = this.currentPlayer == 1 ? 0 : 1;
}
const BoxPlayers = document.querySelectorAll('.box-player');
BoxPlayers.forEach((box, i) => {
if (this.currentPlayer == i) {
box.classList.add('active');
} else {
box.classList.remove('active');
}
});
}
gameReset() {
this.GameBoards = Array(9).fill('');
this.switchPlayer(0);
for (const btn of this.gamePlayEl.children) {
btn.innerHTML = '';
btn.classList.remove(...this.playerList);
btn.disabled = false;
}
}
checkWinner() {
const winConditions = [
[0, 1, 2], //horizontal
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],//vertical
[1, 4, 7],
[2, 5, 8],
[0, 4, 8], //diagonal left to bottom rigth
[2, 4, 6], //diagonal rigth to bottom left
];
for (let i = 0; i < winConditions.length; i++) {
const [a, b, c] = winConditions[i];
if (
this.getPlayerLabel() == this.GameBoards[a] &&
this.getPlayerLabel() == this.GameBoards[b] &&
this.getPlayerLabel() == this.GameBoards[c]
) {
Swal.fire({
title: 'Good Jobs',
text: `Selamat Player ${this.currentPlayer + 1} kamu memenangkan game ini!`,
showDenyButton: true,
confirmButtonText: 'Mantap !',
denyButtonText: `Ulangi dong`,
icon: 'success',
}).then((result) => {
//disable other button
for (const btn of this.gamePlayEl.children) {
btn.disabled = true;
}
if (result.isDenied) {
this.gameReset();
Swal.fire('Game sudah direset, Ayo main lagi !', '', 'info');
}
})
}
}
}
}