Sha256: 4ba31e36dfa15299f09d80970df47218bb652a51a243fb67f295a885b3707a20

Contents?: true

Size: 1.74 KB

Versions: 131

Compression:

Stored size: 1.74 KB

Contents

export default class Robot {
  public coordinates: number[]
  public bearing: string

  constructor(xcoord: number = 0, ycoord: number = 0, direction: string = 'north') {
    this.coordinates = [xcoord, ycoord]
    this.bearing = direction
  }

  at(xcoord: number, ycoord: number) {
    this.coordinates = [xcoord, ycoord]
  }

  orient(direction: string) {
    this.bearing = direction
    return `The robot is pointed ${direction}`
  }

  advance() {
    if (this.bearing === 'north') {
      this.coordinates[1] += 1
    } else if (this.bearing === 'south') {
      this.coordinates[1] -= 1
    } else if (this.bearing === 'east') {
      this.coordinates[0] += 1
    } else if (this.bearing === 'west') {
      this.coordinates[0] -= 1
    }
  }

  turnLeft() {
    if (this.bearing === 'north') {
      this.orient('west')
    } else if (this.bearing === 'south') {
      this.orient('east')
    } else if (this.bearing === 'east') {
      this.orient('north')
    } else if (this.bearing === 'west') {
      this.orient('south')
    }
  }

  turnRight() {
    if (this.bearing === 'north') {
      this.orient('east')
    } else if (this.bearing === 'south') {
      this.orient('west')
    } else if (this.bearing === 'east') {
      this.orient('south')
    } else if (this.bearing === 'west') {
      this.orient('north')
    }
  }

  instructions(s: string) {
    return [...s].map((character) => {
      if (character === 'L') {
        return 'turnLeft'
      } else if (character === 'R') {
        return 'turnRight'
      } else if (character === 'A') {
        return 'advance'
      } else {
        throw 'Invalid Instruction'
      }
    })
  }

  evaluate(s: string) {
    this.instructions(s).forEach((instruction) => {
      this[instruction]()
    })
  }
}

Version data entries

131 entries across 131 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts