用JavaScript中的随机数填充二维数组 [英] Filling up a 2D array with random numbers in javascript

查看:138
本文介绍了用JavaScript中的随机数填充二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常抱歉,如果以前曾在此处发布过类似信息,但我找不到任何信息,我还是该网站的新手!

I'm really sorry if anything like this has been posted here before but I couldn't find anything, I'm kinda new to the site still!

所以一段时间以来,我一直在学习一些通过html5和javascript进行游戏开发的知识,偶然发现了tilelet地图,现在我拥有了tileet和2D我要放入特定图块的数组(在这种情况下,数字在6到10之间变化)。
我认为这可能是一个很酷的功能,可以让地图在一小组相似的图块之间进行选择,因此我不必专门为数组中的每个图块编号(只需定义类型)

So for a while now I've been learning a bit about game development through html5 and javascript and I stumbled upon making tileset maps, I now have a tileset and an 2D array that I want to put certain tiles in (the number varies between 6 and 10 in this case). I figured it could be a cool function to make the map choose between a small set of similar tiles so I don't have to specifically number every tile in the array(just define the type)

我目前拥有的方法可能是能够定义类型的最佳方法,但是我想要看起来更干净的东西和/或有关为什么我的更干净版本无法正常工作的信息。 / p>

The method I have currently is probably the best for being able to define types but I want something that looks a bit cleaner and/or information to why my "cleaner" version dosen't work.

var ground = [
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
    [tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()]];

function tile() {
    var y = (Math.random() * 5 | 0) + 6;
    return y;
}

这是我到目前为止一直在使用的代码,我必须进行编辑每个带有tile()函数的代码元素都将获得一个随机数,我想要的是这样的:

This is the code I've been using so far, I have to edit every element of the code with the tile() function to get a random number in each one, what I wanted to have was something like this:

for (var i = 0 ; i < 15; i++) {
    for (var j = 0; j < 9; j++) {
        ground[[i],[j]] = (Math.random() * 5 | 0) + 6;
    }
}

无需添加函数即可填充数组每个点。

to fill the array without having to add the function to each spot.

我觉得我缺少返回函数或类似的东西,但老实说我不知道​​。

I have a feeling that I'm missing a return function or something along those lines but honestly I have no idea.

推荐答案

您在朝着正确的方向思考,但是您的代码中存在一些错误;)

You were thinking in the right direction but there are some errors in your code ;)


  • 必须先初始化数组,然后才能将元素压入数组。

  • 并且您在计算i ++的次数两次

JavaScript

Javascript

var ground = []; // Initialize array
for (var i = 0 ; i < 15; i++) {
    ground[i] = []; // Initialize inner array
    for (var j = 0; j < 9; j++) { // i++ needs to be j++
        ground[i][j] = (Math.random() * 5 | 0) + 6;
    }
}

也许更好(可重用)

function createGround(width, height){
    var result = [];
    for (var i = 0 ; i < width; i++) {
        result[i] = [];
        for (var j = 0; j < height; j++) {
            result[i][j] = (Math.random() * 5 | 0) + 6;
        }
    }
    return result;
}
// Create a new ground with width = 15 & height = 9
var ground = createGround(15, 9);

这篇关于用JavaScript中的随机数填充二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆