You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

二维数组游戏打印异常及移动逻辑问题排查求助

问题分析与修复方案

1. TypeError: Cannot read properties of undefined (reading 'forEach')

这个错误的核心触发原因有两个:

  • 方向判断逻辑错误:你在instructionsToMove中已经把输入转成了小写,但后续判断时用input.toLowerCase === 'u'——这里toLowerCase是字符串的方法,直接比较函数本身永远不等于字符串'u',导致所有有效输入都会进入else分支直接结束游戏。如果用户点击prompt的取消按钮,promptInputUser会返回null,调用null.toLowerCase()会直接抛出错误,中断程序,后续print调用时this._fields可能因上下文异常变为undefined。
  • 向右移动边界判断错误:this.y < this._fields[this.y].length - 1中,用列索引this.y去取行数据,当this.y大于等于数组行数时,this._fields[this.y]是undefined,访问length会抛出错误,直接中断程序执行。

2. 输入指令后无法看到二维数组新值

  • 方向判断逻辑错误导致所有有效输入都触发else分支,直接设置gameOver=true,循环提前结束,不会执行下一次print。
  • 移动逻辑中只将旧位置标记为路径字符,但未在新位置标记玩家字符,即使移动逻辑正常执行,数组也不会显示玩家的新位置。

3. 移动逻辑的其他问题

  • 未处理用户取消输入的情况(prompt返回null),会直接抛出错误。
  • 找到帽子或掉入陷阱时,未打印最终的游戏状态。

修复后的代码

print与playGame方法

print() {
    console.clear();
    this._fields.forEach(rows => {
        console.log(rows.join(''));
    });
}

playGame() {
    while (!this.gameOver) {      
        this.print();      
        const promptInputUser = prompt("移动指令说明:{u=上}, {d=下}, {l=左} & {r=右}\n请输入你的移动方向? ");
        this.instructionsToMove(promptInputUser);
    }
    // 游戏结束前打印最终状态
    this.print();
    console.log("游戏结束");
}

instructionsToMove方法

instructionsToMove(promptInputUser) {
    // 处理用户取消输入的情况
    if (!promptInputUser) {
        this.gameOver = true;
        console.log("输入取消,游戏结束");
        return;
    }
    
    const input = promptInputUser.toLowerCase();
    // 假设你的类中有_player属性存储玩家字符,比如'*'
    const playerChar = this._player;

    if (input === 'u') { // 向上移动
        if (this.x > 0) {
            this._fields[this.x][this.y] = this._pathCharacter;
            this.x -= 1;
            this._fields[this.x][this.y] = playerChar; // 标记新位置的玩家
        } else {
            this.gameOver = true;
            console.log("你无法继续向上移动,已触碰到边界。");
        }
    } else if (input === 'd') { // 向下移动
        if (this.x < this._fields.length - 1) {
            this._fields[this.x][this.y] = this._pathCharacter;
            this.x += 1;
            this._fields[this.x][this.y] = playerChar;
        } else {
            this.gameOver = true;
            console.log("你无法继续向下移动,已触碰到边界");
        }
    } else if (input === 'l') { // 向左移动
        if (this.y > 0) {
            this._fields[this.x][this.y] = this._pathCharacter;
            this.y -= 1;
            this._fields[this.x][this.y] = playerChar;
        } else {
            this.gameOver = true;
            console.log("你无法继续向左移动,已触碰到边界");
        }
    } else if (input === 'r') { // 向右移动
        // 修复边界判断:用当前行的长度判断列边界
        if (this.y < this._fields[this.x].length - 1) {
            this._fields[this.x][this.y] = this._pathCharacter;
            this.y += 1;
            this._fields[this.x][this.y] = playerChar;
        } else {
            this.gameOver = true;
            console.log("你无法继续向右移动,已触碰到边界");
        }
    } else {
        this.gameOver = true;
        console.log("错误...错误...错误...\n仅支持输入{u=上}, {d=下}, {l=左} & {r=右}。");
        return;
    }

    if (this._fields[this.x][this.y] === this._hat) {
        console.log("你找到帽子了!!!\n获胜!!!\n获胜!!!\n获胜!!!");
        this.gameOver = true;
    } else if (this._fields[this.x][this.y] === this._hole) {
        console.log("很遗憾,你输了!!!\n请重试!!!");
        this.gameOver = true;
    }
}

内容的提问来源于stack exchange,提问作者Jovanny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:59:51