gpt4 book ai didi

javascript - 火星探测器卡塔。可以转弯也可以前进

转载 作者:行者123 更新时间:2023-11-28 16:57:21 25 4
gpt4 key购买 nike

我正在做这个 kata,其中我有一个漫游器,我需要能够向 10x10 板内发出指令。 Rover 一次只能进行 1 个 Action 。要么转身,要么前进。

当我console.log提前功能时,我没有得到漫游器在板上的更新位置。我想不通。我写了所有内容并找到了解决方案,但后来我破坏了它并覆盖了某些内容,并且无法弄清楚我删除或更改了什么。

// Rover Object Goes Here
// ======================
let grid;
let rover = {
direction: "N",
location: {x:0,y:0},
// path: [{x:0,y:0}]
}

// ======================

//Turn left function
function turnLeft(rover){
switch (rover.direction) {
case "N":
rover.direction = "W";
break;
case "W":
rover.direction = "S";
break;
case "S":
rover.direction = "E";
break;
case "E":
rover.direction = "N";
break;
}
console.log(`turnLeft was called!. Rover is now facing ${rover.direction}` );
}


//Turn right function
function turnRight(rover){
switch (rover.direction) {
case "N":
rover.direction = "E";
break;
case "E":
rover.direction = "S";
break;
case "S":
rover.direction = "W";
break;
case "W":
rover.direction = "N";
break;
}
console.log(`turnRight was called!. Rover is now facing ${rover.direction}` );
}


// Function for moving rover
function moveForward(theRover) {
//Variables
let locationX = theRover.location.x;
let locationY = theRover.location.y;
let roverDir = theRover.direction;
let roverInfoConsole = `Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `;
// Restricted Moves
if ( (roverDir === "N" && locationY <= 0) ||
(roverDir === "E" && locationX >= 9) ||
(roverDir === "S" && locationY >= 9) ||
(roverDir === "W" && locationX <= 0) ) {

console.log(`Cannot move in that direction. The rover would move to a restricted area.
${roverInfoConsole}`);

//allowed moves
//North
} else if (roverDir === "N" && locationY <= 9) {
locationY = locationY - 1;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
//East
} else if (roverDir === "E" && locationX < 9) {
locationX = locationX + 1;
console.log(`Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
//South
} else if (roverDir === "S" && locationY < 9) {
locationY = locationY + 1;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
//West
} else if (roverDir === "W" && locationX < 9) {
locationX = locationX - 1;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
}
};

console.log(turnRight(rover));
console.log(moveForward(rover));
console.log(moveForward(rover));
console.log(turnRight(rover))
console.log(moveForward(rover));
console.log(moveForward(rover));

我预计最终的输出是:


"Current rover position is x:2 y:2
Current rover direction is S "

但它没有更新位置。这是我实际得到的整个控制台日志:

"turnRight was called!. Rover is now facing E"
undefined
"Current rover position is x:1 y:0
Current rover direction is E "
undefined
"Current rover position is x:1 y:0
Current rover direction is E "
undefined
"turnRight was called!. Rover is now facing S"
undefined
"moveForward was called.
Current rover position is x:0 y:1
Current rover direction is S "
undefined
"moveForward was called.
Current rover position is x:0 y:1
Current rover direction is S "
undefined

最佳答案

我认为您必须递增/递减 theRover.location.xtheRover.location.y 而不是您正在使用的临时变量。

关于javascript - 火星探测器卡塔。可以转弯也可以前进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58758656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com