From 1c1041cfbac3b5ea3b15428b60227cccdab1cf0b Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Thu, 22 Mar 2012 20:46:58 +0100 Subject: [PATCH] Fixed the order in which clearing of lines and obtaining new bricks is happening. --- tetris-ai.js | 2 +- tetris.js | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/tetris-ai.js b/tetris-ai.js index 391f61b..479fff5 100644 --- a/tetris-ai.js +++ b/tetris-ai.js @@ -84,7 +84,7 @@ function startAI() { var action = chooseAction(dropLocation); doAction(action); - window.setTimeout(startAI, 100); + window.setTimeout(startAI, 50); } $(document).ready(function() { diff --git a/tetris.js b/tetris.js index 88b0365..17fb678 100644 --- a/tetris.js +++ b/tetris.js @@ -218,14 +218,8 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { return false; } - this.moveCurrentBrickDown = function() { - if (this.canCurrentBrickMove(0, -1)) { - this.currentBrickLoc[1] -= 1; - this.redraw(); - return true; - } + this.integrateCurrentIntoBoard = function () { - // If not, integrate into board: var shape = this.brickShape(this.currentBrickId, this.currentBrickRot); for (var i = 0; i < 4; i++) { var x = this.currentBrickLoc[0]+shape[i][0]; @@ -238,8 +232,24 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { this.board[x][y] = this.bricks[this.currentBrickId]; } + + } + + this.moveCurrentBrickDown = function() { + if (this.canCurrentBrickMove(0, -1)) { + this.currentBrickLoc[1] -= 1; + this.redraw(); + return true; + } - this.getNextCurrentBrick(); + // If it can't move, trye to integrate it into board: + if (this.integrateCurrentIntoBoard() == false) { + return false; + } + + this.currentBrickId = 0; + this.currentBrickLoc = 0; + this.currentBrickRot = 0; return true; } @@ -247,7 +257,7 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { while (this.canCurrentBrickMove(0,-1)) { this.moveCurrentBrickDown(); } - return this.moveCurrentBrickDown(); + return this.integrateCurrentIntoBoard(); } this.rotateCurrentBrick = function() { @@ -307,6 +317,7 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { return false; } + var filledLines = this.getFilledLines(); for (var i = 0; i < filledLines.length; i++) { this.removeLine(filledLines[i]-i); @@ -316,6 +327,10 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { this.updateLineCounter(); + if (this.currentBrickId == 0) { + this.getNextCurrentBrick(); + } + this.drawUnlock(); this.redraw();