-
Notifications
You must be signed in to change notification settings - Fork 0
/
columns.html
381 lines (319 loc) · 8.39 KB
/
columns.html
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<html>
<head><title>Columns</title>
<style>
body {
display: grid;
grid-template-columns: 150px auto 200px;
gap: 10px;
}
section {
margin: 10px;
display: grid;
cursor: pointer;
}
section div {
width: 19px;
height: 19px;
}
#board div {
background: lightgrey;
}
#board {
grid-template-columns: repeat(10, 20px);
grid-template-rows: repeat(20, 20px);
}
#nextPiece {
grid-template-columns: repeat(4, 20px);
grid-template-rows: repeat(4, 20px);
}
button { margin-bottom: 20px;}
div.blu { background: blue !important; }
div.ora { background: darkorange !important; }
div.gre { background: green !important; }
div.pur { background: purple !important; }
div.cyn { background: darkcyan !important; }
div.yel { background: goldenrod !important; }
div.red { background: red !important; }
div.blink { border: 2px solid black; }
</style>
</head>
<body>
<div>
<h1>Columns</h1>
<button id="start">Start</button>
<div>Score</div>
<div id=score>0</div>
<p id=message></p>
</div>
<section id=board>
<!-- 20x10 empty div elements will be added here. -->
</section>
<div>
Next Piece:
<section id=nextPiece>
<!-- 4x4 empty div elements will be added here. -->
</section>
</div>
<script>
const LEFT_CODE = 37, UP_CODE = 38, RIGHT_CODE = 39, DOWN_CODE = 40;
const BOARD_WIDTH = 10, BOARD_HEIGHT = 20;
const RGB = {
width: 1, height: 3,
cells: [['red'], ['blu'], ['gre']],
};
const GBY = {
width: 1,
height: 3,
cells: [['gre'], ['blu'], ['yel']],
};
const BYO = {
width: 1,
height: 3,
cells: [['blu'], ['yel'], ['ora']],
};
const OYR = {
width: 1,
height: 3,
cells: [['yel'], ['ora'], ['red']],
};
const YOB = {
width: 1,
height: 3,
cells: [['yel'], ['ora'], ['blu']],
};
const OBG = {
width: 1,
height: 3,
cells: [['ora'], ['blu'], ['gre']],
};
const PIECES =
[RGB, GBY, OYR, YOB, OBG, BYO];
let currentPiece, nextPiece;
let score = 0, message = '';
// Create the HTML elements
let nextPieceArea = document.querySelector("#nextPiece");
for (let r=0; r<4; r++)
for (let c=0; c<4; c++) {
let newDiv = document.createElement('div');
newDiv.dataset.r = r;
newDiv.dataset.c = c;
nextPieceArea.appendChild(newDiv);
}
let nextPiecePixelList = document.querySelectorAll('#nextPiece div');
let boardArea = document.querySelector("#board");
for (let r=0; r<BOARD_HEIGHT; r++)
for (let c=0; c<BOARD_WIDTH; c++) {
let newDiv = document.createElement('div');
newDiv.dataset.r = r;
newDiv.dataset.c = c;
boardArea.appendChild(newDiv);
}
let pixelList = document.querySelectorAll('#board div');
let pixels = [];
let pixelListIndex = 0;
for (let r = 0; r<BOARD_HEIGHT; r++)
pixels.push([]);
for (pixel of pixelList) {
let r = Math.floor(pixelListIndex / BOARD_WIDTH);
pixels[r].push(pixel);
pixelListIndex++;
}
let running = false;
// Board is matrix of pixels that start with no color.
// The board only holds pieces that have landed, not the currently falling piece.
let board = [];
function initializeBoard() {
board = [];
for (let r=0; r<BOARD_HEIGHT; r++) {
let row = [];
board.push(row);
for (let c=0; c<BOARD_WIDTH; c++) {
row.push('');
}
}
}
function render() {
for (let r=0; r<BOARD_HEIGHT; r++)
for (let c=0; c<BOARD_WIDTH; c++)
pixels[r][c].className = board[r][c];
if (currentPiece) {
for (let r=0; r<currentPiece.height; r++)
for (let c=0; c<currentPiece.width; c++) {
if (currentPiece.cells[r][c])
pixels[r + currentPiece.y][c + currentPiece.x].className = currentPiece.cells[r][c];
}
}
if (nextPiece) {
let idx = 0;
for (let r=0; r<4; r++)
for (let c=0; c<4; c++) {
nextPiecePixelList[idx].className = (
(nextPiece.cells[r] && nextPiece.cells[r][c]) || '');
idx++;
}
}
document.querySelector('#score').innerText = score;
document.querySelector('#message').innerText = message;
}
initializeBoard();
render();
const NORTH = {dx: 0, dy: -2};
const SOUTH = {dx: 0, dy: 2};
const EAST = {dx: -2, dy: 0};
const WEST = {dx: 2, dy: 0};
let turnLength = 700;
function startNewPiece() {
currentPiece = nextPiece;
nextPiece = PIECES[Math.floor(Math.random() * PIECES.length)];
// Copy all the parts of the peice so that we can modify it.
currentPiece = JSON.parse(JSON.stringify(currentPiece));
currentPiece.x = Math.floor(BOARD_WIDTH / 2) - 2;
currentPiece.y = 0;
}
render();
function rotatePiece(tempPiece) {
newCells = [[tempPiece.cells[2][0]],
[tempPiece.cells[0][0]],
[tempPiece.cells[1][0]]];
tempPiece.cells = newCells;
}
// Would the falling piece be in conflict if it was at this location?
function hasConflict(tempPiece) {
for (let r=0; r<tempPiece.height; r++)
for (let c=0; c<tempPiece.width; c++) {
if (tempPiece.cells[r][c] == '') continue;
// check world boundaries
if (tempPiece.y + r >= BOARD_HEIGHT) return true;
if (tempPiece.x + c < 0 || tempPiece.x + c >= BOARD_WIDTH) return true;
// check previously landed pieces
if (board[tempPiece.y + r][tempPiece.x + c]) return true;
}
return false;
}
function isLanded() {
let tempPiece = JSON.parse(JSON.stringify(currentPiece));
tempPiece.y++;
return hasConflict(tempPiece);
}
function addPieceToBoard() {
for (let r=0; r<currentPiece.height; r++)
for (let c=0; c<currentPiece.width; c++) {
if (currentPiece.cells[r][c] == '') continue;
board[currentPiece.y + r][currentPiece.x + c] = currentPiece.cells[r][c];
}
score += 10;
}
function compact() {
// move everything down as far as possible into any empty cells.
for (let c=0; c<BOARD_WIDTH; c++) {
let lowestOpenRow = null;
for (let r=BOARD_HEIGHT - 1; r>1; r--) {
if (board[r][c] == '' && lowestOpenRow == null) {
lowestOpenRow = r;
continue;
}
if (lowestOpenRow) {
board[lowestOpenRow][c] = board[r][c];
board[r][c] = '';
lowestOpenRow = r;
}
}
}
}
function checkMatch(r, c, dr, dc, cellsToRemove) {
if (board[r] && board[r][c]) {
let color = board[r][c];
if (board[r+dr] && board[r+dr][c+dc] == color &&
board[r+2*dr] && board[r+2*dr][c+2*dc] == color) {
cellsToRemove.push({r: r, c: c});
cellsToRemove.push({r: r + dr, c: c + dc});
cellsToRemove.push({r: r + 2 * dr, c: c + 2 * dc});
}
}
}
function findAndRemoveMatches() {
let cellsToRemove = [];
for (let r=0; r < BOARD_HEIGHT; r++) {
for (let c=0; c < BOARD_WIDTH; c++) {
checkMatch(r, c, 1, 0, cellsToRemove);
checkMatch(r, c, 0, 1, cellsToRemove);
checkMatch(r, c, 1, 1, cellsToRemove);
checkMatch(r, c, 1, -1, cellsToRemove);
}
}
score += cellsToRemove.length ** 2 * 100;
turnLength = Math.max(250, turnLength - Math.floor(cellsToRemove.length / 3));
for (let cell of cellsToRemove)
board[cell.r][cell.c] = '';
if (cellsToRemove) compact();
}
function gameOver() {
running = false;
message = 'Game over';
currentPiece = null;
render();
}
function update() {
if (isLanded()) {
addPieceToBoard();
findAndRemoveMatches();
startNewPiece();
if (hasConflict(currentPiece)) gameOver();
} else {
// apply gravity
currentPiece.y++;
}
}
function gameLoop() {
if (!running) return;
update();
render();
window.setTimeout(gameLoop, turnLength);
}
function start() {
running = true;
nextPiece = PIECES[Math.floor(Math.random() * PIECES.length)];
startNewPiece();
initializeBoard();
gameLoop();
message = 'Greetings!';
}
function moveLeft() {
let tempPiece = JSON.parse(JSON.stringify(currentPiece));
tempPiece.x--;
if (!hasConflict(tempPiece)) {
currentPiece.x--;
render();
}
}
function moveRight() {
let tempPiece = JSON.parse(JSON.stringify(currentPiece));
tempPiece.x++;
if (!hasConflict(tempPiece)) {
currentPiece.x++;
render();
}
}
function rotate() {
let tempPiece = JSON.parse(JSON.stringify(currentPiece));
rotatePiece(tempPiece);
if (!hasConflict(tempPiece)) {
rotatePiece(currentPiece);
render();
}
}
function advance() {
update();
render();
}
document.querySelector('#start').addEventListener('click', start);
document.addEventListener('keydown', (e) => {
if (!running) return;
if (e.keyCode == LEFT_CODE) moveLeft();
if (e.keyCode == RIGHT_CODE) moveRight();
if (e.keyCode == UP_CODE) rotate();
if (e.keyCode == DOWN_CODE) advance();
});
</script>
</body>
</html>