我正在研究由一个对象/类表示的火星漫游者的模拟(链接到示例)。你应该能够根据火星的地形给它输入方向,并以JavaScript物体的形式得到一个输出,这将更新你的漫游者是如何在地形中前进的。
我已经算出了其他的条件,但我的问题是,当漫游者撞上一座山或裂缝时,我的问题是要想出一个逻辑。
这些是与基于地形的漫游对象/类一起使用的数据类型。
const TERRAIN_TYPES = {
'P': {
obstacle: false,
description: 'plains'
},
'M': {
obstacle: true,
description: 'mountains'
},
'C': {
obstacle: true,
description: 'crevasse'
}
};
const STATUS_CODES = ['OK', 'OBSTACLE', 'INVALID_COMMAND'];
// top left corner is (X:0, Y:0)
// bottom right is (X:4, Y:4)
const WORLD = [
['P', 'P', 'P', 'C', 'P'],
['P', 'M', 'P', 'C', 'P'],
['P', 'M', 'P', 'C', 'P'],
['P', 'M', 'P', 'P', 'P'],
['P', 'M', 'P', 'P', 'P']
];
const DIRECTIONS = ['N', 'S', 'E', 'W'];
const COMMANDS = ['L', 'R', 'F', 'B'];这是我创建的对象/类enter code here。
class Rover {
constructor(location, direction) {
this.location = location
this.direction = direction
this.commands = []
}
positionPossible(value) {
return TERRAIN_TYPES[value].obstacle
}
command(commands) {
this.commands = [...this.commands, ...commands];
if (COMMANDS.indexOf(this.commands[0]) == -1) {
return {
status: STATUS_CODES[2],
loc: this.location,
dir: this.direction
}
}
return this.roverController()
}
roverController() {
const TURN_LEFT_CMD = COMMANDS[0];
const TURN_RIGHT_CMD = COMMANDS[1];
const MOVE_FORWARD_CMD = COMMANDS[2];
const MOVE_BACKWARD_CMD = COMMANDS[COMMANDS.length - 1]
const commands = this.commands
let res = {
status: null,
loc: this.location,
dir: this.direction
}
if (DIRECTIONS.indexOf(this.direction) !== -1) {
for (let element of commands) {
if (element === TURN_LEFT_CMD) {
this.direction = DIRECTIONS[DIRECTIONS.length - 1];
res = {
...res,
...{
status: STATUS_CODES[0],
loc: this.location,
dir: this.direction
}
}
}
if (element === TURN_RIGHT_CMD) {
this.direction = DIRECTIONS[DIRECTIONS.length - 2];
res = {
...res,
...{
status: STATUS_CODES[0],
loc: this.location,
dir: this.direction
}
}
}
if (element === MOVE_FORWARD_CMD) {
if (this.positionPossible(WORLD[this.location[0]][this.location[1]]) === false) {
this.location[1] = this.location[1] = this.location[1] - 1;
res = {
...res,
...{
status: STATUS_CODES[0],
loc: this.location,
dir: this.direction
}
}
} else {
if (this.positionPossible(WORLD[this.location[0]][this.location[1]]) === true) {
this.direction = (commands[commands.indexOf(element - 1)] === 'L') ? 'E' : 'W'
this.location[1] = this.location[1] = this.location[1] + 1;
return res = {
...res,
...{
status: STATUS_CODES[1],
loc: this.location,
dir: this.direction
}
}
break
}
}
}
if (element === MOVE_BACKWARD_CMD && !this.positionPossible(WORLD[this.location[0]][this.location[1]])) {
this.location[1] = this.location[1] = this.location[1] + 1;
res = {
...res,
...{
status: STATUS_CODES[0],
loc: this.location,
}
}
}
}
return res
}
}
}您会注意到,我创建了一个函数roverController(),它包含一些特定数据条件的枚举,以及一个for-of循环,它正在遍历创建的命令并向后吐回一个响应对象,以获得反馈。
let res = {
status: null,
loc: this.location,
dir: this.direction
}当它命中条件时,我用不断变化的状态更新响应对象,
这就是月球车撞到无法通过的地形时应该给我的错误:
if (this.positionPossible(WORLD[this.location[0]][this.location[1]]) === true) {
this.direction = (commands[commands.indexOf(element - 1)] === 'L') ? 'E' : 'W'
this.location[1] = this.location[1] = this.location[1] + 1;
return res = {
...res,
...{
status: STATUS_CODES[1],
loc: this.location,
dir: this.direction
}
}
break
}
}我认为返回(或中断)语句会将一个函数踢出并返回status对象,但它没有;
任何帮助都将不胜感激!!
发布于 2022-04-05 21:53:19
break终止执行它的循环。请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
return后面跟着一个值退出一个函数,将值返回到最初调用该函数的语句。请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
就个人而言,为了提高可读性,我喜欢任何return语句都是函数中的最后一条语句。因此,一个好的结构是:
...
for (...) {
if (whatever) {
returnValue = ...
break; // loop exits regardless of conditional;
}
} // end for loop;
return returnValue; // returnValue set in loop before break;
} // end functionhttps://stackoverflow.com/questions/71758526
复制相似问题