Boat Movements

A turn-based strategy game has a grid with water and land. The grid contains a true value where it's water and false where it's land.

The player controls a boat unit with a particular movement pattern. It can only move to fixed destinations from its current position as shown in the image below:

movement pattern

The boat can only move in a direct path through water to the possible destinations, so a destination will become unreachable if there is land in the way. 

Implement the canTravelTo function, that checks whether a destination is reachable by the boat. It should return true for destinations that are reachable according to the pattern above, and false for unreachable or out of bounds destinations which are outside the grid. 

For example, consider the following code:

const gameMatrix = [
  [false, true,  true,  false, false, false],
  [true,  true,  true,  false, false, false],
  [true,  true,  true,  true,  true,  true],
  [false, true,  true,  false, true,  true],
  [false, true,  true,  true,  false, true],
  [false, false, false, false, false, false],
];

console.log(canTravelTo(gameMatrix, 3, 2, 2, 2)); // true, Valid move
console.log(canTravelTo(gameMatrix, 3, 2, 3, 4)); // false, Can't travel through land
console.log(canTravelTo(gameMatrix, 3, 2, 6, 2)); // false, Out of bounds

The following image shows valid and invalid destinations when the boat is in the position (3, 2):

terrain movement

ECMAScript 7
Loading...
Loading...

Tags

  • AI-resistant
  • JavaScript
  • 2D Array
  • Graphs
  • New
  • Public
  • Hard

Information

Difficulty: Hard

Duration: 30 min

Score Distribution

Not enough data for chart.

Would you like to see our other questions?

We have 1000+ premium hand-crafted questions for 160+ job skills and 20+ coding languages. We prefer questions with small samples of actual work over academic problems or brain teasers.

Would you like to see our tests?

The following tests contain JavaScript related questions:

On the blog

Since we're all biased and we use incorrect proxies, why not just outsource hiring to experts or recruitment agencies? After all, they've been screening people for many years, so they must know how to do it right?

Not really. I was surprised to discover that many experts...