Consider the following:
Let's say below is the multi-dimensional, and you are given the starting point of [2, 2].
const WORLD = [
['P', 'P', 'P', 'C', 'P'],
['P', 'M', 'P', 'C', 'P'],
['P', 'M', 'P', 'C', 'P'], <---- That would result the "P" at the 2nd index
['P', 'M', 'P', 'P', 'P'],
['P', 'M', 'P', 'P', 'P']
];
Each of those values represent a terrain type described separately:
const TERRAIN_TYPES = {
'P': {
obstacle: false,
description: 'plains'
},
'M': {
obstacle: true,
description: 'mountains'
},
'C': {
obstacle: true,
description: 'crevasse'
}
};
My question is this... At first blush when I see a multi dimensional array, I imagine some kind of dynamic algorithm one should be using to traverse through it. But the fact a starting point, and a legend regarding the terrain types is given is the following implementation brute force?
I have a function excerpt from a class which accepts some commands. Also I included the commands and directions this function can use:
const DIRECTIONS = ['N', 'S', 'E', 'W'];
const COMMANDS = ['L', 'R', 'F', 'B'];
executeCommads(commands) {
const LEFTCOMMAND = COMMANDS[0];
const RIGHTCOMMAND = COMMANDS[1];
const FORWARDCOMMAND = COMMANDS[2];
const BACKWARDCOMMAND = COMMANDS[COMMANDS.length - 1]
const loc = this.location
const moveForward = this.location[1] - 1;
const moveBackWard = this.location[1] + 1;
const positionPossible = !this.positionPossible(WORLD[loc[0]][loc[1]])
for (var i = 0; i < commands.length; i++) {
if (commands[i] === LEFTCOMMAND) {
this.direction = 'W'
}
if (commands[i] === RIGHTCOMMAND) {
this.direction = 'E'
}
if (commands[i] === FORWARDCOMMAND) {
if (positionPossible) {
this.location[1] = moveForward
break
}
}
if (commands[i] === BACKWARDCOMMAND) {
if (positionPossible) {
this.location[1] = moveBackWard
break
}
}
}
return {
status: STATUS_CODES[0],
loc: this.location,
dir: this.direction
}
}
The part in question is this:
const moveForward = this.location[1] - 1;
const moveBackWard = this.location[1] + 1;
const positionPossible = !this.positionPossible(WORLD[loc[0]][loc[1]])
I would think that makes a lot of sense, that function:
positionPossible(value) {
return TERRAIN_TYPES[value].obstacle
}
checks the WORLD[2][1] if that position/terrain is a plain type, and then executes the COMMAND value passed i.e. F from the possible ['L', 'R', 'F', 'B']
For 'F' it will be const moveForward = this.location[1] - 1;
Thus resulting in:
const WORLD = [
['P', 'P', 'P', 'C', 'P'],
['P', 'M', 'P', 'C', 'P'], <---- That would result the "P" at the 2nd index
['P', 'M', 'P', 'C', 'P'],
['P', 'M', 'P', 'P', 'P'],
['P', 'M', 'P', 'P', 'P']
];
Hopefully that all makes sense, just wondering if that is kosher? Thanks in advance!
executeCommands(). Imagine we already haveexecuteCommand(), then you could implementfor (var c of commands) this.executeCommand(c). Next, implement the imagined function. For that, you might imagineloc = positionAhead()andisObstacle(loc)functions, and so on. But use them as functions, don't prematurely assign their results to variables that might be outdated when you access them later. For example, yourpositionPossibledescribes the initial position.