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;
}}
class Grid {
constructor (W, H){
this.width = W;
this.height = H;
this.space = Array.from(new Array(this.height*this.width));
}
get (vector) {return this.space[vector.x + this.width * vector.y];}
set (vector, value){this.space[vector.x + this.width * vector.y] = value;}
map (f) {return this.space.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);
}
set (){
map.forEach( (line,y) => {
line.split("").forEach( (char,x) => {
this.grid.set(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))
);
}
}
myWorld = new World();
myWorld.set();
console.log(myWorld.toString());