app/grid.js

const { ERRORS } = require('../utils')

/**
 * Create a grid through which robots can move
 */
class Grid {
  /**
   * @param length {Number} - The length of the grid
   * @param height {Number} - The height of the grid
   * @throws {Error} Will throw an error if length or height are greater than 50
   * @throws {Error} Will throw an error if length or height are smaller than 0
   */
  constructor (length = 0, height = 0) {
    // Max grid size 50x50
    if (length > 50 || height > 50) {
      throw new Error(ERRORS.GRID_NO_GREATER_50_50)
    }

    // Min grid size 1x1
    else if (length < 0 || height < 0) {
      throw new Error(ERRORS.GRID_NO_SMALLER_1_1)
    }

    this.length = length
    this.height = height
    this.forbiddenPositions = []
  }

  /**
   * Adds a position to the array of forbidden's positions
   * @param position {String}
   */
  addForbiddenPosition (position) {
    this.forbiddenPositions.push(position)
  }

  /**
   * Check if a position is forbidden
   * @param position {String}
   * @returns {boolean}
   */
  hasForbidden (position) {
    return this.forbiddenPositions.includes(position)
  }
}

module.exports = Grid