JavascriptをJavascriptを:文字列を使用して、ロボットの更新方向が(右に移動する)、R(左に移動する)キーLを使用してロボットを動かす簡単なゲームで動作するようにしようとし
が動作していない、F(移動します5x5の次元を使用して作成され、常に北に面しているボード上の特定の位置から(前方に)移動します(Nとして表示)。上記の文字列/文字のいずれかを入力した後に移動ボタンをクリックすると、現在の位置が表示されます。
期待値:たとえば、ボードの寸法(5x5)、現在の位置(3,3)、「R」と入力してMoveボタンを押すと、(3,3)Eとして結果の位置が表示されます。ロボットは最初に北(N)に面していて、今度は東(E)になるRIGHTに移動するように求められました。
問題:私のコードで、方向が更新されない理由を見つけることができないようです。
ここでは、すべての計算と更新を行うコードを示します。
var RobotManager = {
roomType: 'square',
roomParameters: [5, 5],
robotDirection: 'N',
robotPosition: [1, 2],
possibleDirections: ["N", "E", "S", "W"],
errorMessageNumber: -1,
errorMessage: [
"error0",
"error1",
"all other errors removed to keep code clean"
],
squares: [],
stringCommandThatExexuted: '',
init: function() {
var regexp_number = /^[1-9]$|([1][0-9])$/;
return true;
},
// This should move the robot to the right direction
turnRight: function() {
var movePosition = this.possibleDirections.indexOf(this.robotDirection);
if (movePosition == this.possibleDirections.length - 1) {
return this.possibleDirections[0];
}
return this.possibleDirections[movePosition + 1];
},
turnLeft: function() {
var movePosition = this.possibleDirections.indexOf(this.robotDirection);
if (movePosition == 0) {
return this.possibleDirections[this.possibleDirections.length - 1];
}
return this.possibleDirections[movePosition - 1];
},
moveForward: function() {
var nextPosition = this.getNextPosition();
var nextSquare = {
X: nextPosition[0],
Y: nextPosition[1]
};
if (this.isSquareAvailable(nextSquare)) {
this.robotPosition = nextPosition;
} else {
this.errorMessageNumber = 1;
return false;
}
return true;
},
//this is not getting executed to update the direction
getNextPosition: function() {
var x, y;
switch (this.robotDirection) {
case "N":
x = this.robotPosition[0];
y = this.robotPosition[1] - 1;
break;
case "E":
x = this.robotPosition[0] + 1;
y = this.robotPosition[1];
break;
case "W":
x = this.robotPosition[0] - 1;
y = this.robotPosition[1];
break;
case "S":
y = this.robotPosition[1] + 1;
x = this.robotPosition[0];
break;
}
return [x, y];
},
//First button clicks comes here and just renders default value of direction
getRobotsPositionAndDirection: function() {
if (this.errorMessageNumber <= 1) {
var message = "";
if (this.errorMessageNumber == 0) {
return this.errorMessage[0];
}
if (this.errorMessageNumber == 1) {
message = this.errorMessage[1];
}
return message + this.robotPosition[0] + " " + this.robotPosition[1] + " " + this.robotDirection;
}
return this.errorMessage[8];
},
checkCommandString: function(string) {
var english_command = /^[LRF]*$/;
if (english_command.test(string)) {
return true;
}
this.errorMessageNumber = 0;
return false;
},
getCommandStringThatExecuted: function() {
return this.stringCommandThatExexuted;
},
//This is where index is passed as 0 and doesn't execute
moveRobotToOnePosition: function(letter) {
switch (letter) {
case 'L':
this.robotDirection = this.turnLeft();
this.stringCommandThatExexuted += 'L';
break;
case 'R':
this.robotDirection = this.turnRight();
this.stringCommandThatExexuted += 'R';
break;
case 'F':
if (this.moveForward()) {
this.stringCommandThatExexuted += 'F';
return true;
} else {
return false;
}
break;
}
},
moveRobot: function(string) {
string = string.toUpperCase();
if (this.checkCommandString(string)) {
var array = string.split('');
for (var i = 0; i < array.length; i++) {
return this.moveRobotToOnePosition(i);
}
return true;
}
this.errorMessageNumber = 0;
}
}
問題は少ないコードを提供することができませんでした、検出が困難であるので、 '<>'スニペットエディタ – mplungjan
@mplungjanを使用して、[MCVE]作成してみてください。 – shaz
コード内のいくつかのコメントを使って、方向を変更する必要があることを示します。 –