我正在建造河内塔游戏以适应反应。我有一个名为“disks”的状态属性,它是一个由 3 个长度为 N 的数组组成的数组(N 是磁盘的总数)。我还定义了一个状态属性“历史”,它应该包含磁盘阵列的历史,如下所示:
- 最初:历史= [磁盘(初始配置)]
- 1 次移动后: history = [磁盘(初始配置),磁盘(1 次移动后)]
- 2 次移动后: history = [disks(Initial config), disks(1 次移动后), disks(2 次移动后)] 等。
然而,在 M 移动之后,历史数组如下所示:
历史 = [磁盘(M 移动后),磁盘(M 移动后),...,磁盘(M 移动后)]。
我找不到我的错误。如果有人知道出了什么问题,将不胜感激。以下是相关代码:
constructor(props){
super(props);
let disks = [
[],
[],
[]
];
//Initially all disks in first tower
for(let i=0; i<props.numberOfDisks; i++){
disks[0].push(i);
}
this.state = {
disks : disks,
selected : null,
move: 0,
history: [disks]
};
}
handleClick(i){
const disks = this.state.disks.slice();
const history = this.state.history.slice();
let move = this.state.move;
let selected = this.state.selected;
//if user has not previously selected a tower or selects the same tower again
if(selected===null || i===selected){
selected = disks[i].length>0 && i!==selected ? i : null;
this.setState({
selected : selected
});
return;
}
//Check if move is legal
//index is at bottom is 0 and the largest disk has id 0
if(disks[i].length === 0 || disks[i][disks[i].length-1] < disks[selected][disks[selected].length-1]){
//perform move
disks[i].push(disks[selected].pop());
move++;
// I guess this is where it goes wrong, but I can't see why
this.setState({
history: history.concat([disks]),
disks: disks,
move: move
});
}
this.setState({
selected: null
});
console.log(this.state.history);
}
请注意,游戏正常运行,这意味着磁盘阵列正在正确更新等......这只是历史阵列的更新以某种方式出错。我尝试将 disks.slice() 放入 history.concat 中,因为在我看来历史以某种方式存储了对磁盘数组的引用,但这并没有帮助。