[SOLVED] javaScript - multidimensional array (with some restrictions)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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();
// Array size
ms = [30, 20];
for (i=0;i<ms[1];i++) {
a[i] = new Array();
for (j=0;j<ms[0];j++) {
a[i][j] = Math.floor(Math.random() * 5);
}
}
console.log(a);
Now I've got the following questions. I'm not sure how to start:
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,
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
var ms = [2, 5];
var idx = _.shuffle(_.range(ms[0]*ms[1])); // You may use for-loop to gen. array and random indexes+splice to pick numbers.
var i, j, n, nums;
var count_3=0, count_2=0;
for(var k=0, len=idx.length; k<len; k++) {
i = Math.floor( idx[k]/ms[1] );
j = idx[k] % ms[1];
console.log("IDX", k, i, j);
nums = _.shuffle([0, 1, 2, 3]);
for(var l=0; l<4; i++) {
n = nums[l];
(n==3) && (count_3++);
(n==2) && (count_2++);
// check all restrictions for number `num` in cell (i, j) here, save to array and break if satisfied
.....
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.