簡體   English   中英

火星到漫游者的 JavaScript。 我需要能夠將一系列命令告訴流動站並按順序執行它們。

[英]Mars to Rover on JavaScript. I need to be able to tell a series of commands to the rover and execute them in sequence.

我正在使用 JavaScript 開發 Mars to Rover 程序,並且我已經完成了迭代 1,該迭代允許我的漫游車在 2 維 (x,y) 上向所有方向移動。 現在是第 2 次迭代,我需要能夠將一系列命令告訴流動站並按順序執行它們。

請給我一些有關如何進行迭代 2 的見解。

這是我當前的代碼:

 // --------- Mars Rover Kata: Iteration #1 ------------ \\\\ // ------------------- User Experience using Console --------- \\\\ var promptSay = "Play with the Console!\\nW = UP \\nS = DOWN \\nD = RIGHT \\nA = LEFT"; var gridEdge = "You can\\'go there! \\n\\nPlease notice that you are playing with an imaginary grid and the farest you can go is 9 steps"; var wrongInput = "---WRONG INPUT!--- Please use a correct input ie : \\nW = UP \\nS = DOWN \\nD = RIGHT \\nA = LEFT"; // Object Definition: (Vars inside a Var , may have functions) \\\\ var AJRover = { position : [0, 0], invalidInput: function(notright) { // Notification for invalid Input alert(notright); this.move(prompt(wrongInput)); }, invalidKey: function(message) { // Notification if you reach grid's edge alert(message); this.move(prompt(promptSay)); }, move: function(moveRover) { //Directions switch(moveRover.toLowerCase()) { case 'w': this.goDirection("up"); break; case 's': this.goDirection("down"); break; case 'd': this.goDirection("right"); break; case 'a': this.goDirection('left'); break; default: this.invalidInput(wrongInput); } }, goDirection: function(direction) { //Directions Functions switch(direction) { case 'up': if (this.position[1] >= -9 && (this.position[1] + 1) <= 9) { this.position[1]++; break; } else { this.invalidKey(gridEdge); break; } case 'down': if (this.position[1] <= 9 && (this.position[1] -1 ) >= -9) { // this needs to go back and stop at -9 this.position[1]--; break; } else { this.invalidKey(gridEdge); break; } case 'right': if (this.position[0] >= -9 && (this.position[0] + 1) <= 9) { this.position[0]++; break; } else { this.invalidKey(gridEdge); break; } case 'left': if (this.position[0] <= 9 && (this.position[0] -1) >= -9) { this.position[0]--; break; } else { this.invalidKey(gridEdge); break; } } } }; // ---- object END ----- \\\\\\ // 1- This function calls the object move (this.move) // 2- Sends the alert to prompts the var promptSay // 3- Expects input to decide the output while (true) { //This code block allows user move the rover on mars by interacting with console var entry = prompt(promptSay); AJRover.move(entry); console.log('You are now at position: ', AJRover.position); }

在瀏覽環境中,這不能通過與控制台交互來完成。

您需要做的是利用事件偵聽器。 這是一個如何通過將它們綁定到文檔正文來完成的示例。

const W_KEY = 119;
const A_KEY = 97;
const S_KEY = 115;
const D_KEY = 100;

document.body.addEventListener("keypress", function(e) {
  let entry = null;

  switch(e.keyCode) {
    case W_KEY:
      entry = "w";
      break;
    case A_KEY:
      entry = "a";
      break;
    case S_KEY:
      entry = "s";
      break;
    case D_KEY:
      entry = "d";
      break;
  }

  if(entry) {
    console.log("Key " + entry + " was pressed!");

    AJRover.move(key);
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM