var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var t = Math.round(Math.random()*10/2);
var lakecount = 0;
var arr = [];
var ln = row = 0;
//create initial array
for(let i = 0; i < 10; i++) {
arr.push([]);
};
//create second part of 2d array
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < 10; j++) {
//check if there are more then five lakes.
if(lakecount > 5) {
//if there is, reset until the new tile generated isn't a lake.
while(t == 4) {
t = Math.round(Math.random()*10/2);
}
//create new tile
arr[i].push([t,0,0,0]);
t = Math.round(Math.random()*10/2);
}
else {
if(t == 4 && ln == 1) {
//check if the program generated a lake and there is one already in the row, regenerate tile until a lake hasn't been generated for the tile
while(t == 4) {
t = Math.round(Math.random()*10/2);
}
console.log("one per row");
}
//create tile
arr[i].push([t,0,0,0]);
t = Math.round(Math.random()*10/2);
console.log(arr[i][j]);
if(t == 4) {
//if there is a lake generated, add to the amount of overall lakes, then say there is a lake in the row.
lakecount++
ln++;
console.log("add");
}
if(row < 10) {
//add for each tile, until you reach end of row.
row++
console.log("row add");
}
else {
//reset row, reset if lakes are in that row.
row = 0;
ln = 0;
console.log("row reset");
}
}
}
};
//color everything
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < 10; j++) {
switch(arr[i][j][0]) {
case 0:
ctx.fillStyle = "lime"
ctx.fillRect(j*48,i*48,48,48);
break;
case 1:
ctx.fillStyle = "black"
ctx.fillRect(j*48,i*48,48,48);
break;
case 2:
ctx.fillStyle = "green"
ctx.fillRect(j*48,i*48,48,48);
break;
case 3:
ctx.fillStyle = "gray"
ctx.fillRect(j*48,i*48,48,48);
break;
case 4:
ctx.fillStyle = "blue"
ctx.fillRect(j*48,i*48,48,48);
break;
case 5:
ctx.fillStyle = "bisque"
ctx.fillRect(j*48,i*48,48,48);
break;
}
}
}
<canvas id="canvas" height="480" width="480"></canvas>