/
main.js
453 lines (427 loc) · 17.5 KB
/
main.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
$(document).ready( initializeGame );
function initializeGame(){
controller.createBoard();
view.applyClickHandlers();
controller.InitialChips();
view.displayChipCount();
}
var model = {
grid: [],
player: 0,
ai: null,
currentAvailableSpots: null,
directionMap: [[-1, -1],[-1, 0],[-1, 1],[0, -1],[0, 0],[0, 1],[1, -1],[1, 0],[1, 1]],
chipCount: null,
player1ChipCount: null,
player2ChipCount: null,
humanTurn: false,
aiTurn: false,
CreateGridCell: function(y, x){
this.occupied = false;
this.location = $('.row:eq('+y+') .cell:eq('+x+')'); //:eq selects an element w/ specific index number starting at 0 - like :nth child but starts at 0, not 1
this.player = null;
this.clickable = false;
},
createGridArrayMatrix: function(){ //creates 8x8 grid. Outer loop creates 8 rows; Inner loop creates 8 cells within and pushes into the row array per loop. Pushes to grid array at the end.
for (var y=0; y<8; y++){
var row = [];
for (var x=0; x<8; x++){
var cell = new this.CreateGridCell(y, x); //give coordinate location of each new cell created as the loop runs, then pushes to row every time outer loop runs.
row.push(cell)
}
this.grid.push(row);
}
},
addChipData: function(y, x, player){ //adds properties to current cell, calls function to make surrounding cells clickable
var currentCell = this.grid[y][x];
currentCell.occupied = true;
currentCell.player = player;
this.addSurroundingClick(y, x);
this.chipCount++;
if (player === 0){
this.player1ChipCount++;
} else {
this.player2ChipCount++;
}
},
addSurroundingClick: function(y, x){ //checks all 8 cells surrounding current cell and changes them to clickable
for (let y_i=-1; y_i<2; y_i++){
for (let x_i=-1; x_i<2; x_i++){
if (y+y_i < 0 || y+y_i > 7){
break;
} else if (x+x_i < 0 || x+x_i > 7){
continue;
}
this.grid[y + y_i][x + x_i].clickable = true;
}
}
},
checkSurroundingChips: function(y, x, player){ //checks all 8 cells surrounding current cell, if it finds other player's disc, calls function to search in appropriate direction
var otherPlayer = 1 - player; //also checks position on board. Won't go into cells outside the board if y or x is less than 0 or greater than 7.
for (let i=0; i<9; i++){
let ydir = y + this.directionMap[i][0];
let xdir = x + this.directionMap[i][1];
if (ydir >= 0 && ydir < 8 && xdir >= 0 && xdir < 8) {
if (this.grid[ydir][xdir].player === otherPlayer) {
if (this.checkDir(y, x, player, this.directionMap[i])) {
return true;
};
}
}
}
return false;
},
checkDir: function(y, x, player, dir){ //following 8 functions check direction of chips for opponent chip by first seeing if their own chip occupies space.
var outputArray = []; //Then checks if space is empty. Then checks for opponent chip. If found, push to an array and output array after you loop back to if and find current players chip (i.e. their own)
for (let i=1, ydir = y + dir[0], xdir = x + dir[1];
ydir >= 0 && ydir < 8 && xdir >= 0 && xdir < 8;
i++, ydir += dir[0], xdir += dir[1]){
if (model.grid[ydir][xdir].player === player){
return outputArray;
} else if (model.grid[ydir][xdir].occupied === false){
return false;
} else if (model.grid[ydir][xdir].player !== player){
outputArray.push(model.grid[ydir][xdir]);
}
}
return false;
},
flipChipData: function(cellObject, player){
cellObject.player = 1 - cellObject.player; //switch players and then add chip count
if (player === 0){
this.player1ChipCount++;
this.player2ChipCount--;
} else {
this.player1ChipCount--;
this.player2ChipCount++;
}
},
checkWinStats: function(){
if (this.chipCount === 64 || this.currentAvailableSpots[0] === undefined){
if (this.player1ChipCount > this.player2ChipCount) {
return 0;
} else if (this.player1ChipCount < this.player2ChipCount){
return 1;
} else {
return 2;
}
} else if (this.player1ChipCount === 0){
return 1;
} else if (this.player2ChipCount === 0){
return 0;
}
return false;
},
gridAnnihilation: function(){
this.grid = [];
},
statReset: function(){
this.currentAvailableSpots = null;
this.chipCount = null;
this.player1ChipCount = null;
this.player2ChipCount = null;
},
getAiSpot: function(){
var highestPriorityIndex = 0;
var badSpots = [];
var goodSpots = [];
for (var i=0; i<model.currentAvailableSpots.length; i++){
var position = model.currentAvailableSpots[i].location.attr('position');
switch (position){
case '0-0':
case '0-7':
case '7-0':
case '7-7':
return this.currentAvailableSpots[i];
case '0-1':
case '1-0':
case '1-1':
if (model.grid[0][0].player === model.ai){
return this.currentAvailableSpots[i]
}
badSpots.push(this.currentAvailableSpots[i]);
break;
case '0-6':
case '1-7':
case '1-6':
if (model.grid[0][7].player === model.ai){
return this.currentAvailableSpots[i]
}
badSpots.push(this.currentAvailableSpots[i]);
break;
case '6-0':
case '7-1':
case '6-1':
if (model.grid[7][0].player === model.ai){
return this.currentAvailableSpots[i]
}
badSpots.push(this.currentAvailableSpots[i]);
break;
case '7-6':
case '6-7':
case '6-6':
if (model.grid[7][7].player === model.ai){
return this.currentAvailableSpots[i]
}
badSpots.push(this.currentAvailableSpots[i]);
break;
default:
goodSpots.push(this.currentAvailableSpots[i]);
}
}
if (goodSpots[0]){
return goodSpots[Math.floor(Math.random()*goodSpots.length)];
} else {
return badSpots[Math.floor(Math.random()*badSpots.length)];
}
}
};
//////////////////////////////////////////////////////////////
/////////////// VIEW ///////////////////
//////////////////////////////////////////////////////////////
var view = {
applyClickHandlers: function(){
$('#gameboard').on('click', '.cell', controller.addChipToGame);
$('.singleButton').on('click', function(){
controller.gameStart(1-model.player); //opposite of what player chooses and set it to ai b/c we play in ai as the parameter
});
$('.multiButton').on('click', function(){
controller.gameStart(null);
});
$('.playerBox1').on('click', controller.chosePlayer1);
$('.playerBox2').on('click', controller.chosePlayer2);
$('.playerBox1Win').on('click', controller.playAgainPlayer1);
$('.playerBox2Win').on('click', controller.playAgainPlayer2);
},
gameboardCreation: function() {
for (var i = 0; i < 8; i++) {
var row = $('<div>').addClass('row');
for (var col = 0; col < 8; col++) {
var cell = $('<div>').addClass('cell')
.attr('position', i + '-' + col);
for (var j = 0; j < 1; j++){
var innerDiv = $('<div>').addClass("chip hideChip")
.text("o");
$(cell).append(innerDiv);
}
$(row).append(cell);
}
$('#gameboard').append(row);
}
},
addChipToBoard: function(targetCell, player){
if(player === 0){
targetCell.find('.chip').removeClass('hideChip').addClass('blue');
} else {
targetCell.find('.chip').removeClass('hideChip').addClass('orange');
}
},
flipChip: function(domElement){
domElement.addClass('rotateY90');
setTimeout(()=>{
domElement.toggleClass('orange blue');
domElement.removeClass('rotateY90');
}, 500)
},
playerTurn: function(player) { //adds glow class to player stats to show player turn, removes glow from opponent
if (player === 0) {
$('.leftAside').addClass('glowBlue');
$('.rightAside').removeClass('glowOrange');
} else if (player === 1) {
$('.rightAside').addClass('glowOrange');
$('.leftAside').removeClass('glowBlue');
}
},
removeGhostOutlines: function(cellArray){
for (var i=0; i<cellArray.length; i++){
cellArray[i].location.find('.chip').removeClass('chipGhostOutline')
}
},
addGhostOutlines: function(cellArray){ //displays light outline of disc to show player available moves
for (var i=0; i<cellArray.length; i++){
cellArray[i].location.find('.chip').addClass('chipGhostOutline');
}
},
removeModal: function(){
$('.introModalContainer').css('transform', 'scale(0)');
setTimeout(function(){
$('main').css('opacity', '1');
}, 1000);
},
displayChipCount: function(){
$('.counter1').text(model.player1ChipCount);
$('.counter2').text(model.player2ChipCount);
},
addPlayer1Glow: function(){ //add glow to player selection in intro model
$('.playerBox1').addClass('playerBox1Clicked');
$('.playerBox2').removeClass('playerBox2Clicked');
$('.playerBox1Win').addClass('playerBox1WinClicked');
$('.playerBox2Win').removeClass('playerBox2WinClicked');
},
addPlayer2Glow: function(){ //add glow to player selection in intro model
$('.playerBox2').addClass('playerBox2Clicked');
$('.playerBox1').removeClass('playerBox1Clicked');
$('.playerBox2Win').addClass('playerBox2WinClicked');
$('.playerBox1Win').removeClass('playerBox1WinClicked');
},
addWinnerModal: function(){
$('.winModalContent').css('transform', 'scale(1)');
},
removeWinnerModal: function(){
$('.winModalContent').css('transform', 'scale(0)');
controller.playAgain();
},
gameboardAnnihilation: function(){
$('#gameboard').empty();
},
};
//////////////////////////////////////////////////////////////
/////////////// CONTROLLER //////////////////
//////////////////////////////////////////////////////////////
var controller = {
createBoard: function(){
view.gameboardCreation();
model.createGridArrayMatrix();
},
gameStart: function(numberOfPlayers){ //called when 'enter' button is clicked. Removes model, checks positions available for player one, based on user choice.
view.removeModal();
view.playerTurn(model.player);
model.currentAvailableSpots = controller.checkAvailableSpots(model.player);
view.addGhostOutlines(model.currentAvailableSpots);
model.ai = numberOfPlayers;
},
chosePlayer1: function(){ //changes player data in model object to reflect users player choice, lights up in model
model.player = 0;
view.addPlayer1Glow();
},
chosePlayer2: function(){ //changes player data in model object to reflect users player choice, lights up in model
model.player = 1;
view.addPlayer2Glow();
},
InitialChips: function() { //adds the initial four chips to the board at initiation
view.addChipToBoard($(model.grid[3][3].location), 0);
view.addChipToBoard($(model.grid[3][4].location), 1);
view.addChipToBoard($(model.grid[4][3].location), 1);
view.addChipToBoard($(model.grid[4][4].location), 0);
model.addChipData(3, 3, 0);
model.addChipData(3, 4, 1);
model.addChipData(4, 3, 1);
model.addChipData(4, 4, 0);
},
checkAvailableSpots: function(player){ //checks all 8 surrounding positions for available plays based on opponent discs and makes them clickable.
var available = [];
for(var i = 0; i < model.grid.length; i++){
for(var j = 0; j < model.grid[i].length; j++){
if(model.grid[i][j].occupied === false && model.grid[i][j].clickable === true){
if(model.checkSurroundingChips(i, j, player)){
available.push(model.grid[i][j]);
}
}
}
}
return available;
},
addChipToGame: function() {
if (!model.humanTurn && !model.aiTurn) {
model.humanTurn = true;
var targetCell = $(this);
var targetPosition;
var y;
var x;
for (var i = 0; i < model.currentAvailableSpots.length; i++) {
if (targetCell[0] === model.currentAvailableSpots[i].location[0]) {
targetPosition = targetCell.attr('position').split('-');
y = parseInt(targetPosition[0]);
x = parseInt(targetPosition[1]);
view.removeGhostOutlines(model.currentAvailableSpots);
view.addChipToBoard(targetCell, model.player);
model.addChipData(y, x, model.player);
for (var j = 0; j < model.directionMap.length; j++) {
var dir = model.directionMap[j];
var currentCheck = model.checkDir(y, x, model.player, dir);
if (currentCheck) {
for (var k = 0; k < currentCheck.length; k++) {
view.flipChip(currentCheck[k].location.find('.chip'));
model.flipChipData(currentCheck[k], model.player);
}
}
}
afterMove();
}
}
function afterMove() {
view.displayChipCount();
model.player = 1 - model.player; //switches player at turn end
model.currentAvailableSpots = controller.checkAvailableSpots(model.player);
if (model.currentAvailableSpots[0] === undefined) {
model.player = 1 - model.player;
model.currentAvailableSpots = controller.checkAvailableSpots(model.player);
}
view.playerTurn(model.player); //switches player glow to opposite player at turn end
if (model.player === model.ai) {
controller.aiMove();
} else {
view.addGhostOutlines(model.currentAvailableSpots);
}
controller.checkWinState();
}
setTimeout(function(){
model.humanTurn = false;
}, 500)
}
},
aiMove: function(){
model.aiTurn = true;
var targetSpot = model.getAiSpot();
setTimeout(function() {
model.aiTurn = false;
if (targetSpot) {
targetSpot.location.click();
}
}, (Math.random() * 1000 + 2000));
},
checkWinState: function(){
var winState = model.checkWinStats();
if (winState === 0){
setTimeout(function(){
$('.winnerModalHeader').text('Tron Wins!');
view.addWinnerModal();
}, 1000);
} else if (winState === 1){
setTimeout(function(){
$('.winnerModalHeader').text('Clu Wins!');
view.addWinnerModal();
}, 1000);
} else if (winState === 2){
setTimeout(function() {
$('.winnerModalHeader').text('It\'s a draw!');
view.addWinnerModal();
}, 1000);
}
},
playAgain: function(ai){
// will need to bind the controller object to "this" when we call this function from our click handler
view.gameboardAnnihilation();
model.gridAnnihilation();
model.statReset();
controller.createBoard();
controller.InitialChips();
view.displayChipCount();
if (model.ai === null){
model.ai = null;
} else {
model.ai = ai;
}
this.gameStart(model.ai);
},
playAgainPlayer1: function(){
controller.chosePlayer1();
view.removeWinnerModal();
controller.playAgain(1);
},
playAgainPlayer2: function(){
controller.chosePlayer2();
view.removeWinnerModal();
controller.playAgain(0);
}
};