【问题标题】:Trying to replace random array characters within an array尝试替换数组中的随机数组字符
【发布时间】:2021-06-14 03:52:05
【问题描述】:

这是我的第一个问题,如果我对此不满意,请给我反馈。谢谢

我正在尝试从付费课程创建 JavaScript 终端游戏。游戏名为“寻找我的帽子”。

问题的一部分需要我创建一个名为 generateField() 的静态方法。该方法生成一个字段,字段上有4个可能的字符:

hat = '^';

hole = 'O';

fieldCharacter = '░';

pathCharacter = '*';

fieldCharacter 是游戏区域的背景。游戏区域由嵌套数组组成。我在创建字段时没有遇到问题,但我也想用孔替换随机字段字符。我尝试使用 Math.random 和嵌套循环来迭代嵌套数组。我一直无法弄清楚如何让它发挥作用。

以下是代码,如有遗漏,请见谅,我会尽量回复大家。

    class Field {
    constructor(field) {
        this._field = field;
    }

    print() {
        this._field = field.join('\n');
        console.log(this._field.replace(/,/g, ''))
    }

    static generateField(height, width) {
        let finalArray = []
        for (let i = 0; i < height; i++) {
            finalArray.push([fieldCharacter.repeat(width)]);
        }
        for (let i = 0; i < finalArray.length; i++) {
            let randomHole = () => {
                let result = Math.random() * width;
                return result;
                }
            for (let j = 0; j < finalArray[i].length; j++) {
                if (randomHole() > width - 2) {
                    finalArray[i][j] = hole;
                }
            }
        }
        return finalArray.join('\n').replace(/,/g, '');
    }
}

console.log(Field.generateField(5, 10));

一个随机输出:

O
O
░░░░░░░░░░
░░░░░░░░░░
O

良好输出示例:

░░░░O░░░░░
░░░O░OO░░░
░░O░░░O░░░
░░░░O░░░░░
OO░░░░O░░O

【问题讨论】:

  • 你能举个例子来获得好的结果吗?
  • 我的错。谢谢回复。 ░░░░O░░░░░░░░O░OO░░░░░O░░░O░░░░░░░O░░░░░░OO░░░░O░跨度>

标签: javascript arrays


【解决方案1】:

我对您的代码进行了一些更改,并使用 stackoverflow sn-p 向您展示了它是如何工作的

String.prototype.replaceAt = function(index, replacement) {
    return this.substr(0, index) + replacement + this.substr(index + replacement.length);
}

hat = '^';

hole = 'O';

fieldCharacter = '░';

pathCharacter = '*';

class Field {
    constructor(field) {
        this._field = field;
    }

    print() {
        this._field = field.join('\n');
        console.log(this._field.replace(/,/g, ''))
    }

    static generateField(height, width) {
        let finalArray = []
        for (let i = 0; i < height; i++) {
            finalArray.push(fieldCharacter.repeat(width));
        }
        //i dont know why you did this loop but i changed the inside for marking random holes
        for (let i = 0; i < finalArray.length; i++) {
            //pick random x and y
            let randomHoleX = Math.floor(Math.random() * width);
            let randomHoleY = Math.floor(Math.random() * height);
            //becouse you save array of strings you need to find the correct array and replace the char at the correct index.
            finalArray[randomHoleY] = finalArray[randomHoleY].replaceAt(randomHoleX, hole);
        }
        return finalArray.join('\n').replace(/,/g, '');
    }
}

console.log(Field.generateField(5, 10));

【讨论】:

  • 就是这样!非常感谢,我已经为此工作了3天。您对我将来如何改进我的问题有任何反馈吗?我知道这里非常讨厌不好的问题:)
猜你喜欢
  • 2017-05-20
  • 1970-01-01
  • 2018-02-07
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
相关资源
最近更新 更多