-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
365 lines (318 loc) · 10.4 KB
/
Copy pathindex.js
File metadata and controls
365 lines (318 loc) · 10.4 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// constants
const WEB_WORKER_URL = 'Connect-4.js';
const MOVE_TIME_LIMIT = 10; // seconds per move
// Game mode: 'ai' or '2p'
let gameMode = 'ai';
let currentPlayer = 1;
let timerInterval = null;
let timeRemaining = MOVE_TIME_LIMIT;
const BLURBS = {
'start': {
header: 'Get Ready',
blurb: 'Select your Mode and Start the Game.'
},
'p1-turn': {
header: 'Player 1\'s Turn',
blurb: 'Click on the Board to drop your chip.'
},
'p2-turn-ai': {
header: 'Computer\'s Turn',
blurb: 'The Computer is trying to find the best way to make you Lose.'
},
'p2-turn': {
header: 'Player 2\'s Turn',
blurb: 'Click on the Board to drop your chip.'
},
'p1-win': {
header: 'Player 1 Wins!',
blurb: 'Amazing victory! Player 1 is the champion!'
},
'p2-win-ai': {
header: 'Computer Wins',
blurb: 'Try again and NEVER GIVE UP, remember that.'
},
'p2-win': {
header: 'Player 2 Wins!',
blurb: 'Congratulations! Player 2 claims victory!'
},
'tie': {
header: 'Tie',
blurb: 'Everyone\'s a winner! Or loser. Depends on how you look at it.'
}
};
const OUTLOOKS = {
'win-imminent': 'Uh oh, computer is feeling saucy!',
'loss-imminent': 'Computer is unsure. Now\'s your chance!'
};
// global variables
const worker = new Worker(WEB_WORKER_URL);
// document ready
$(function () {
$('.start button').on('click', startGame);
setBlurb('start');
setOutlook();
// Mode toggle handler
$('input[name=mode-options]').on('change', function () {
gameMode = $(this).val();
if (gameMode === '2p') {
$('.dif').addClass('hidden');
} else {
$('.dif').removeClass('hidden');
}
});
worker.addEventListener('message', function (e) {
switch (e.data.messageType) {
case 'reset-done':
currentPlayer = 1;
startHumanTurn();
break;
case 'human-move-done':
endHumanTurn(e.data.coords, e.data.isWin, e.data.winningChips, e.data.isBoardFull);
break;
case 'progress':
updateComputerTurn(e.data.col);
break;
case 'computer-move-done':
endComputerTurn(e.data.coords, e.data.isWin, e.data.winningChips, e.data.isBoardFull,
e.data.isWinImminent, e.data.isLossImminent);
break;
}
}, false);
});
function setBlurb(key) {
$('.info h2').text(BLURBS[key].header);
$('.info .blurb').text(BLURBS[key].blurb);
}
function setOutlook(key) {
const $outlook = $('.info .outlook');
if (key) {
$outlook
.text(OUTLOOKS[key])
.show();
} else {
$outlook.hide();
}
}
// Timer functions
function startTimer() {
timeRemaining = MOVE_TIME_LIMIT;
updateTimerDisplay();
$('.timer-container').addClass('active');
timerInterval = setInterval(function () {
timeRemaining -= 0.1;
updateTimerDisplay();
if (timeRemaining <= 0) {
stopTimer();
handleTimeout();
}
}, 100);
}
function stopTimer() {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
$('.timer-container').removeClass('active');
}
function updateTimerDisplay() {
const displayTime = Math.ceil(timeRemaining);
const percentage = (timeRemaining / MOVE_TIME_LIMIT) * 100;
$('.timer-display').text(displayTime);
$('.timer-fill').css('width', percentage + '%');
// Warning state when 3 seconds or less
if (timeRemaining <= 3) {
$('.timer-display').addClass('warning');
$('.timer-fill').addClass('warning');
} else {
$('.timer-display').removeClass('warning');
$('.timer-fill').removeClass('warning');
}
}
function handleTimeout() {
// Remove click handlers to prevent conflicts
$('.click-columns, .click-columns div').off();
$('.click-columns div').removeClass('hover');
destroyCursorChip();
// Find first available column and make a random move
const availableCols = [];
for (let i = 0; i < 7; i++) {
// Check if column is available by checking the board visually
availableCols.push(i);
}
// Pick a random available column
const randomCol = availableCols[Math.floor(Math.random() * availableCols.length)];
// Show chip and make the move
createCursorChip(currentPlayer, randomCol);
worker.postMessage({
messageType: 'human-move',
col: randomCol,
player: currentPlayer
});
}
function startGame() {
gameMode = $('input[name=mode-options]:checked').val();
currentPlayer = 1;
$('.sidebar').addClass('freeze');
$('.mode input, .dif input').prop('disabled', true);
$('.lit-cells, .chips').empty();
worker.postMessage({
messageType: 'reset',
});
}
function startHumanTurn() {
if (currentPlayer === 1) {
setBlurb('p1-turn');
} else {
setBlurb('p2-turn');
}
// Start the move timer
startTimer();
$('.click-columns div').addClass('hover');
// if mouse is already over a column, show cursor chip there
const col = $('.click-columns div:hover').index();
if (col !== -1) {
createCursorChip(currentPlayer, col);
}
$('.click-columns')
.on('mouseenter', function () {
const col = $('.click-columns div:hover').index();
createCursorChip(currentPlayer, col);
})
.on('mouseleave', function () {
destroyCursorChip();
});
$('.click-columns div')
.on('mouseenter', function () {
const col = $(this).index();
moveCursorChip(col);
})
.on('click', function () {
stopTimer();
$('.click-columns, .click-columns div').off();
const col = $(this).index();
worker.postMessage({
messageType: 'human-move',
col: col,
player: currentPlayer
});
});
}
function endHumanTurn(coords, isWin, winningChips, isBoardFull) {
$('.click-columns div').removeClass('hover');
if (!coords) {
// column was full, human goes again
startHumanTurn();
} else {
dropCursorChip(coords.row, function () {
if (isWin) {
const winBlurb = currentPlayer === 1 ? 'p1-win' : 'p2-win';
endGame(winBlurb, winningChips);
} else if (isBoardFull) {
endGame('tie');
} else {
// Switch turns
if (gameMode === '2p') {
// Two-player mode: switch to other player
currentPlayer = currentPlayer === 1 ? 2 : 1;
startHumanTurn();
} else {
// AI mode: pass turn to computer
startComputerTurn();
}
}
});
}
}
function startComputerTurn() {
setBlurb('p2-turn-ai');
// computer's cursor chip starts far left and moves right as it thinks
createCursorChip(2, 0);
const maxDepth = parseInt($('input[name=dif-options]:checked').val(), 10) + 1;
worker.postMessage({
messageType: 'computer-move',
maxDepth: maxDepth
});
}
function updateComputerTurn(col) {
moveCursorChip(col);
}
function endComputerTurn(coords, isWin, winningChips, isBoardFull, isWinImminent, isLossImminent) {
moveCursorChip(coords.col, function () {
dropCursorChip(coords.row, function () {
if (isWin) {
endGame('p2-win-ai', winningChips);
} else if (isBoardFull) {
endGame('tie');
} else {
if (isWinImminent) {
setOutlook('win-imminent');
} else if (isLossImminent) {
setOutlook('loss-imminent');
} else {
setOutlook();
}
// pass turn to human
currentPlayer = 1;
startHumanTurn();
}
});
});
}
function endGame(blurbKey, winningChips) {
$('.sidebar').removeClass('freeze');
$('.mode input, .dif input').prop('disabled', false);
setBlurb(blurbKey);
setOutlook();
if (winningChips) {
// not a tie, highlight the chips in the winning run
for (let i = 0; i < winningChips.length; i++) {
createLitCell(winningChips[i].col, winningChips[i].row);
}
}
}
function createLitCell(col, row) {
$('<div>')
.css({
'left': indexToPixels(col),
'bottom': indexToPixels(row)
})
.appendTo('.lit-cells');
}
function createCursorChip(player, col) {
const playerClass = 'p' + player;
$('<div>', { 'class': 'cursor ' + playerClass })
.css('left', indexToPixels(col))
.appendTo('.chips');
// also highlight column
$('.lit-columns div').eq(col).addClass('lit');
}
function destroyCursorChip() {
$('.chips .cursor').remove();
$('.lit-columns .lit').removeClass('lit');
}
function moveCursorChip(col, callback) {
$('.chips .cursor').css('left', indexToPixels(col));
$('.lit-columns .lit').removeClass('lit');
$('.lit-columns div').eq(col).addClass('lit');
// callback is only used when the computer is about to drop a chip
// give it a slight delay for visual interest
setTimeout(callback, 300);
}
function dropCursorChip(row, callback) {
// speed of animation depends on how far the chip has to drop
const ms = (7 - row) * 40;
const duration = (ms / 1000) + 's';
$('.chips .cursor')
.removeClass('cursor')
.css({
'bottom': indexToPixels(row),
'transition-duration': duration,
'animation-delay': duration
})
.addClass('dropped');
$('.lit-columns .lit').removeClass('lit');
setTimeout(callback, ms);
}
function indexToPixels(index) {
return (index * 61 + 1) + 'px';
}