-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
170 lines (149 loc) · 3.6 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
const GRID_HEIGHT = 6;
const GRID_WIDTH = 7;
const WINNING_LENGTH = 4;
const PLAYERS_COLORS = {
1: "yellow",
2: "red",
};
let actualPlayer = 1;
document.addEventListener("DOMContentLoaded", function () {
createGrid(GRID_HEIGHT, GRID_WIDTH);
changeTurnTo(1);
bindEvents();
});
function createGrid(height, width) {
const grid = document.querySelector(".grid");
for (let i = 0; i < width; i++) {
const col = document.createElement("div");
col.classList.add("col");
for (let j = 0; j < height; j++) {
const cell = document.createElement("div");
cell.classList.add("cell");
col.appendChild(cell);
}
grid.appendChild(col);
}
}
function bindEvents() {
const cols = document.querySelectorAll(".col");
cols.forEach((col, index) => {
col.addEventListener("click", () => {
placeToken(index);
});
});
}
function placeToken(col) {
const cells = document
.querySelectorAll(".col")
[col].querySelectorAll(".cell");
for (let i = 0; i < cells.length; i++) {
if (!cells[i].classList.contains("filled")) {
cells[i].classList.add("filled");
cells[i].classList.add(`player-${actualPlayer}`);
if (isWin()) {
setTimeout(() => {
alert(`${PLAYERS_COLORS[actualPlayer]} wins!`);
reset();
}, 100);
return;
}
changeTurnTo(actualPlayer === 1 ? 2 : 1);
break;
}
}
}
function changeTurnTo(player) {
actualPlayer = player;
document.querySelector(".grid").setAttribute("player-turn", player);
}
function isWin() {
// vertical
for (let i = 0; i < GRID_WIDTH; i++) {
let count = 0;
for (let j = 0; j < GRID_HEIGHT; j++) {
const cell = getCell(i, j);
if (isActualPlayer(cell)) {
count++;
if (count === WINNING_LENGTH) {
return true;
}
} else {
count = 0;
}
}
}
// horizontal
for (let i = 0; i < GRID_HEIGHT; i++) {
let count = 0;
for (let j = 0; j < GRID_WIDTH; j++) {
const cell = getCell(j, i);
if (isActualPlayer(cell)) {
count++;
if (count === WINNING_LENGTH) {
return true;
}
} else {
count = 0;
}
}
}
// diagonal bottom left to top right
for (
let i = -(GRID_WIDTH - 1 - WINNING_LENGTH);
i < GRID_WIDTH + 1 - WINNING_LENGTH;
i++
) {
let count = 0;
for (let j = 0; j < GRID_HEIGHT; j++) {
const cell = getCell(i + j, j);
if (isActualPlayer(cell)) {
count++;
if (count === WINNING_LENGTH) {
return true;
}
} else {
count = 0;
}
}
}
// diagonal bottom right to top left
for (
let i = -(GRID_WIDTH - 1 - WINNING_LENGTH);
i < GRID_WIDTH + 1 - WINNING_LENGTH;
i++
) {
let count = 0;
for (let j = 0; j < GRID_HEIGHT; j++) {
const cell = getCell(GRID_WIDTH - 1 - j - i, j);
if (isActualPlayer(cell)) {
count++;
if (count === WINNING_LENGTH) {
return true;
}
} else {
count = 0;
}
}
}
}
function isActualPlayer(cell) {
if (!cell) {
return false;
}
return cell.classList.contains(`player-${actualPlayer}`);
}
function getCell(col, row) {
if (col < 0 || col >= GRID_WIDTH || row < 0 || row >= GRID_HEIGHT) {
return null;
}
return document.querySelectorAll(".col")[col].querySelectorAll(".cell")[row];
}
function reset() {
const cells = document.querySelectorAll(".cell");
cells.forEach((cell) => {
cell.classList.remove("filled");
cell.classList.remove("player-1");
cell.classList.remove("player-2");
});
changeTurnTo(1);
}