Javascript 学习 if for 循环的第二次迭代 - 学校作业

问题描述 投票:0回答:1

我很难解决游戏板问题。我能够迭代我的辅助函数一次,然后它就会停止。我很困惑为什么我的 while 循环不会获取当前的答案,添加到当前的解决方案并在板上画一个 X。我尝试过很多不同的答案。我需要 for 循环多次循环,并且板子需要在原来的位置用 X 进行更新。我还需要队列和访问过的数组来更新

这应该是通过 findpath 函数到达末端节点的最短第一路径函数。我在棋盘上的骑士棋子的所有可能方向上循环,直到到达最终位置或者队列中的空间或物品用完

let board = [[], [], [], [], [], [], [], []];

queue = [];
visited = [];

let counter = 0;

let xPossibleMoves = [2, 1, -1, -2, -2, -1, 1, 2];
let yPossibleMoves = [1, 2, 2, 1, -1, -2, -2, -1];

function findPath(currentX, currentY, endX, endY) {
  console.log(currentX, currentY);

  console.log(queue);
  console.log(visited);
  queue.forEach(function (item) {
    visited.push(item);
  });
  console.log(visited);

  for (let i = 0; i < 8; i++) {
    let vectorX = currentX + xPossibleMoves[i];
    let vectorY = currentY + yPossibleMoves[i];
    console.log("next hop X: " + vectorX + " Y: " + vectorY);

    if (board[vectorX] == undefined || board[vectorY] == undefined) continue;

    if (board[vectorX][vectorY] == "X") continue;

    if (board[vectorX][vectorY] == "END") return "DONE";

    queue.push(vectorX, vectorY);
    board[vectorX][vectorY] = "X";
  }
  //Stuff out of my for loop happens Once
  tail = 2;
  queue = queue.slice(tail);
  console.log(queue + " : queue");
  console.log(board);

  console.log(visited);
  queue.forEach(function (item) {
    visited.push(item);
  });
  console.log(visited);

 //causes a infinite loop
  //while (board[[endX][endY]] == null)
   //findPath(currentX, currentY, endX, endY);
}

function knightsMove(startX, startY, endX, endY) {
  if (board[endX][endY] == null) {
    let currentX = startX;
    let currentY = startY;

    queue.push(currentX);
    queue.push(currentY);
    board[endX][endY] = "END";
    board[currentX][currentY] = "START";

    console.log(board);

    findPath(currentX, currentY, endX, endY);
  }
}

knightsMove(0, 0, 3, 3);

我尝试添加 do while 循环、while 循环、制作原始实例的原型、大量不同的变体、修改任何会阻止循环的错误、添加一个函数来更新所有内容并推送 while 循环等等。

javascript function iteration
1个回答
0
投票

清理了一下

嗯...我不想做你的全部作业,但我在 Chrome 中保留了一些旧的片段。这至少应该有助于清理你的输出并使调试更容易。我也知道这种感觉——你最终得到的

console.log(...)
行比实际代码行还要多。

console.clear();
// This is just a variable to help differentiate the console output lines.
let dumpInfoCount = 0;

// Some console styles to make reading the output a little easier...
cs = {
  h1: "font-size:150%;",
  h2: "font-size:130%;",
  i: "font-style: italic;",
  b: "font-weight:bold;",
  attn: "color: #990000;",
  warn: "color: #ffcc00;",
  good: "color: #009900;",
  info: "color: #00ccff;",
  gray: "color: #ccc;",
  reset: "color: inherit;font-size:100%;",
};

let board = [[], [], [], [], [], [], [], []];
queue = [];
visited = [];
let counter = 0;
let xPossibleMoves = [2, 1, -1, -2, -2, -1, 1, 2];
let yPossibleMoves = [1, 2, 2, 1, -1, -2, -2, -1];

/**
 * Clean up code by removing all the `console.log(...)`
 * lines. Also makes it easier to turn it all off later.
 */ 
function dumpInfo(cx, cy, extraMessage) {
  console.groupCollapsed(
    "%cInfo Number:%c %d%c %s",
    cs.warn,
    cs.reset,
    dumpInfoCount,
    cs.info,
    extraMessage ? extraMessage : ""
  );
  console.log(
    "%cPassed In %cX%c,%cY:%c %d , %d",
    cs.info,
    cs.good,
    cs.reset,
    cs.good,
    cs.reset,
    cx,
    cy
  );
  console.log("%cBoard:%c %o", cs.info, cs.reset, board);
  console.log("%cQueue:%c %o", cs.info, cs.reset, queue);
  console.log("%cVisited:%c %o", cs.info, cs.reset, visited);
  console.groupEnd();
  dumpInfoCount += 1;
}

function findPath(currentX, currentY, endX, endY) {
  dumpInfo(currentX, currentY);

  // Not sure why the need to clone into `visited` here. Nothing happens with it.
  // queue.forEach(function (item) {
  // visited.push(item);
  // });
  // dumpInfo(currentX, currentY);

  console.groupCollapsed("%cInner Loop Here", cs.info);

  for (let i = 0; i < 8; i++) {
    let vectorX = currentX + xPossibleMoves[i];
    let vectorY = currentY + yPossibleMoves[i];

    // Just be mindful that we're passing a differnt X/Y pair, here...
    dumpInfo(vectorX, vectorY, "Inner Loop Here");

    if (board[vectorX] == undefined || board[vectorY] == undefined) continue;

    if (board[vectorX][vectorY] == "X") continue;

    if (board[vectorX][vectorY] == "END") return "DONE";

    queue.push(vectorX, vectorY);
    board[vectorX][vectorY] = "X";
  }
  console.groupEnd();
  //Stuff out of my for loop happens Once
  tail = 2;
  queue = queue.slice(tail);

  dumpInfo(currentX, currentY, "BEFORE Queue to Visited");

  queue.forEach(function (item) {
    visited.push(item);
  });
  dumpInfo(currentX, currentY, "AFTER Queue to Visited");

    queue.forEach((item) => {
        findPath(currentX, currentY, endX, endY);
    });
    //   while (queue.length > 0 && board[[endX][endY]] == null)
    //     return findPath(currentX, currentY, endX, endY);
    
    /**
     * Need to decide what the final return value is. It's not
     * clear how you're defining "success", here.
     */
    return { currentX, currentY, endX, endY };
}

function knightsMove(startX, startY, endX, endY) {
  if (board[endX][endY] == null) {
    // let currentX = startX;
    // let currentY = startY;

    queue.push(startX);
    queue.push(startY);
    board[endX][endY] = "END";
    board[startX][startY] = "START";

    dumpInfo(startX, startY);

    return findPath(startX, startY, endX, endY);
  }
}

knightsMove(0, 0, 3, 3);

奖金信息

我曾经使用过一个很酷的小型 Chrome 扩展程序,名为 jsShell,但那家伙不再支持它了。现在我使用一种叫做 Snippets 的东西。我知道它没有任何自动化功能(比如

run this script if the URL matches this regex
之类的东西),但它确实 可以将所有内容与我的 GitHub 帐户同步。这很好——我几乎可以在学校、家里、工作、虚拟机等地方通过浏览器访问它。

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.