javaScript - multidimensional array (with some restrictions)
Hi all,
I have created a multidimensional array in JavaScript and filled it with some random integers from 0 to 4. That was an easy part: Code:
a = new Array(); a) limit the number of occurrences of certain number: I need to have only 4 instances of number 3 and 1 instance of number 2. b) all instances of number 1 must be adjacent (creating a path in a maze) It is going to be a randomly generated maze map with 0 - walls, 1 - paths, 2 - altars, 3 - doors, Is it doable in a relatively simple way? |
Hi.
Maybe keep generating random numbers until all restrictions for a given cell are satisfied? Say, assign 1 only if there is already one adjacent 1 or check counter of 3's if 3 was generated. To avoid possible bias in distribution of numbers due to ordered filling of the array (row-by-row) one may also take indices from shuffled array of indices, something like (untested) Code:
var _ = require('underscore'); // in Nodejs |
Quote:
|
Thank you guys for your input. In the meantime, I think I found a working example of what I need.
http://jsfiddle.net/bigbadwaffle/YeazH/ |
All times are GMT -5. The time now is 06:48 AM. |