Created
October 18, 2019 20:13
-
-
Save Colter-Hammer/6b9ff7b36cf4400e278b2dffdfd3951d to your computer and use it in GitHub Desktop.
A little bit of recursion practice using the game Tower of Hanoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function move(ring, start, end) { | |
console.log(`Move ring ${ring} from tower ${start} to tower ${end}`) | |
} | |
/* | |
* hanoi() | |
* @param - n = Number of rings in game | |
* @param - start = starting tower | |
* @param - end = ending tower | |
* @param - aux = in-between tower | |
*/ | |
var globalCount = 0 | |
function hanoi(n, start, end, aux) { | |
let count = 1; | |
globalCount++; | |
console.log(globalCount); | |
if (n === 1) { | |
move(n, start, end); | |
return count; | |
} | |
count += hanoi(n-1, start, aux, end); | |
move(n, start, end); | |
count += hanoi(n-1, aux, end, start); | |
return count; | |
} | |
console.log('Hanoi: ', hanoi(7, 'A', 'B', 'C')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment