在javascript中用随机数填充二维数组

在javascript中用随机数填充二维数组

本文介绍了在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 学习一些关于游戏开发的知识,我偶然发现了制作图块集地图,我现在有一个图块集和一个二维数组,我想将某些图块放入(数字在这种情况下,在 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)

我目前拥有的方法可能是能够定义类型的最佳方法,但我想要一些看起来更简洁的方法和/或有关为什么我的更简洁"的版本不起作用的信息.

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

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

也许更好(可重复使用)

Maybe even better (reusable)

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中用随机数填充二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:09