Repository navigation
Expand file tree
/
Copy pathcontroller.js
More file actions
73 lines (62 loc) · 2.47 KB
/
Copy pathcontroller.js
File metadata and controls
73 lines (62 loc) · 2.47 KB
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
// FUNCTIONS
/**
* CreateBoard procedure make all the cells inside the board.
* Pre: Recieve a grid, and amount of Cells to create (Number).
* Post: Modify the grid append with cells.
*/
function createBoard (grid, amountCells) {
// Create cells inside the grid
for(let i = 0; i < amountCells; i++){
// Create an base element
let cell = document.createElement('div');
// Set attributes and class
cell.setAttribute('id', `${i}`);
cell.classList.add('cell');
// Insert inside the grid
grid.appendChild(cell);
}
// Create the last 10 taken cells
for(let i = 300; i < TAKEN_AMOUNT_CELLS + 300; i++){
// Create an base element
let cell = document.createElement('div');
// Set attributes and class
cell.setAttribute('id', `${i}`);
cell.classList.add('taken');
// Insert inside the grid
grid.appendChild(cell);
}
}
function draw (currentTetromino, squares, currentPosition) {
currentTetromino.forEach( index => {
squares[index + currentPosition].classList.add('tetromino');
});
}
function undraw (currentTetromino, squares, currentPosition) {
currentTetromino.forEach( index => {
squares[index + currentPosition].classList.remove('tetromino');
});
}
function moveDown (currentTetromino, squares, current) {
undraw(currentTetromino, squares, current.position);
current.position += SETP_DOWN;
draw(currentTetromino, squares, current.position);
freeze (currentTetromino, squares, current);
}
function freeze (currentTetromino, squares, current) {
// test
console.log(currentTetromino.some( index => squares[current.position + index + width].classList.contains('taken') ), currentTetromino)
if( currentTetromino.some( index => squares[current.position + index + width].classList.contains('taken') ) ){
//
currentTetromino.forEach( index => squares[current.position + index].classList.add('taken'));
// Randomly select a Tetromino and a rotation
randomTetromino = nextRandom;
nextRandom = Math.floor(Math.random()*theTetrominoes.length);
// let currentRotation = Math.floor(Math.random()*4);
// Select one Tetromino
currentTetromino = theTetrominoes[nextRandom][0]
console.log(currentTetromino)
current.position = 4;
// Draw initially
// draw(currentTetromino, squares, current.position);
}
}