Towers of Hanoi

Towers of Hanoi preview image

1 collaborator

Default-person Rishubh Gupta (Author)

Tags

computer science 

Tagged by Rishubh Gupta about 10 years ago

game 

Tagged by Rishubh Gupta about 10 years ago

mathematics 

Tagged by Rishubh Gupta about 10 years ago

model 

Tagged by Rishubh Gupta about 10 years ago

nature 

Tagged by Rishubh Gupta about 10 years ago

play 

Tagged by Rishubh Gupta about 10 years ago

towers game practical 

Tagged by Rishubh Gupta about 10 years ago

world 

Tagged by Rishubh Gupta about 10 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.1.0 • Viewed 544 times • Downloaded 40 times • Run 0 times
Download the 'Towers of Hanoi' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

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 about 10 years ago by Rishubh Gupta.

Attached files

File Type Description Last updated
Towers of Hanoi.png preview Preview for 'Towers of Hanoi' about 10 years ago, by Rishubh Gupta Download

This model does not have any ancestors.

This model does not have any descendants.