javascript 在javascript中用随机数填充二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25005561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filling up a 2D array with random numbers in javascript
提问by Gatreh
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!
如果以前在这里发布过类似的东西,我真的很抱歉,但我找不到任何东西,我还是这个网站的新手!
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)
所以一段时间以来,我一直在通过 html5 和 javascript 学习一些关于游戏开发的知识,我偶然发现了制作瓦片集地图,我现在有一个瓦片集和一个我想放入某些瓦片的二维数组(数量在 6在这种情况下为 10)。我认为这可能是一个很酷的功能,可以让地图在一小组相似的瓷砖之间进行选择,这样我就不必专门为数组中的每个瓷砖编号(只需定义类型)
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;
}
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:
这是我到目前为止一直在使用的代码,我必须使用 tile() 函数编辑代码的每个元素以在每个元素中获得一个随机数,我想要的是这样的:
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.
我有一种感觉,我缺少返回函数或类似的东西,但老实说我不知道。
回答by A1rPun
You were thinking in the right direction but there are some errors in your code ;)
您的想法是正确的,但您的代码中存在一些错误;)
- You have to initialize the array first before you can push elements into it.
- And you were counting i++ twice
- 您必须先初始化数组,然后才能将元素推入其中。
- 你数了两次 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;
}
}
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);
回答by cgatian
Here's a quick example. I've created a function that will take in a width and height parameter and generate the size requested. Also I placed your tile function inside generate ground to keep it private, preventing other script from invoking it.
这是一个快速示例。我创建了一个函数,它将接受宽度和高度参数并生成请求的大小。此外,我将您的 tile 函数放在 generate ground 内以保持它的私密性,防止其他脚本调用它。
var ground = generateGround(10, 10); //Simple usage
function generateGround(height, width)
{
var ground = [];
for (var y = 0 ; y < height; y++)
{
ground[y] = [];
for (var x = 0; x < width; x++)
{
ground[y][x] = tile();
}
}
return ground;
function tile()
{
return (Math.random() * 5 | 0) + 6;
}
}
回答by colinmcp
Try removing the comma from...
尝试从...中删除逗号
ground[[i],[j]] = (Math.random() * 5 | 0) + 6;
...in your 'clean' version. Also, your incrementing 'i' in both for loops:
...在您的“干净”版本中。此外,您在两个 for 循环中都增加了 'i':
for (var i = 0 ; i < 15; i++) {
for (var j = 0; j < 9; i++) {
Hopefully these changes make it work for you :)
希望这些更改使其对您有用:)