-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameClass.js
More file actions
131 lines (123 loc) · 5.87 KB
/
Copy pathgameClass.js
File metadata and controls
131 lines (123 loc) · 5.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
var Game = function(){
this.board = this.newGame();
};
Game.prototype.newGame = function() {
return [[BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK]]
};
Game.prototype.printBoard = function() {
console.log(this.board[0] + "\n" + this.board[1] + "\n" +
this.board[2] + "\n" + this.board[3] + "\n" +
this.board[4] + "\n" + this.board[5]);
};
Game.prototype.placePiece = function(col, piece) {
//for col === 1 => search board[5][0], board[4][0]...
if(!piece){
piece = RED;
};
var colTrans = col - 1, rowInd, colInd;
for(var i = 5; i >= 0; i--){ //search col for 1st empty
if(this.board[i][colTrans] === BLANK){
this.board[i][colTrans] = piece;
rowInd = i;
break;
}
}
return [rowInd, colTrans, piece];
};
Game.prototype.markBoard = function(rowColPieceArr){
var row = rowColPieceArr[0].toString();
var col = rowColPieceArr[1].toString();
var piece = rowColPieceArr[2].toString();
var workOn = document.querySelector('[data-matrixval="'+row+'x'+col+'"]');
workOn.style.background = piece;
};
Game.prototype.clearBoard = function() {
var t = document.getElementById("game"),
tableRows = t.getElementsByTagName("tr"),
r = [], i, len, tds, j, jlen;
for ( i =0, len = tableRows.length; i<len; i++) {
tds = tableRows[i].getElementsByTagName('td');
for( j = 0, jlen = tds.length; j < jlen; j++) {
tds[j].style.background = 'white';
}
}
};
Game.prototype.spotCheck = function(modRow, modCol, piece) {
var modRow = modRow;
var modCol = modCol;
var piece = piece;
return (!!(this.board[modRow]) && !!(this.board[modRow][modCol]) && this.board[modRow][modCol] === piece);
};
Game.prototype.winCheck = function(rowColPieceArr){
var row = rowColPieceArr[0];
var col = rowColPieceArr[1];
var piece = rowColPieceArr[2];
//START of Horizontal win condition checks
if(this.spotCheck(row, col + 1, piece) && this.spotCheck(row, col + 2, piece) && this.spotCheck(row, col + 3, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row, col + 1, piece) && this.spotCheck(row, col + 2, piece) && this.spotCheck(row, col - 1, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row, col + 1, piece) && this.spotCheck(row, col - 1, piece) && this.spotCheck(row, col - 2, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row, col - 1, piece) && this.spotCheck(row, col - 2, piece) && this.spotCheck(row, col - 3, piece)){
console.log(piece + "'s Win!")
return true;
//END of Horizontal win condition checks
//-------------------------------------------------------------------------------------------------------------------
//START of Vertical win condition checks
} else if(this.spotCheck(row + 1, col, piece) && this.spotCheck(row + 2, col, piece) && this.spotCheck(row + 3, col, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row + 1, col, piece) && this.spotCheck(row + 2, col, piece) && this.spotCheck(row - 1, col, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row + 1, col, piece) && this.spotCheck(row - 1, col, piece) && this.spotCheck(row - 2, col, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row - 1, col, piece) && this.spotCheck(row - 2, col, piece) && this.spotCheck(row - 3, col, piece)){
console.log(piece + "'s Win!")
return true;
//END of Vertical win condition checks
//-------------------------------------------------------------------------------------------------------------------
//START of Diagonal down-right win condition checks
} else if(this.spotCheck(row + 1, col + 1, piece) && this.spotCheck(row + 2, col + 2, piece) && this.spotCheck(row + 3, col + 3, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row + 1, col + 1, piece) && this.spotCheck(row + 2, col + 2, piece) && this.spotCheck(row - 1, col - 1, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row + 1, col + 1, piece) && this.spotCheck(row - 1, col - 1, piece) && this.spotCheck(row - 2, col - 2, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row - 1, col - 1, piece) && this.spotCheck(row - 2, col - 2, piece) && this.spotCheck(row - 3, col - 3, piece)){
console.log(piece + "'s Win!")
return true;
//END of Diagonal down-right win condition checks
//-------------------------------------------------------------------------------------------------------------------
//START of Diagonal down-left win condition checks
} else if(this.spotCheck(row + 1, col - 1, piece) && this.spotCheck(row + 2, col - 2, piece) && this.spotCheck(row + 3, col - 3, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row + 1, col - 1, piece) && this.spotCheck(row + 2, col - 2, piece) && this.spotCheck(row - 1, col + 1, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row + 1, col - 1, piece) && this.spotCheck(row - 1, col + 1, piece) && this.spotCheck(row - 2, col + 2, piece)){
console.log(piece + "'s Win!")
return true;
} else if(this.spotCheck(row - 1, col + 1, piece) && this.spotCheck(row - 2, col + 2, piece) && this.spotCheck(row - 3, col + 3, piece)){
console.log(piece + "'s Win!")
return true;
//END of Diagonal down-left win condition checks
//-------------------------------------------------------------------------------------------------------------------
} else {
return false;
}
};