Skip to content

Commit f93614e

Browse files
Merge pull request #17 from Bhairavi-28/main
Fix Tower of Hanoi reset and solve button state
2 parents cad8c9d + e5b9b83 commit f93614e

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

web-app/js/projects.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,7 @@ function initTowerOfHanoi() {
32063206
let diskCount = 3;
32073207
let moveCount = 0;
32083208
let isAnimating = false;
3209+
let shouldStop = false;
32093210

32103211
const towerX = [200, 400, 600];
32113212
const baseY = 350;
@@ -3217,6 +3218,10 @@ function initTowerOfHanoi() {
32173218
towers = [[], [], []];
32183219
moveCount = 0;
32193220
diskCount = parseInt(diskCountInput.value) || 3;
3221+
3222+
//Reset animation state
3223+
isAnimating = false;
3224+
solveBtn.disabled = false;
32203225

32213226
for (let i = diskCount; i >= 1; i--) {
32223227
towers[0].push(i);
@@ -3270,6 +3275,8 @@ function initTowerOfHanoi() {
32703275
}
32713276

32723277
async function moveDisk(from, to) {
3278+
if(shouldStop) return;
3279+
32733280
const disk = towers[from].pop();
32743281
towers[to].push(disk);
32753282
moveCount++;
@@ -3302,13 +3309,18 @@ function initTowerOfHanoi() {
33023309
solveBtn.disabled = true;
33033310

33043311
await solveHanoi(diskCount, 0, 2, 1);
3312+
3313+
shouldStop = false;
33053314

33063315
isAnimating = false;
33073316
solveBtn.disabled = false;
33083317
}
33093318

33103319
solveBtn.addEventListener('click', solve);
3311-
resetBtn.addEventListener('click', initTowers);
3320+
resetBtn.addEventListener('click', () => {
3321+
shouldStop = true;
3322+
initTowers();
3323+
});
33123324
diskCountInput.addEventListener('change', initTowers);
33133325

33143326
initTowers();

0 commit comments

Comments
 (0)