-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (114 loc) · 3.4 KB
/
index.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
// arrow function
window.addEventListener('DOMContentLoaded', () => {
const tiles = Array.from(document.querySelectorAll('.tile'));
const playerDispley = document.querySelector('.display-player');
const resetButton = document.querySelector('#reset');
const announcer = document.querySelector('.announcer');
let board = ['', '', '', '', '', '', '', '', '',];
let currentPlayer = 'X';
let isGameActive = true;
const PLAYER_X_WON = 'PLAYER_WON';
const PLAYER_O_WON = 'PLAYER_LOSE';
const TIE = 'TIE';
/*
Indexes within the board
[0] [1] [2]
[3] [4] [5]
[6] [7] [8]
*/
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
const updateBoard = (index) => {
board[index] = currentPlayer;
console.log('updateBoard', currentPlayer);
}
const changePlayer = () => {
playerDispley.classList.remove(`player${currentPlayer}`);
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
playerDispley.innerText = currentPlayer;
playerDispley.classList.add(`player${currentPlayer}`);
}
const isValidAction = (tile) => {
if (tile.innerText === 'X' || tile.innerText === 'O'){
return false;
}
return true;
}
const announce = (type) => {
switch (type) {
case PLAYER_O_WON:
announcer.innerHTML = 'player <span class="playerO">O</span> Won'
break;
case PLAYER_X_WON:
announcer.innerHTML = 'player <span class="playerX">X</span> Won'
break;
case PLAYER_TIE:
announcer.innerHTML = 'Tie'
break;
}
announcer.classList.remove('hide');
}
handleResultValidation = () => {
let roundWon = false;
for (let i = 0; i <= 7; i++) {
const winCondition = winningConditions[i];
const a = board[winCondition[0]];
const b = board[winCondition[1]];
const c = board[winCondition[2]];
if (a === '' || b === '' || c === '') {
continue;
}
if (a === b && b === c) {
roundWon = true;
break;
}
}
if (roundWon) {
announce(currentPlayer === 'X' ? PLAYER_X_WON : PLAYER_O_WON);
isGameActive = false;
return;
}
if (!board.includes('')) {
announce(TIE);
}
}
const userAction = (tile, index) => {
if (isGameActive && isValidAction(tile)) {
tile.innerText = currentPlayer;
tile.classList.add(`player${currentPlayer}`);
updateBoard(index);
handleResultValidation();
changePlayer ();
}
}
const resetBoard = () => {
board = ['', '', '', '', '', '', '', '', ''];
announcer.classList.add('hide');
if (currentPlayer === 'O') {
changePlayer();
}
tiles.forEach(tile => {
tile.innerText = '';
tile.classList.remove('playerX');
tile.classList.remove('playerO');
})
}
tiles.forEach((tile, index) => {
tile.addEventListener('click', () => {
userAction(tile, index);
console.log('hello');
})
})
resetButton.addEventListener('click', () => {
console.log('--------> click');
resetBoard();
})
})