var map = [ "HHHHHHHHHH",
"X ! X",
"X X",
"X ? X",
"X X",
"HHHHHHHHHH"
];
let legend = {"X":"1",
"!": "A",
"H":"-",
"?":"E"};
let legendInv = {"1":"|",
"A": "x",
"-" :"-",
"E" :"Y"};
class Vector {
constructor (x, y){
this.x = x;
this.y = y;
}}
//modif de la representation interne de la grille array of array et non array
class Grid {
constructor (W, H){
this.width = W;
this.height = H;
this.space = Array.from(new Array(this.height), () => new Array(this.width));
//console.log(this.space.length,this.space[0].length);
}
getE ( vector) {
return this.space[vector.y][vector.x];
}
setE ( vector, value){
this.space[vector.y][vector.x] = value;
}
map (f) {
//console.log(this.space.reduce( (p, n) => p.concat( n )));
return this.space.reduce( (p, n) => p.concat(n)).map(f);
}
// forEach(f) {return this.space.forEach(f);}
}
function nbFromChar(legend, ch) {
if (ch == " ") return " ";
var element = legend[ch];
return element;
}
function charFromNb(legend, ch) {
if (ch == " ") return " ";
var element = legend[ch];
return element;
}
class World {
constructor (){
this.grid = new Grid(map[0].length, map.length);
}
setE (){
map.forEach( (line,y) => {
line.split("").forEach( (char,x) => {
//ici autre modif new Vector(y,x) a la place de new Vector(x,y)
this.grid.setE(new Vector(x,y),nbFromChar(legend,char));
});
});
}
toString () {
return (
this.grid.map( (elt) => charFromNb(legendInv, elt))
.reduce((p,n,i) => (i%this.grid.width===0) ? (p+"\n"+n) : (p+n))
);
}
}
var myWorld = new World();
myWorld.setE();
console.log(myWorld.toString());