-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (158 loc) · 5.06 KB
/
index.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
let numRows = 0;
let numColumns = 0;
let playerTurn = 2;
let state = [];
function setViewState() {
const firstChild = gameEl().firstChild;
if (firstChild) {
gameEl().removeChild(firstChild);
}
const game = document.createElement('div');
game.id = 'game-container';
updateInstructions(game);
for (let rowNum = 0; rowNum < state.length; rowNum++) {
const row = document.createElement('div');
row.className = 'row';
for (let column = 0; column < state[rowNum].length; column++) {
const cell = document.createElement('div');
cell.className = `cell player-${state[rowNum][column]}`;
cell.setAttribute('data-row', rowNum);
cell.setAttribute('data-column', column);
cell.onclick = onMove;
row.appendChild(cell);
}
game.appendChild(row);
}
gameEl().appendChild(game);
}
function startGame() {
const rowsColsStr = prompt('Please enter a string in of comma separated integers in the format "rows, columns".');
if (rowsColsStr) {
const rowsCols = rowsColsStr.split(',').map(n => Number(n));
if (rowsCols.length === 2 && rowsCols.every(num => !isNaN(num))) {
// Assemble array
const grid = [];
const [rows, columns] = rowsCols;
for (let i = 0; i < rows; i++) {
grid[i] = [];
for (let x = 0; x < columns; x++) {
grid[i].push(0);
}
}
state = grid;
setViewState(state);
} else {
// set error state
const invalidDiv = document.createElement('div');
invalidDiv.id = 'invalid-input';
invalidDiv.innerText = 'Invalid rows/columns input. Please click restart and enter a valid value.'
gameEl().appendChild(invalidDiv);
}
}
}
function setWinnerViewState() {
const game = gameEl();
document.getElementById('instructions').remove();
document.getElementById('player-move').remove();
document.getElementById('game-container').remove();
const winEl = document.createElement('h2');
winEl.className = 'winner';
winEl.innerText = `Player ${playerTurn === 1 ? 2 : 1} wins!!!`;
game.prepend(winEl);
}
function getWinner() {
let winner = 0;
for (let rowNum = state.length - 1; rowNum > -1; rowNum--) {
const row = state[rowNum];
for (let colNum = 0; colNum < row.length; colNum++) {
// Check surrounding for consecutive
const cellValue = row[colNum];
if (cellValue === 0) {
continue;
}
let horizStart = colNum;
while (row[horizStart - 1] === row[horizStart]) horizStart--;
const inARowHoriz = row.slice(horizStart).filter((cellNum, index, arr) => index === 0 || cellNum === row[horizStart]);
if (inARowHoriz.length === 4) {
return cellValue;
}
let verticalStart = rowNum;
let verticalInARow = 1;
while (verticalStart > -1 && state[verticalStart - 1] && state[verticalStart - 1][colNum] === cellValue) {
verticalStart--;
verticalInARow++;
};
if (verticalInARow > 3) {
return cellValue;
}
// Check Diagonal forward
const diagForwardStart = [rowNum, colNum];
let diagForwardInARow = 1;
while (
diagForwardStart[0] > -1 && diagForwardStart[1] < row.length &&
state[diagForwardStart[0] - 1][diagForwardStart[1] + 1] === cellValue
) {
diagForwardStart[0]--;
diagForwardStart[1]++;
diagForwardInARow++;
}
if (diagForwardInARow > 3) {
return cellValue;
}
// Check diagonal backward
const diagBackward = [rowNum, colNum];
let diagBackInARow = 1;
while (
diagBackward[0] > -1 && diagBackward[1] < row.length &&
state[diagBackward[0] - 1][diagBackward[1] - 1] === cellValue
) {
diagBackward[0]--;
diagBackward[1]--;
diagBackInARow++;
}
if (diagBackInARow > 3) {
return cellValue;
}
}
}
return winner;
}
function onMove(clickEvent) {
const cell = clickEvent.target;
const column = Number(cell.getAttribute('data-column'));
for (let rowNum = 0; rowNum < state.length; rowNum++) {
if (!state[rowNum + 1] || state[rowNum + 1][column] !== 0) {
state[rowNum][column] = playerTurn;
break;
}
}
setViewState();
const winner = getWinner();
if (winner > 0) {
setWinnerViewState();
}
}
function gameEl() {
return document.getElementById('game');
}
function updateInstructions(gameContainer) {
playerTurn = playerTurn === 1 ? 2 : 1;
const playerMoveEl = document.createElement('h3');
playerMoveEl.innerText = `Player ${playerTurn}'s move!`;
playerMoveEl.id = 'player-move';
gameContainer.appendChild(playerMoveEl);
const instEl = document.createElement('p');
instEl.id = 'instructions';
instEl.innerText = 'Click inside any column to drop your piece into the column';
gameContainer.appendChild(instEl);
[1,2].forEach(num => {
const cell = document.createElement('div');
cell.className = `cell example player-${num}`;
cell.innerText = `Player ${num} color`
gameContainer.appendChild(cell);
});
}
startGame();
document.getElementById('restart').onclick = () => {
startGame();
}