Apologies if this has been asked before, but I couldn't find any previous posts on the topic as I'm still fairly new to this site!
Lately, I've been exploring game development using HTML5 and JavaScript and have gotten into creating tileset maps. I currently have a tileset and a 2D array where I want to assign specific tiles (usually between 6 and 10). I thought it would be neat to create a function that allows the map to randomly select from a small set of similar tiles, rather than assigning a number to each tile in the array.
The method I'm using now works for defining types, but I'm looking for a cleaner solution or an explanation of why my "cleaner" version isn't working.
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()],
// More rows...
];
function tile() {
var y = (Math.random() * 5 | 0) + 6;
return y;
}
This is the code I've been using so far, where I have to manually add the 'tile()' function to each element to generate random numbers. What I'd like is something like:
for (var i = 0; i < 15; i++) {
for (var j = 0; j < 9; j++) {
ground[i][j] = (Math.random() * 5 | 0) + 6;
}
}
This way, I can fill the array without having to individually apply the function to each position. I suspect I might be missing a return statement or something similar, but I'm not entirely sure.