1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| clear();
var caracterWall = "#";
var caracterGrass = " ";
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
}
function isInMapSet(mapSet, value ){
for (var index in mapSet){
if(mapSet[index] instanceof Array){
if(mapSet[index].contains(value)){
return true;
}
}
}
return false;
}
function getIndexInMapSet(mapSet, value ){
for (var index in mapSet){
if(mapSet[index] instanceof Array){
if(mapSet[index].contains(value)){
return index;
}
}
}
return false;
}
function mergeIndexInMapSet(mapSet, indexA, indexB){
if(mapSet[indexA] instanceof Array && mapSet[indexB] instanceof Array ){
mapSet[indexA] = mapSet[indexA].concat(mapSet[indexB]);
delete mapSet[indexB];
return true;
}
return false;
}
function displayMap(map){
for (var x in map){
if(typeof map[x] !== "function"){
var line = "";
for (var y in map[x]){
if(typeof map[x][y] !== "function"){
var line = line+ map[x][y];
}
}
console.log(line);
}
}
}
var map= new Array();
var x = 10;
var y = 60;
var randomOrder = new Array();
var mapSet =new Array();
for(i=0;i<x*2;i++){
var line= new Array();
for(j=0;j<y*2;j++){
var matsetLine = new Array();
var entry = new Array();
if(i%2==0 && j%2==0){
matsetLine.push((i/2)+':'+(j/2));
mapSet.push(matsetLine);
randomOrder.push((i/2)+':'+(j/2));
line.push(caracterGrass);
} else {
line.push(caracterWall);
}
}
map.push(line);
}
randomOrder.sort(function() {return 0.5 - Math.random()});
for (var i in randomOrder)
{
if(typeof randomOrder[i] !== "function"){
var index = randomOrder[i].split(":");
var posX = index[0];
var posY = index[1];
var possible = new Array();
var currentIndex = posX+ ":"+posY;
var ouestIndex = (posX-1)+ ":"+posY;
if(isInMapSet(mapSet,ouestIndex) && getIndexInMapSet(mapSet, ouestIndex)!= getIndexInMapSet(mapSet, currentIndex)){
possible.push(ouestIndex);
}
var estIndex = (posX+1)+ ":"+posY;
if(isInMapSet(mapSet,estIndex) && getIndexInMapSet(mapSet, estIndex)!= getIndexInMapSet(mapSet, currentIndex)){
possible.push(estIndex);
}
var nordIndex = posX+ ":"+(posY-1);
if(isInMapSet(mapSet,nordIndex) && getIndexInMapSet(mapSet, nordIndex)!= getIndexInMapSet(mapSet, currentIndex)){
possible.push(nordIndex);
}
var sudIndex = posX+ ":"+(posY+1);
if(isInMapSet(mapSet,sudIndex) && getIndexInMapSet(mapSet, sudIndex)!= getIndexInMapSet(mapSet, currentIndex)){
possible.push(sudIndex);
}
if(possible.length>0){
var randomNumber=Math.floor(Math.random()*possible.length);
var posSlip = possible[randomNumber].split(':');
var posXRand = posSlip[0];
var posYRand = posSlip[1];
var wallX = parseInt(posX)+parseInt(posXRand);
var wallY = parseInt(posY)+parseInt(posYRand);
map[wallX][wallY] = caracterGrass;
mergeIndexInMapSet(mapSet, getIndexInMapSet(mapSet,currentIndex), getIndexInMapSet(mapSet, possible[randomNumber]));
}
}
}
displayMap(map); |
Partager