-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
198 lines (171 loc) · 4.87 KB
/
app.js
File metadata and controls
198 lines (171 loc) · 4.87 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const board = document.querySelectorAll('.board');
const grids = document.querySelectorAll('.grid');
const message = document.querySelector('#message');
const aiMusic = new Audio('sounds/ClowningAround.mp3');
const humanMusic = new Audio('sounds/ElevatorMusic.mp3');
const player1 = 'Player 1';
const player2 = 'Player 2';
const AI = 'AI';
const spotsPlayed = {
p1: [],
p2: [],
ai: []
}
const scoreDiv = document.querySelector('#scoreBoard');
const scores = {
p1: 0,
p2: 0
}
const winCombos = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['1', '4', '7'],
['2', '5', '8'],
['3', '6', '9'],
['1', '5', '9'],
['3', '5', '7']
];
document.querySelector('.btn2-players').addEventListener('click', createBoardHuman);
document.querySelector('.btn1-player').addEventListener('click', createBoardAI);
function createBoardHuman() {
document.querySelector('.blink-title').classList.remove('blink-title');
humanMusic.play();
board.forEach (elem => {
elem.addEventListener('click', startGameHuman);
});
}
function createBoardAI() {
document.querySelector('.blink-title').classList.remove('blink-title');
aiMusic.play()
board.forEach (elem => {
elem.addEventListener('click', startGameAI);
});
}
function startGameHuman(e) {
if (e.target.classList.contains('played')) {
return;
} else {
e.target.classList.add('played');
// console.log(e.target)
twoPlayerTurns(playerTurnCount++, player1, player2);
if (playerTurnCount % 2) {
spotsPlayed.p1.push(e.target.id)
e.target.style.opacity = 0.7;
checkWin(spotsPlayed.p1);
} else {
spotsPlayed.p2.push(e.target.id)
e.target.style.opacity = 0.3;
checkWin(spotsPlayed.p2);
}
}
if (playerTurnCount == grids.length) {
declareWinner('Draw!')
}
}
function startGameAI(e) {
if (e.target.classList.contains('played')) {
return;
} else {
e.target.classList.add('played');
// console.log(e.target)
spotsPlayed.p1.push(e.target.id)
e.target.style.opacity = 0.7;
message.innerText = `AI's thinking..`;
message.style.backgroundColor = 'lightblue';
checkWin(spotsPlayed.p1)
setTimeout(() => {
const aiTarget = aiTurn();
if (aiTarget !== undefined) {
aiTarget.style.opacity = 0.3;
message.innerText = `player 1, your turn!`;
message.style.backgroundColor = 'yellow';
//adding back classlist to keep track
aiTarget.classList.add('played');
checkWin(spotsPlayed.ai);
}
}, 600)
}
if (spotsPlayed.ai.length + spotsPlayed.p1.length == grids.length) {
declareWinner('Draw!');
}
}
function aiTurn() {
const remainingSpots = Array.from(grids).filter(square =>
!square.classList.contains('played'));
const randomSpots = Math.floor(Math.random() * remainingSpots.length);
spotsPlayed.ai.push(remainingSpots[randomSpots].id);
return remainingSpots[randomSpots];
}
let playerTurnCount = 0;
function twoPlayerTurns(playerTurnCount, p1, p2) {
if (playerTurnCount % 2) {
message.innerText = p1 + ', go for it';
message.style.backgroundColor = 'lightblue';
} else {
message.innerText = p2 + ', your turn';
message.style.backgroundColor = 'yellow';
}
}
function declareWinner (msg) {
document.querySelector('#notification').style.display = 'block';
document.querySelector('#notification #text').innerHTML = msg;
if (createBoardHuman) {
board.forEach (elem => {
elem.removeEventListener('click', createBoardHuman);
});
} else {
board.forEach (elem => {
elem.removeEventListener('click', createBoardAI);
});
}
setTimeout(() => {
document.querySelector('#notification').style.display = 'none';
replay();
}, 3000);
}
function checkWin(playerArray) {
let gameWon = false;
winCombos.forEach( win => {
let turns = 0;
win.forEach (elem => {
if (playerArray.indexOf(elem) > -1) {
turns++;
}
if (turns === 3) {
gameWon = true;
// updateScores(playerTurnCount % 2 ? player1 : player2, playerTurnCount % 2 ? scores.p1 : scores.p2);
declareWinner(playerTurnCount % 2 ? player1 + ' congrats!' : player2 + ' yay!');
// 1p mode displaying one player's congrats msg only
}
});
})
}
function updateScores(player, playScore) {
if (player1) {
return document.querySelector('#p1-scores').innerHTML = `${player} scores ${playScore + 1}`;
} else {
return document.querySelector('#p2-scores').innerHTML = `${player} scores ${playScore + 1}`;
}
}
function replay() {
playerTurnCount = 0;
message.innerText = 'Get ready to rebattle!';
message.style.backgroundColor = 'lightblue';
setTimeout(() => {
message.innerText = 'Aaaannnnd go!';
message.style.backgroundColor = 'yellow';
}, 1000)
for (spot in spotsPlayed) {
if (spotsPlayed.hasOwnProperty(spot)) {
spotsPlayed[spot] = [];
}
}
grids.forEach (elem => {
elem.classList.remove('played');
elem.style.opacity = 1;
elem.addEventListener('click', createBoardHuman);
elem.addEventListener('click', createBoardAI);
});
//wont reset to allow players to reselect mode, fix!
}