2016-11-23 3 views
0

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; 
    } 
} 
+1

問題は少ないコードを提供することができませんでした、検出が困難であるので、 '<>'スニペットエディタ – mplungjan

+0

@mplungjanを使用して、[MCVE]作成してみてください。 – shaz

+1

コード内のいくつかのコメントを使って、方向を変更する必要があることを示します。 –

答えて

2

だからあなたの問題は、実際にはかなり単純であり、これを理解するためにそう長く取るための私の謝罪。問題は、配列の要素ではなく、配列のインデックスを渡していたことです。

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); 
     this.moveRobotToOnePosition(array[i]); // don't use return here unless you want to exit the function. 
     } 
     return true; 
    } 
    this.errorMessageNumber = 0; 
    } 
+0

にスポットがあります。配列要素は欠けていたものです。ありがとう。 – shaz

関連する問題