LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2015, 11:49 PM   #1
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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,

Is it doable in a relatively simple way?
 
Old 12-20-2015, 08:43 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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

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
    .....
  }
}

Last edited by firstfire; 12-20-2015 at 09:13 AM.
 
Old 12-20-2015, 10:43 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by sycamorex View Post
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?
If you want a walkable maze, you can't just throw random numbers into an array. See for example http://pcg.wikidot.com/pcg-algorithm:dungeon-generation
 
Old 12-20-2015, 02:34 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Joining elements from a multidimensional array elonden Programming 7 09-02-2011 02:22 AM
perl - multidimensional array toredo Programming 3 06-11-2010 08:29 AM
Perl multidimensional array zhjim Programming 9 08-21-2009 09:59 AM
multidimensional array (php) niehls Programming 2 01-31-2003 05:34 AM
PHP: Multidimensional array problem lhoff Programming 1 06-10-2002 02:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration