-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.js
168 lines (133 loc) · 4.46 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
/**
* @constructor blocks.Board
* @param {Object} board - Configuration object.
* @param {jQuery} board.$frame - Table element to construct the board in.
* @param {number} board.height - Height of the board.
* @param {number} board.width - Width of the board.
* @param {number} board.over - Number of hidden overflow cells to add.
*/
blocks.Board = function (board) {
var i, j;
this.$frame = board.$frame;
this.$board = $("<tbody>").appendTo(this.$frame);
this.rows = [];
// these are pseudo-constants
// the true dimensions of the board
this._height = board.height + board.over;
this._width = board.width + 2 * board.over;
// indices denoting the visible board
this._vis_w_start = board.over;
this._vis_w_end = board.over + board.width;
this._vis_h_start = board.over;
this._vis_h_end = board.over + board.height;
//board includes two non-visible rows at the top, where pieces start
//and two non-visible columns on each side
this._board = blocks.Board._blank_board(this._height, this._width);
for (i = 0; i < this._height; i++) {
var row = [], $row = $("<tr>");
this.rows.push(row);
this.$board.append($row);
this._row($row, row)
if (i < this._vis_h_start || i >= this._vis_h_end) {
$row.children().addClass("hidden");
}
}
};
blocks.Board._blank_board = function (height, width) {
var i, j, board = [], row;
for (i = 0; i < height; i++) {
row = [];
for (j = 0; j < width; j++) {
row.push(0);
}
board.push(row)
}
return board;
};
// fill out a row
blocks.Board.prototype._row = function ($row, row) {
var i;
for (i = 0; i < this._width; i++) {
var $cell = $("<td>");
if (i < this._vis_w_start || i >= this._vis_w_end) {
$cell.addClass("hidden");
}
$row.append($cell);
row.push($cell);
}
};
// helper function
blocks.Board._piece_action = function (action) {
return function (piece) {
var i, j, res;
for (i = piece.position.top; i < piece.position.top + piece.dim; i++) {
for (j = piece.position.left; j < piece.position.left + piece.dim; j++) {
if (piece.map[piece.direction][i - piece.position.top][j - piece.position.left]) {
res = action.call(this, piece, i, j);
if (res) {
return true;
}
}
}
}
return false;
};
};
// write a piece to the board, at the position in the piece
//SHOULD BE WRITE_PIECE, or change the others to remove _piece
blocks.Board.prototype.write = blocks.Board._piece_action(function (piece, i, j) {
var pos = piece.position;
this._board[i][j] =
piece.map[piece.direction][i - pos.top][j - pos.left];
});
blocks.Board.prototype.set_piece = blocks.Board._piece_action(function (piece, i, j) {
var pos = piece.position;
this.rows[i][j].css("background-color", blocks.colors[
piece.map[piece.direction][i - pos.top][j - pos.left]
]);
});
//clear a square starting from position and going dim
blocks.Board.prototype.clear_piece = blocks.Board._piece_action(function (piece, i, j) {
this.rows[i][j].css("background-color", blocks.colors[0]);
});
blocks.Board.prototype.check_piece = blocks.Board._piece_action(function (piece, i, j) {
return this._board[i + 1][j];
});
//remove full lines
//return the number of lines removed
blocks.Board.prototype.clear_lines = function () {
var self = this;
var cnt = 0, i, j, lines = [], full;
for (i = 0; i < this._height; i++) {
full = true;
for (j = this._vis_w_start; j < this._vis_w_end; j++) {
if (0 === this._board[i][j]) {
full = false;
break;
}
}
if (full) {
lines.push(i);
cnt++;
}
}
lines.forEach(function (line) {
var row = [], $row = $("<tr>"), _row = [];
self._row($row, row);
$(self.$board.children()[line]).remove();
$row.insertAfter(self.$board.children()[1]);
self.rows.splice(line, 1, row);
for (i = 0; i < self._width; i++) {
self._board[line][i] = 0;
}
});
return cnt;
};
// debuggging function
blocks.Board.prototype.print = function () {
var i;
for (i = 0; i < this._height; i++) {
console.log(this._board[i]);
}
};