Commit a51f866a authored by Terence Martin's avatar Terence Martin

Change bonus brick generation to be per column

This works the same as the gray brick code. In fact, so similar that
it seems like I should actually make that one method that does both.
parent be1798e4
...@@ -754,15 +754,11 @@ var nurdz; ...@@ -754,15 +754,11 @@ var nurdz;
*/ */
var GRAY_BRICK_PROBABILITY = [0, 0, 0, 0, 0, 1, 1, 1, 2, 2]; var GRAY_BRICK_PROBABILITY = [0, 0, 0, 0, 0, 1, 1, 1, 2, 2];
/** /**
* The chance (percentage) that a row will contain any bonus bricks. * The relative probabilty of the number of gray bricks that appear in a
*/ * column. One of these elements is randomly selected in order to determine
var BONUS_BRICK_CHANCE = 40; * the number of gray bricks in a column.
/**
* The minimum and maximum number of gray bricks that are generated per row.
* This is only used after BONUS_BRICK_CHANCE has been used to determine if
* there will be any bricks at all.
*/ */
var BONUS_BRICKS_PER_ROW = [1, 2]; var BONUS_BRICK_PROBABILITY = [0, 0, 0, 0, 0, 0, 0, 1, 1, 2];
/** /**
* This class contains the code used to generate the content of a new maze. * This class contains the code used to generate the content of a new maze.
* It requires access to the Maze entity so that it can get at the content * It requires access to the Maze entity so that it can get at the content
...@@ -838,7 +834,7 @@ var nurdz; ...@@ -838,7 +834,7 @@ var nurdz;
* *
* @returns {number} the maximum number of bonus bricks in a maze * @returns {number} the maximum number of bonus bricks in a maze
*/ */
get: function () { return (game.MAZE_HEIGHT - 4) * BONUS_BRICKS_PER_ROW[1]; }, get: function () { return (game.MAZE_HEIGHT - 4) * Math.max.apply(null, BONUS_BRICK_PROBABILITY); },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
...@@ -1111,29 +1107,26 @@ var nurdz; ...@@ -1111,29 +1107,26 @@ var nurdz;
* randomly generated and might be 0. * randomly generated and might be 0.
*/ */
MazeGenerator.prototype.genBonusBricks = function () { MazeGenerator.prototype.genBonusBricks = function () {
// Iterate over all of the rows that can possibly contain bricks. We // Iterate over all of the columns that can possibly contain gray
// start two rows down to make room for the initial ball locations // bricks.
// and the empty balls, and we stop 2 rows short to account for the for (var x = 1; x < game.MAZE_WIDTH - 1; x++) {
// border of the maze and the goal row. // Determine how many bricks we will generate in this column;
for (var row = 2; row < game.MAZE_HEIGHT - 2; row++) { // there may be zero.
// See if we should bother generating any bricks in this row var brickCount = this.randomProbabilty(BONUS_BRICK_PROBABILITY);
// at all.
if (game.Utils.randomIntInRange(0, 100) > BONUS_BRICK_CHANCE)
continue;
// First, we need to determine how many bricks we will generate
// for this row.
var brickCount = game.Utils.randomIntInRange(BONUS_BRICKS_PER_ROW[0], BONUS_BRICKS_PER_ROW[1]);
// Now keep generating bricks into this row until we have // Now keep generating bricks into this row until we have
// generated enough. // generated enough.
while (brickCount > 0) { while (brickCount > 0) {
// Generate a column randomly. If this location is already // Generate a row. We start 3 rows down (0 offset) to leave
// filled or the square above is an arrow, try again. // room for the initial ball placement and a single row of
var column = this.genRandomMazeColumn(); // potential unobstructed movement.
if (this._contents.getCellAt(column, row) != null || var y = game.Utils.randomIntInRange(2, game.MAZE_HEIGHT - 3);
(this._contents.cellNameAt(column, row - 1) == "arrow")) // Get the contents at this location; if this location is
// already filled, or the tile above it is an arrow, then
// try again (a arrow stops this brick from being useful).
if (this._contents.getCellAt(x, y) != null ||
this._contents.cellNameAt(x, y - 1) == "arrow")
continue; continue;
// This cell contains brick; resurrect one from the object // Get a brick from the pool; leave if we can't.
// pool.
var brick = this._maze.getBonusBrick(); var brick = this._maze.getBonusBrick();
if (brick == null) { if (brick == null) {
console.log("Ran out of bonus bricks generating maze"); console.log("Ran out of bonus bricks generating maze");
...@@ -1141,7 +1134,7 @@ var nurdz; ...@@ -1141,7 +1134,7 @@ var nurdz;
} }
// Add it to the maze, mark it to appear, and count it as // Add it to the maze, mark it to appear, and count it as
// placed. // placed.
this._contents.setCellAt(column, row, brick); this._contents.setCellAt(x, y, brick);
brick.appear(); brick.appear();
brickCount--; brickCount--;
} }
......
...@@ -30,16 +30,11 @@ module nurdz.game ...@@ -30,16 +30,11 @@ module nurdz.game
const GRAY_BRICK_PROBABILITY = [0, 0, 0, 0, 0, 1, 1, 1, 2, 2]; const GRAY_BRICK_PROBABILITY = [0, 0, 0, 0, 0, 1, 1, 1, 2, 2];
/** /**
* The chance (percentage) that a row will contain any bonus bricks. * The relative probabilty of the number of gray bricks that appear in a
*/ * column. One of these elements is randomly selected in order to determine
const BONUS_BRICK_CHANCE = 40; * the number of gray bricks in a column.
/**
* The minimum and maximum number of gray bricks that are generated per row.
* This is only used after BONUS_BRICK_CHANCE has been used to determine if
* there will be any bricks at all.
*/ */
const BONUS_BRICKS_PER_ROW = [1, 2]; const BONUS_BRICK_PROBABILITY = [0, 0, 0, 0, 0, 0, 0, 1, 1, 2];
/** /**
* This class contains the code used to generate the content of a new maze. * This class contains the code used to generate the content of a new maze.
...@@ -119,7 +114,7 @@ module nurdz.game ...@@ -119,7 +114,7 @@ module nurdz.game
* @returns {number} the maximum number of bonus bricks in a maze * @returns {number} the maximum number of bonus bricks in a maze
*/ */
get maxBonusBricks () : number get maxBonusBricks () : number
{ return (MAZE_HEIGHT - 4) * BONUS_BRICKS_PER_ROW[1]; } { return (MAZE_HEIGHT - 4) * Math.max.apply (null, BONUS_BRICK_PROBABILITY); }
/** /**
* Construct a new generator object that can generate mazes into the * Construct a new generator object that can generate mazes into the
...@@ -461,34 +456,31 @@ module nurdz.game ...@@ -461,34 +456,31 @@ module nurdz.game
*/ */
private genBonusBricks () : void private genBonusBricks () : void
{ {
// Iterate over all of the rows that can possibly contain bricks. We // Iterate over all of the columns that can possibly contain gray
// start two rows down to make room for the initial ball locations // bricks.
// and the empty balls, and we stop 2 rows short to account for the for (let x = 1 ; x < MAZE_WIDTH - 1 ; x++)
// border of the maze and the goal row.
for (let row = 2 ; row < MAZE_HEIGHT - 2 ; row++)
{ {
// See if we should bother generating any bricks in this row // Determine how many bricks we will generate in this column;
// at all. // there may be zero.
if (Utils.randomIntInRange (0, 100) > BONUS_BRICK_CHANCE) let brickCount = this.randomProbabilty (BONUS_BRICK_PROBABILITY);
continue;
// First, we need to determine how many bricks we will generate
// for this row.
let brickCount = Utils.randomIntInRange (BONUS_BRICKS_PER_ROW[0], BONUS_BRICKS_PER_ROW[1]);
// Now keep generating bricks into this row until we have // Now keep generating bricks into this row until we have
// generated enough. // generated enough.
while (brickCount > 0) while (brickCount > 0)
{ {
// Generate a column randomly. If this location is already // Generate a row. We start 3 rows down (0 offset) to leave
// filled or the square above is an arrow, try again. // room for the initial ball placement and a single row of
let column = this.genRandomMazeColumn (); // potential unobstructed movement.
if (this._contents.getCellAt (column, row) != null || let y = Utils.randomIntInRange (2, MAZE_HEIGHT - 3);
(this._contents.cellNameAt (column, row - 1) == "arrow"))
// Get the contents at this location; if this location is
// already filled, or the tile above it is an arrow, then
// try again (a arrow stops this brick from being useful).
if (this._contents.getCellAt (x, y) != null ||
this._contents.cellNameAt (x, y - 1) == "arrow")
continue; continue;
// This cell contains brick; resurrect one from the object // Get a brick from the pool; leave if we can't.
// pool.
let brick = this._maze.getBonusBrick (); let brick = this._maze.getBonusBrick ();
if (brick == null) if (brick == null)
{ {
...@@ -498,7 +490,7 @@ module nurdz.game ...@@ -498,7 +490,7 @@ module nurdz.game
// Add it to the maze, mark it to appear, and count it as // Add it to the maze, mark it to appear, and count it as
// placed. // placed.
this._contents.setCellAt (column, row, brick); this._contents.setCellAt (x, y, brick);
brick.appear (); brick.appear ();
brickCount--; brickCount--;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment