-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.js
154 lines (130 loc) · 4.06 KB
/
board.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
class Board {
constructor() {
// these variables are for the timing of the piece dropping
this.lastTime = 0;
this.dropCounter = -1000;
this.dropInterval = 1000;
// how long they've been holding an arrow key down for
this.downFor = 0;
// define the bottom and right of the board per how many gridSizes
// value is in pixels
this.lowerLimit = gridHeight*gridSize;
this.rightLimit = gridWidth*gridSize;
this.difficulty = 'Easy';
this.diffColor = 'green';
this.gridOn = true;
// personal best
this.best = getCookie('personal_best');
// game state variables
this.score = 0;
this.gameStarted = false;
this.gameOver = false;
// create a board grid, size (gridHeight) x (gridWidth)
// boxes in the board are referenced:
// matrix[row][col]
// with [0][0] at the top-left
let empty = new Array(gridHeight);
for (let j=0; j<empty.length; j++) {
empty[j] = new Array(gridWidth);
for (let i=0; i<gridWidth; i++) {
empty[j][i] = {
pos: {x: i, y: j},
color: {r: 255, g: 255, b: 255},
value: 0
};
}
}
this.matrix = empty;
} // end of constructor()
deleteRow(row) {
// go through each row, starting at the full one, up to the top
while (row > 0) {
for (let i = 0; i < this.matrix[row].length; i++) {
// move each row down by changing
// its attributes to the one above it
if (this.matrix[row-1][i].value == 1) {
this.matrix[row][i].value = 1;
this.matrix[row][i].color = this.matrix[row-1][i].color;
}
else {
this.matrix[row][i].value = 0;
this.matrix[row][i].color = {r: 255, g: 255, b: 255};
}
}
row--;
}
}
// checks if any rows are full
checkIfRowsFull() {
// go through each row
for (let j=1; j<this.matrix.length; j++) {
// go through each column
for (let i=0; i<this.matrix[j].length; i++) {
if (this.matrix[j][i].value == 0) {
// go to next row
break;
}
// we are at the last column, all values are 1
else if (i == this.matrix[j].length-1) {
// delete row j
this.deleteRow(j);
this.checkIfRowsFull();
return;
}
}
}
return;
}
// add a new piece to the board
addPiece(piece) {
for (let j=0; j<piece.matrix.length; j++) {
for (let i=0; i<piece.matrix[j].length; i++) {
let value = piece.matrix[j][i];
// new piece overlaps with board, game over
if (value == 1 && this.matrix[j+piece.pos.y][i+piece.pos.x].value == 1) {
this.gameOver = true;
}
}
}
if (!this.gameOver) {
// go through every element in the piece (4x4)
for (let j=0; j<piece.matrix.length; j++) {
for (let i=0; i<piece.matrix[j].length; i++) {
let value = piece.matrix[j][i];
// if its value is 1, save its state in the (Board)
if (value == 1) {
this.matrix[j+piece.pos.y][i+piece.pos.x].value = 1;
this.matrix[j+piece.pos.y][i+piece.pos.x].color.r = piece.color.r;
this.matrix[j+piece.pos.y][i+piece.pos.x].color.g = piece.color.g;
this.matrix[j+piece.pos.y][i+piece.pos.x].color.b = piece.color.b;
}
}
}
// check if any rows are full
this.checkIfRowsFull();
}
}
// draw the entire board
draw() {
// go through every element of the entire board matrix (gridHeight) x (gridWidth)
for (let j=0; j<this.matrix.length; j++) {
for (let i=0; i<gridWidth; i++) {
// if the value is 1, draw the box (gridSize) x (gridSize)
let box = this.matrix[j][i];
if (box.value == 1 || this.gridOn) {
fill(
box.color.r,
box.color.g,
box.color.b
);
rect(
box.pos.x*gridSize,
box.pos.y*gridSize,
gridSize,
gridSize
);
}
}
}
} // end of draw()
} // end of Board class