gpt4 book ai didi

javascript - 如何调整随机化级别javascript

转载 作者:数据小太阳 更新时间:2023-10-29 04:39:53 25 4
gpt4 key购买 nike

<分区>

我正在使用 javascript 开发 8 个益智游戏,我通过打乱拼图拼图数组来打乱拼图

enter image description here

var shuffleArray = (array) => {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};

我想让用户选择难度:简单、中等、困难、我该如何实现?

更新:

我会稍微解释一下我的实现。 (这是 typescript )

拼图数组是 PuzzleNode 类的数组

export class PuzzleNode {
goal: Node;
current: Node;
}

当我洗牌时,我不碰 puzzleArray,但我像这样洗牌 current 属性

shuffle() {
this.shuffledNodes = shuffleArray(this.shuffledNodes);
for (let i = 0; i < this.shuffledNodes.length; i++) {
this.puzzleNodes[i].current = this.shuffledNodes[i];
}
/** Keep shuffling until getting a solvable puzzle */
if (!this.isSolvable()) {
this.shuffle();
}
}

这样我可以使用索引到达任何节点,因为即使在洗牌后索引也不会改变,例如空白图 block 始终是 puzzleNodes[8]

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com