Tower of Hanoi ADA Code

Here is an ADA program for the Tower of Hanoi:


--Ada program for Towers of Hanoi
--Courtesy of Robert McCloskey, Ph.D.; University of Scranton with Ada.Text_IO ; with Saigon_Display ; procedure Hanoi is package tio renames Ada.Text_IO ; package iio is new tio.integer_io(integer) ; Num_Disks : positive ; --# of disks to be transferred Disp_Mode_Resp : integer ; --user's display mode response begin -- of Hanoi tio.Put("Enter # of disks to be transferred:"); iio.Get (Num_Disks) ; tio.Skip_Line ; tio.New_Line ; tio.Put_Line("You have a choice of display modes:"); tio.New_Line ; tio.Put_Line(" 1) fast") ; tio.Put_Line(" 2) slow") ; tio.Put_Line(" 3) interactive") ; tio.New_Line ;
tio.Put("Enter your choice:") ; iio.Get( Disp_Mode_Resp ) ; declare package SG is new Saigon_Display( Num_Pegs => 3, Num_Disks => Num_Disks ) ; procedure Towers_of_Hanoi ( From, To, Extra : in SG.Peg_ID_Type ; N : in natural ) is begin -- Towers_of_Hanoi case N is when 0 => null ; when 1 => SG.Move_Disk( From, To ) ; when others => Towers_of_Hanoi( From => From, --transfer N-1 smallest
To => Extra, --disks to "extra" peg Extra => To, N => N-1 ) ; SG.Move_Disk( From, To ) ; --move largest peg to destination Towers_of_Hanoi( From => Extra, --transfer the N-1 disks To => To, --on "extra" peg to the Extra => From, --destination peg N => N-1 ) ; end case ; end Towers_of_Hanoi ; begin --of declare block case Disp_Mode_Resp is when 1 => SG.Set_Display_Mode( SG.Fast ) ; when 2 => SG.Set_Display_Mode( SG.Slow ) ; when 3 => SG.Set_Display_Mode( SG.Interactive ) ; when others => tio.Put_Line("Invalid response; using FAST mode") ; SG.Set_Display_Mode( SG.Fast ) ; end case ; SG.Initialize ; --place disks into usual beginning configuration Towers_of_Hanoi( From => 1, Extra => 2, To => 3, N => Num_Disks ) ; end ; --of declare block end Hanoi ;

LinkExchange
LinkExchange Member