简体   繁体   English

二维数组随机生成器jQuery

[英]two dimensional array random generator jQuery

All rows always have same numbers, why? 所有行总是有相同的数字,为什么? They should take random number to populate it. 他们应该使用随机数来填充它。 Also, how can I repair it? 另外,我该如何修理? Once I saw answer here but now I cannot find it. 一旦我在这里看到答案,但现在找不到了。

 var mapSizex = 5; var mapSizey = 6; var mapArray = []; $(function() { console.log("ready!"); $('#map-draw').html(drawMap()); }); function mapGenerator() { for (i = 0; i < mapSizex; i++) { for (x = 0; x < mapSizey; x++) { mapArray[i, x] = getRandom(1, 5); } } } function drawMap() { mapGenerator(); var map = ''; tileID = 0; for (i = 0; i < mapSizex; i++) { map = map + '<br style="clear: both;">'; for (x = 0; x < mapSizey; x++) { map = map + '<div class="tile tileID' + tileID + '">' + mapArray[i, x] + '</div>'; tileID++; } } return map; } function getRandom(min, max) { var x = Math.floor((Math.random() * max) + min); return x; } 
 .tile { float: left; height: 20px; width: 20px; border: 1px solid black; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main-container"> <div id="map-container"> <div id="map-draw"></div> </div> </div> 

To post it I need to add some more content but all included information's should be enough to find what I mean. 要发布它,我需要添加更多内容,但是所有包含的信息都应该足以找到我的意思。

here is a link to JSFIDDLE woking code 这是JSFIDDLE唤醒代码的链接

It should be mapArray[i][x] and I added mapArray[i]=[]; 应该是mapArray[i][x] ,我添加了mapArray[i]=[]; in the outer loop. 在外循环中。

here is your fixed code: 这是您的固定代码:

var mapSizex=5;
var mapSizey=6;
var mapArray=[];

$(function() {
    console.log( "ready!" );
    $('#map-draw').html(drawMap());
});

function mapGenerator(){
    for(i=0;i<mapSizex;i++){
    mapArray[i]=[];
        for(x=0;x<mapSizey;x++){
            mapArray[i][x]= getRandom(1,5);
      console.log(i,x,getRandom(1,5))
        }
    }
}
function drawMap(){
    mapGenerator();
  console.log(mapArray)
    var map='';
    tileID=0;
    for(i=0;i<mapSizex;i++){
        map=map+'<br style="clear: both;">';
        for(x=0;x<mapSizey;x++){
            map=map+'<div class="tile tileID'+tileID+'">'+mapArray[i][x]+'</div>';
            tileID++;
        }
    }return map;
}
function getRandom(min,max) {
    var x = Math.floor((Math.random() * max) + min);
    return x;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM