Remove full lines

This commit is contained in:
Arnaud Vergnet 2020-03-16 14:58:13 +01:00
parent 3fe1d85eec
commit 8fc5cfb25e

View file

@ -43,16 +43,21 @@ export default class GameLogic {
return this.gameRunning;
}
getEmptyGrid() {
let grid = [];
for (let row = 0; row < this.getHeight(); row++) {
grid.push([]);
getEmptyLine() {
let line = [];
for (let col = 0; col < this.getWidth(); col++) {
grid[row].push({
line.push({
color: this.colors.tetrisBackground,
isEmpty: true,
});
}
return line;
}
getEmptyGrid() {
let grid = [];
for (let row = 0; row < this.getHeight(); row++) {
grid.push(this.getEmptyLine());
}
return grid;
}
@ -81,6 +86,31 @@ export default class GameLogic {
isEmpty: false,
};
}
this.clearLines(this.getLinesToClear(coord));
}
clearLines(lines: Array<number>) {
lines.sort();
for (let i = 0; i < lines.length; i++) {
this.currentGrid.splice(lines[i], 1);
this.currentGrid.unshift(this.getEmptyLine());
}
}
getLinesToClear(coord: Object) {
let rows = [];
for (let i = 0; i < coord.length; i++) {
let isLineFull = true;
for (let col = 0; col < this.getWidth(); col++) {
if (this.currentGrid[coord[i].y][col].isEmpty) {
isLineFull = false;
break;
}
}
if (isLineFull && rows.indexOf(coord[i].y) === -1)
rows.push(coord[i].y);
}
return rows;
}
isTetrominoPositionValid() {