Difference between revisions of "CSC103 Towers of Hanoi in Javascript"
(Created page with "--~~~~ ---- <br /> <br /> <center>Image:AnimatedTowersOfHanoi.gif</center> <br /> <br /> __TOC__ =References= * [http://en.wikipedia.org/wiki/Tower_of_Hanoi Towers of Hanoi]...") |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
<br /> | <br /> | ||
__TOC__ | __TOC__ | ||
− | |||
− | + | ||
− | |||
=Code= | =Code= | ||
Line 16: | Line 14: | ||
<html><head><title>CSC103 Javascript Program</title></head> | <html><head><title>CSC103 Javascript Program</title></head> | ||
<body> | <body> | ||
− | <h1> | + | <h1>Towers of Hanoi</h1> |
<script language="JavaScript" type="text/javascript"><!-- | <script language="JavaScript" type="text/javascript"><!-- | ||
− | |||
//Enter your code below this line: | //Enter your code below this line: | ||
Line 57: | Line 54: | ||
<br /> | <br /> | ||
+ | =Running the Program= | ||
+ | |||
+ | * Go to this site: http://htmledit.squarefree.com/ | ||
+ | * Paste the whole program above in the top window of [http://htmledit.squarefree.com/ squarefree.com] | ||
+ | * You should get all the moves printed in the lower window. | ||
+ | |||
+ | =References= | ||
+ | |||
+ | * [http://en.wikipedia.org/wiki/Tower_of_Hanoi Towers of Hanoi] on Wikipedia | ||
+ | * [http://mathworld.wolfram.com/TowerofHanoi.html Towers on Hanoi] on MathWorld.com | ||
<br /> | <br /> |
Latest revision as of 08:09, 28 February 2011
--D. Thiebaut 07:35, 28 February 2011 (EST)
Contents
Code
<html><head><title>CSC103 Javascript Program</title></head>
<body>
<h1>Towers of Hanoi</h1>
<script language="JavaScript" type="text/javascript"><!--
//Enter your code below this line:
// This program illustrates how a very short program could take
// an extremely long time to run. The program prints out all the moves
// that are required to move N disks from one peg to another peg in the
// game of the Towers of Hanoi (http://en.wikipedia.org/wiki/Tower_of_Hanoi)
// The function moveNDisks is given a number in the variable N, which is the
// number of disks to move. It is also given the names of the 3 pegs it must
// use. The goal of the function is to move the N disks from the "fromPeg"
// and put them on the "toPeg" using the "extraPeg" as a temporary peg.
//
function moveNDisks( N, fromPeg, toPeg, extraPeg ) {
// if there's only one disk to move, then just print the move required
if ( N==1 )
document.write( "Move disk from Peg " + fromPeg + " to " + toPeg + "<br>" );
else {
// otherwise, then call the function to move N-1 disks out of the way...
moveNDisks( N-1, fromPeg, extraPeg, toPeg );
// then move 1 disk to the destination
moveNDisks( 1, fromPeg, toPeg, extraPeg );
// and the N-1 that were out of the way should be brought back
moveNDisks( N-1, extraPeg, toPeg, fromPeg );
}
}
// now, let's try a test case with 5 disks
moveNDisks( 5, "A", "B", "C" );
//Do not write below this line.
//-->
</script>
</body></html>
Running the Program
- Go to this site: http://htmledit.squarefree.com/
- Paste the whole program above in the top window of squarefree.com
- You should get all the moves printed in the lower window.
References
- Towers of Hanoi on Wikipedia
- Towers on Hanoi on MathWorld.com