-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-four.js
245 lines (214 loc) · 8.36 KB
/
connect-four.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
/*
Write a function to determine who is the winner of a Connect Four game. It returns false if nobody won. Otherwise, it returns A or B depending on which player has won.
*/
const PLAYER_A = 'A';
const PLAYER_B = 'B';
const EMPTY = ' ';
const NUMBER_OF_ROWS = 6;
const NUMBER_OF_COLUMNS = 7;
const NUMBER_OF_CELLS_TO_CONNECT = 4;
const EMPTY_GRID = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
];
const HORIZONTAL_WINNER = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[PLAYER_A, PLAYER_B, EMPTY, PLAYER_A, PLAYER_A, PLAYER_A, PLAYER_A],
[PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_B, PLAYER_B]
];
const VERTICAL_WINNER = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, EMPTY],
[PLAYER_A, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, EMPTY],
[PLAYER_B, PLAYER_B, EMPTY, EMPTY, EMPTY, PLAYER_A, EMPTY],
[PLAYER_B, PLAYER_A, EMPTY, EMPTY, PLAYER_B, PLAYER_B, PLAYER_B]
];
const UPWARD_DIAGONAL_WINNER = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, PLAYER_B, PLAYER_A, EMPTY, EMPTY],
[EMPTY, EMPTY, PLAYER_B, PLAYER_B, PLAYER_B, EMPTY, EMPTY],
[EMPTY, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A, EMPTY, EMPTY],
[PLAYER_A, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_A, PLAYER_A, EMPTY]
];
const DOWNARD_DIAGONAL_WINNER = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, PLAYER_A, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, PLAYER_B, PLAYER_A, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, PLAYER_B, PLAYER_A, PLAYER_A, EMPTY, EMPTY, EMPTY],
[EMPTY, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_A, PLAYER_B, EMPTY],
[EMPTY, PLAYER_B, PLAYER_B, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A]
];
const DRAW = [
[PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_B],
[PLAYER_B, PLAYER_B, PLAYER_B, PLAYER_A, PLAYER_A, PLAYER_A, PLAYER_B],
[PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A],
[PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_B],
[PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A, PLAYER_B],
[PLAYER_A, PLAYER_B, PLAYER_B, PLAYER_B, PLAYER_A, PLAYER_B, PLAYER_A]
];
const EDGE_CASE_HORIZONTAL = [
[EMPTY, EMPTY, EMPTY, PLAYER_A, PLAYER_A, PLAYER_A, PLAYER_A],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
];
const EDGE_CASE_VERTICAL = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, PLAYER_A],
[EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, PLAYER_A, PLAYER_A]
];
const EDGE_CASE_UPWARD_DIAGONAL = [
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, PLAYER_A],
[EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_A, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, PLAYER_A, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
];
const EDGE_CASE_DOWNARD_DIAGONAL = [
[EMPTY, EMPTY, EMPTY, PLAYER_B, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PLAYER_B],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
];
const getCellOwner = function(cell) {
switch (cell) {
default: {
return cell;
}
case EMPTY: {
return false;
}
}
};
const cellsAreConnected = cells => {
return (
cells[0] === getCellOwner(cells[1]) &&
cells[0] === getCellOwner(cells[2]) &&
cells[0] === getCellOwner(cells[3])
);
};
const connectedHorizontally = function(columnIndex, row) {
// there are not 3 cells on the right of the cell at this index
const columnOffset = columnIndex + NUMBER_OF_CELLS_TO_CONNECT;
if (columnOffset > NUMBER_OF_COLUMNS) {
return false;
}
const cells = [
row[columnIndex],
row[columnIndex + 1],
row[columnIndex + 2],
row[columnIndex + 3]
];
return cellsAreConnected(cells) && cells[0];
};
const connectedVertically = function(rowIndex, columnIndex, grid) {
// there are not 3 cells on top of the cell at this index
const rowOffset = rowIndex - NUMBER_OF_CELLS_TO_CONNECT + 1;
if (rowOffset < 0) {
return false;
}
const cells = [
grid[rowIndex][columnIndex],
grid[rowIndex - 1][columnIndex],
grid[rowIndex - 2][columnIndex],
grid[rowIndex - 3][columnIndex]
];
return cellsAreConnected(cells) && cells[0];
};
const connectedUpwardDiagonally = function(rowIndex, columnIndex, grid) {
// there are not 3 cells on top of the cell at this index
// there are not 3 cells on the rigth of the cell at this index
const rowOffset = rowIndex - NUMBER_OF_CELLS_TO_CONNECT + 1;
const columnOffset = columnIndex + NUMBER_OF_CELLS_TO_CONNECT;
if (rowOffset < 0 || columnOffset > NUMBER_OF_COLUMNS) {
return false;
}
const cells = [
grid[rowIndex][columnIndex],
grid[rowIndex - 1][columnIndex + 1],
grid[rowIndex - 2][columnIndex + 2],
grid[rowIndex - 3][columnIndex + 3]
];
return cellsAreConnected(cells) && cells[0];
};
const connectedDownardDiagonally = function(rowIndex, columnIndex, grid) {
// there are not 3 cells on top of the cell at this index
// there are not 3 cells on the rigth of the cell at this index
const rowOffset = rowIndex - NUMBER_OF_CELLS_TO_CONNECT + 1;
const columnOffset = columnIndex + NUMBER_OF_CELLS_TO_CONNECT;
if (rowOffset < 0 || columnOffset > NUMBER_OF_COLUMNS) {
return false;
}
const cells = [
grid[rowIndex][columnIndex + 3],
grid[rowIndex - 1][columnIndex + 2],
grid[rowIndex - 2][columnIndex + 1],
grid[rowIndex - 3][columnIndex]
];
return cellsAreConnected(cells) && cells[0];
};
const getWinner = function(grid = EMPTY_GRID) {
console.table(grid);
for (let i = NUMBER_OF_ROWS - 1; i >= 0; i--) {
const row = grid[i];
const rowIsEmpty = !row.filter(cell => cell !== EMPTY);
if (!rowIsEmpty) {
for (let j = 0; j < NUMBER_OF_COLUMNS; j++) {
const horizontalWinner = connectedHorizontally(j, row);
if (horizontalWinner) {
return horizontalWinner;
}
const verticalWinner = connectedVertically(i, j, grid);
if (verticalWinner) {
return verticalWinner;
}
const upwardDiagonalWinner = connectedUpwardDiagonally(
i,
j,
grid
);
if (upwardDiagonalWinner) {
return upwardDiagonalWinner;
}
const downardDiagonalWinner = connectedDownardDiagonally(
i,
j,
grid
);
if (downardDiagonalWinner) {
return downardDiagonalWinner;
}
}
}
}
return false;
};
// pass a grid as an argument to get which player won if any
const winner = getWinner(HORIZONTAL_WINNER);
switch (winner) {
default: {
console.log(`The winner is player ${winner}.`);
break;
}
case false: {
console.log('Nobody has won.');
break;
}
}