Created
January 16, 2021 16:51
-
-
Save nikonov91-dev/db9e0e07792b09457e241ab08ab4482a to your computer and use it in GitHub Desktop.
задача кодевам
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
необходимо чтобы на сходе давать команду и на выходе после расчета нужно показать позицию и ориентацию. | |
суть в том что есть газонокосилка ездящая налево\направо\прямо и передвигаться она может ездить туда-сюда без гранично | |
и вот | |
const _d = { | |
r: 'r', | |
l: 'l' | |
}; | |
class Mower { | |
directions = [ | |
{name: 'E', axis: 'x', direction: 1}, | |
{name: 'S', axis: 'y', direction: 1}, | |
{name: 'W', axis: 'x', direction: -1}, | |
{name: 'N', axis: 'y', direction: -1} | |
]; | |
commands = { | |
[_d.r]: {name: 'R', step: 1, botton: 0, top: 3, direction: 'clockwise'}, | |
[_d.l]: {name: 'L', step: -1, top: 0, bottom: 3, direction: 'counter-clockwise'}, | |
a: {name: 'A', direction: 'advance'} | |
} | |
position = { | |
x: 0, | |
y: 0, | |
orientation: this.directions[0].name | |
} | |
_get_orientation_index = () => | |
this.directions.findIndex( e => e === this.position.orientation ); | |
_turn_around = (direction) => { | |
let dir_index; | |
this._get_orientation_index === this.commands[direction].top | |
? dir_index = this.commands[direction].bottom + this.commands[direction].step | |
: dir_index = this.directions[this._get_orientation_index + this.commands[direction].step]; | |
this.position.orientation = this.directions[dir_index].name; | |
} | |
_advance = () => { | |
const orientation = this.position.orientation; | |
const i = this.directions.findIndex(e => e.name === orientation); | |
this.position[i.axis] = this.position[i.axis] + this.position[i.direction]; | |
} | |
_perform_move = (move) => { | |
switch (true) { | |
case move === this.commands.r.name: { | |
this._turn_around(_d.r); | |
break; | |
} | |
case move === this.commands.l.name: { | |
this._turn_around(_d.l); | |
break; | |
} | |
case move === this.commands.a.name: { | |
this._advance(); | |
break; | |
} | |
} | |
} | |
run = (passed_commands) => { | |
Array.from(passed_commands).map(e => this._perform_move(e)); | |
this.inform_user(); | |
} | |
inform_user = () => `The mower is at ${this.position.x}, ${this.position.y} and oriented on ${this.position.orientation}` | |
} | |
const mower = new Mower(); | |
mower.run(''); | |
mower.run('A'); | |
mower.run('L'); | |
mower.run('AAAR'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment