-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
242 lines (161 loc) · 4.15 KB
/
script.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
// DOM Variables
var buttons = document.getElementsByClassName("btn");
var reset = document.getElementById("reset-btn");
var playerType = document.getElementById("player-type");
// Game Flow Variables
var playerNumber = 1; // Initially player - 1 gets to start his/her turn
var filledGrid = []; // Player board
var filledCells = 0; // No. of cells that has been filled
for(var i = 0; i < 6; i++) {
var arr = [-1 , -1 , -1 , -1 , -1 , -1 , -1]; // Board is initialised with -1
filledGrid.push(arr);
}
// Event Listener for Buttons
reset.addEventListener("click" , function() {
resetBoard();
});
for(var i = 0; i < buttons.length; i++) {
// Handing the Event when button was clicked
buttons[i].addEventListener("click" , function() {
// Make move and disable the button to avoid furthur clicking it again
var buttonNo = this.classList[1];
makeMove(this , buttonNo.slice(4));
});
}
// Function to Make Move on the passed button and disable it
function makeMove(button , buttonNo) {
var row = buttonNo % 7 === 0 ? Math.floor(buttonNo / 7) - 1 : Math.floor(buttonNo / 7);
var col = buttonNo % 7 === 0 ? 6: (buttonNo % 7) - 1;
if(playerNumber === 1) {
button.classList.add("btn-player-1");
filledGrid[row][col] = 1;
filledCells++;
if(playerWon(row , col , 1) === true) {
setTimeout(function() {
alert("Game Over: White Wins");
resetBoard();
} , 200);
}
// Update the player
playerNumber = 2;
playerType.textContent = "Player - 2";
} else {
button.classList.add("btn-player-2");
filledGrid[row][col] = 2;
filledCells++;
if(playerWon(row , col , 2) === true) {
setTimeout(function() {
alert("Game Over : Black Wins");
resetBoard();
} , 200);
}
// Update the player
playerNumber = 1;
playerType.textContent = "Player - 1";
}
// If all the cells has been filled
if(filledCells === 42) {
setTimeout(function() {
alert("Game Draw");
resetBoard();
} , 200);
return;
}
// Disable the button is the move is made
setTimeout(function () {
button.disabled = true;
},10);
}
function playerWon(row , col , player) {
var count = 0;
// Check for columns
for(var i = 0; i < 7; i++) {
if(filledGrid[row][i] === player) {
count++;
if(count === 4) return true;
} else {
count = 0;
}
}
count = 0;
// Check for Rows
for(var i = 0; i < 6; i++) {
if(filledGrid[i][col] === player) {
count++;
if(count === 4) return true;
} else {
count = 0;
}
}
count = 0;
// Check for primary diagonal
if(row >= col) {
var i = row - col;
var j = 0;
for(; i <= 5; i++ , j++) {
if(filledGrid[i][j] === player) {
count++;
if(count == 4) return true;
} else {
count = 0;
}
}
} else {
var i = 0;
var j = col - row;
for(; j <= 6; i++ , j++) {
if(filledGrid[i][j] === player) {
count++;
if(count == 4) return true;
} else {
count = 0;
}
}
}
count = 0;
// Check for secondary diagonal
if(row + col <= 5) {
var i = row + col;
var j = 0;
for(; i >= 0 && j <= row + col; i-- , j++) {
if(filledGrid[i][j] === player) {
count++;
if(count == 4) return true;
} else {
count = 0;
}
}
} else {
var i = 5;
var j = row + col - 5;
for(; j <= 6; j++ , i--) {
if(filledGrid[i][j] === player) {
count++;
if(count == 4) return true;
} else {
count = 0;
}
}
}
return false;
}
// Function to reset the Board completely
function resetBoard() {
// Remove all the disabled buttons and the styles
for(var i = 0; i < buttons.length; i++) {
buttons[i].disabled = false;
buttons[i].classList.remove("btn-player-1");
buttons[i].classList.remove("btn-player-2");
}
// Player Number is changed to 1
playerNumber = 1;
playerType.textContent = "Player - 1";
// Filled Cells is changed to 0
filledCells = 0;
// Filling the Board with -1
for(var i = 0; i < 6; i++) {
for(var j = 0; j < 7; j++) {
filledGrid[i][j] = -1;
}
}
}