0

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!

2
  • 1
    Could you clarify what your specific question is? If you have already-working code and want feedback on code style etc, then the Code Review site might be more helpful. (However, I'm fairly sure that your code is not yet working as you intend because you're not actually using the direction.)
    – amon
    Commented Apr 3, 2022 at 22:25
  • 1
    One strategy I can recommend for thinking through your problem clearly is “programming by wishful thinking”: So you want to executeCommands(). Imagine we already have executeCommand(), then you could implement for (var c of commands) this.executeCommand(c). Next, implement the imagined function. For that, you might imagine loc = positionAhead() and isObstacle(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, your positionPossible describes the initial position.
    – amon
    Commented Apr 3, 2022 at 22:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.