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.78 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.77 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.76 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.75 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.74 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.73 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.72 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.71 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.70 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.69 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.68 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.67 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.66 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.65 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.64 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.63 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.62 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.61 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.60 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts
trackler-2.2.1.59 tracks/typescript/exercises/robot-simulator/robot-simulator.example.ts