From 8fc5cfb25e5dc313549e89f725fd383d23b4a17e Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Mon, 16 Mar 2020 14:58:13 +0100 Subject: [PATCH] Remove full lines --- screens/Tetris/GameLogic.js | 44 +++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/screens/Tetris/GameLogic.js b/screens/Tetris/GameLogic.js index 80deca8..05a14eb 100644 --- a/screens/Tetris/GameLogic.js +++ b/screens/Tetris/GameLogic.js @@ -43,16 +43,21 @@ export default class GameLogic { return this.gameRunning; } + getEmptyLine() { + let line = []; + for (let col = 0; col < this.getWidth(); col++) { + line.push({ + color: this.colors.tetrisBackground, + isEmpty: true, + }); + } + return line; + } + getEmptyGrid() { let grid = []; for (let row = 0; row < this.getHeight(); row++) { - grid.push([]); - for (let col = 0; col < this.getWidth(); col++) { - grid[row].push({ - color: this.colors.tetrisBackground, - isEmpty: true, - }); - } + grid.push(this.getEmptyLine()); } return grid; } @@ -81,6 +86,31 @@ export default class GameLogic { isEmpty: false, }; } + this.clearLines(this.getLinesToClear(coord)); + } + + clearLines(lines: Array) { + 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() {