Towers of Hanoi
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
(a general understanding of what the model is trying to show or explain)
HOW IT WORKS
(what rules the agents use to create the overall behavior of the model)
HOW TO USE IT
(how to use the model, including a description of each of the items in the Interface tab)
THINGS TO NOTICE
(suggested things for the user to notice while running the model)
THINGS TO TRY
(suggested things for the user to try to do (move sliders, switches, etc.) with the model)
EXTENDING THE MODEL
(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)
NETLOGO FEATURES
(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)
RELATED MODELS
(models in the NetLogo Models Library and elsewhere which are of related interest)
CREDITS AND REFERENCES
(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)
Comments and Questions
globals [ SETA SETB SETC DISKA DISKB DISKC delsize ] to setup reset-ticks set SETA [0 0 0 0 0 0 0 0 0 0] set SETB [0 0 0 0 0 0 0 0 0 0] set SETC [0 0 0 0 0 0 0 0 0 0] set DISKA Numdisks set DISKB 0 set DISKC 0 installdisk Numdisks -19 5 end to go ToH 1 2 3 Numdisks print (word "Total steps taken" ticks) end to installdisk [numbe xcord ycord] if(numbe > 0)[ makedisk numbe xcord ycord set SETA replace-item (DISKA - numbe) SETA numbe set ycord ycord - 1 set numbe numbe - 1 installdisk numbe xcord ycord ] end to makedisk [numb xcord ycord] if(numb > 0)[ ask patch (xcord + numb) (- ycord) [set pcolor blue] set numb numb - 1 makedisk numb xcord ycord ] end to deldisk[numb xcord ycord] if(numb > 0)[ ask patch (xcord + numb) (- ycord) [set pcolor black] set numb numb - 1 deldisk numb xcord ycord ] end to alterdisk [a c] if(a = 1)[ set delsize (item (DISKA - 1) SETA ) deldisk delsize -19 (5 - DISKA + 1) set DISKA DISKA - 1 set SETA replace-item (DISKA) SETA 0 ] if(a = 2)[ set delsize (item (DISKB - 1) SETB ) deldisk delsize -7 (5 - DISKB + 1) set DISKB DISKB - 1 set SETB replace-item (DISKB) SETB 0 ] if(a = 3)[ set delsize (item (DISKC - 1) SETC ) deldisk delsize 5 (5 - DISKC + 1) set DISKC DISKC - 1 set SETC replace-item (DISKC) SETC 0 ] if(c = 3)[ let addsize delsize makedisk addsize 5 (5 - DISKC) set SETC replace-item (DISKC) SETC addsize set DISKC DISKC + 1 ] if(c = 2)[ let addsize delsize makedisk addsize -7 (5 - DISKB) set SETB replace-item (DISKB) SETB addsize set DISKB DISKB + 1 ] if(c = 1)[ let addsize delsize makedisk addsize -19 (5 - DISKA) set SETA replace-item (DISKA) SETA addsize set DISKA DISKA + 1 ] end to ToH [a b c n] let num 0 ifelse(n = 1)[ alterdisk a c tick show SETA show SETB show SETC ] [ set num n - 1 ToH a c b num alterdisk a c tick ToH b a c num show SETA show SETB show SETC ] end
There is only one version of this model, created almost 11 years ago by Rishubh Gupta.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Towers of Hanoi.png | preview | Preview for 'Towers of Hanoi' | almost 11 years ago, by Rishubh Gupta | Download |
This model does not have any ancestors.
This model does not have any descendants.