From cf5c2313fdc7fb9e821a3637e5c920919f69e861 Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Fri, 23 Nov 2012 00:35:46 +0100 Subject: [PATCH] Further enhancments of code. --- tetris-ai-robertko.js | 20 ++++++++++++++------ tetris.js | 22 +++++++++++----------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/tetris-ai-robertko.js b/tetris-ai-robertko.js index 418e024..25bc1e6 100644 --- a/tetris-ai-robertko.js +++ b/tetris-ai-robertko.js @@ -147,14 +147,22 @@ function rankAmountHoles(brickId, dropLocation, tempBoard) { var currentMalus = mod_rank_hole_malus; // for each row check how many empty tiles there are right under: + var isStarted = false; + var x = bounds[0][0] + i; - var y = lowestTiles[i] - 1; + var y = game.height - 1; while (y >= 0) { - if (game.board[x][y] == 0) { - totalRankBonus -= currentMalus; - currentMalus *= mod_rank_hole_mult; - } else { - currentMalus *= mod_rank_hole_decay; + if (tempBoard[x][y] != 0) { + isStarted = true; + } + + if (isStarted) { + if (tempBoard[x][y] == 0) { + totalRankBonus -= currentMalus; + currentMalus *= mod_rank_hole_mult; + } else { + currentMalus *= mod_rank_hole_decay; + } } y--; } diff --git a/tetris.js b/tetris.js index 7cf73bc..73db08a 100644 --- a/tetris.js +++ b/tetris.js @@ -279,16 +279,16 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { return false; } - this.getFilledLines = function(b) { - if (typeof(b) === 'undefined') b = this.board; + this.getFilledLines = function(board) { + if (typeof(board) === 'undefined') board = this.board; var lines = []; - for (var y = 0; y < b[0].length; y++) { + for (var y = 0; y < board[0].length; y++) { var isLineFilled = true; - for (var x = 0; x < b.length; x++) { - if (b[x][y] == 0) { + for (var x = 0; x < board.length; x++) { + if (board[x][y] == 0) { isLineFilled = false; } } @@ -300,7 +300,7 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { return lines; } - this.removeLine = function(linenr) { + this.removeLine = function(board, linenr) { if (typeof(linenr) === 'undefined') { return; } @@ -308,13 +308,13 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { for (var y = parseInt(linenr); y < this.height; y++) { for (var x = 0; x < this.width; x++) { if (y+1 >= this.height) { - this.board[x][y] = 0; + board[x][y] = 0; } else { - this.board[x][y] = this.board[x][y+1]; + board[x][y] = board[x][y+1]; } } } - } + } this.updateLineCounter = function() { if (this.lineCounterElement != 0) { @@ -330,9 +330,9 @@ function TetrisGame(canvasId, linesCounterId, nextBricksCanvasId) { } - var filledLines = this.getFilledLines(); + var filledLines = this.getFilledLines(this.board); for (var i = 0; i < filledLines.length; i++) { - this.removeLine(filledLines[i]-i); + this.removeLine(this.board, filledLines[i]-i); } this.stats.linesCleared(filledLines.length);