-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
133 lines (119 loc) · 3.3 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
var BOARD_HEIGHT = 22;
var BOARD_WIDTH = 10;
// Board Constructor
function Board() {
this.width = BOARD_WIDTH;
this.height = BOARD_HEIGHT;
this.theBoard = [this.height];
for (var i = 0; i < this.height; i++) {
this.theBoard[i] = [this.width];
for(var j = 0; j < this.width; j++) {
this.theBoard[i][j] = new Block();
this.theBoard[i][j].setCoord(i, j);
}
}
}
// toString() of Board
Board.prototype.print = function() {
var returnString = "<table id='board'>";
for(var i = 0; i < this.height; i++) {
// print top number markers
// if(i === 0) {
// returnString += "<tr><th class='board_title'>G</th>";
// for(var k = 0; k < this.width; k++) {
// returnString += "<th>" + k + "</th>";
// }
// returnString += "</tr>";
// }
if(i === 0 || i === 1) {
continue;
}
returnString += "<tr>"
// if(i < 10) {
// returnString += "<td class='board_title'>" + i + "</td>";
// } else {
// returnString += "<td class='board_title'>" + i + "</td>";
// }
for(var j = 0; j < this.width; j++) {
returnString += "<td class='block_" + this.theBoard[i][j].blockType.toLowerCase() + "'>";
returnString += this.theBoard[i][j].print() + "";
returnString += "</td>"
}
returnString += "</tr>";
}
returnString += "</table>"
return returnString;
};
Board.prototype.reset = function() {
for (var i = 0; i < this.height; i++) {
this.theBoard[i] = [this.width];
for(var j = 0; j < this.width; j++) {
this.theBoard[i][j] = new Block();
}
}
};
Board.prototype.getBlock = function(row, col) {
return this.theBoard[row][col];
};
Board.prototype.addTetromino = function(tetromino) {
// get x and y of the blocks, and add it to the board.
tetromino.board = this;
// check if any of the blocks are already occupied.
for(var i = 0; i < 4; i++) {
if(this.getBlock(tetromino.blocks[i].row, tetromino.blocks[i].col).isAlive()) {
tetromino = null;
console.warn("Cannot Create Another Tetromino!");
return false;
}
}
tetromino.findGhostRow();
tetromino.setGhostAlive();
for(var i = 0; i < 4; i++) {
this.setAlive(tetromino.blocks[i].row, tetromino.blocks[i].col, tetromino.blocks[i].blockType);
}
return true;
};
Board.prototype.removeTetromino = function(tetromino) {
tetromino.setGhostDead();
for(var i = 0; i < 4; i++) {
this.setDead(tetromino.blocks[i].row, tetromino.blocks[i].col, tetromino.blocks[i].blockType);
}
tetromino.reset();
return true;
};
Board.prototype.setDead = function(row, col) {
this.theBoard[row][col].setDead()
};
Board.prototype.setAlive = function(row, col, blockType) {
this.theBoard[row][col].setAlive(blockType);
};
// findFullLines () -> int
// loop through theBoard array and return the number of rows are full
Board.prototype.findFullLine = function() {
var isFull = true;
var count = 0;
for(var i = 0; i < this.height; i++) {
isFull = true;
for(var j = 0; j < this.width; j++) {
if(this.theBoard[i][j].isDead()) {
// as soon as one block is dead, no need to see the rest
isFull = false;
break;
}
}
if(isFull) {
count++;
// remove this row;
this.removeRow(i);
console.log(i, " row removed!");
}
}
return count;
};
Board.prototype.removeRow = function(row) {
for(var i = row; i > 2; i--) {
for(var j = 0; j < this.width; j++) {
this.theBoard[i][j].blockType = this.theBoard[i - 1][j].blockType;
}
}
};