home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / misc / vdt100.src < prev    next >
Encoding:
Text File  |  1988-05-03  |  14.8 KB  |  494 lines

  1. ::::::::::
  2. VDT100.PRO
  3. ::::::::::
  4.  
  5. -------- SIMTEL20 Ada Software Repository Prologue ------------
  6. --                                                           -*
  7. -- Unit name    : VDT100
  8. -- Version      : 1.0
  9. -- Author       : Triplett, Brenda and Hammons, Bud
  10. --              : Department of Computer Science
  11. --              : North Texas State University
  12. --              : Denton, TX  76203
  13. -- DDN Address  : HAMMONS%TI-EG at CSNET-RELAY
  14. -- Copyright    : (c) 1984, 1985 Brenda Triplett and Bud Hammons
  15. -- Date created :  1984
  16. -- Release date :  15 Jan 1985
  17. -- Last update  :  15 Jan 1985
  18. -- Machine/System Compiled/Run on : Telesoft Ada 1.5, VAX
  19. --                                                           -*
  20. ---------------------------------------------------------------
  21. --                                                           -*
  22. -- Keywords     :  VT100, TERMINAL INTERFACE, TOWERS OF HANOI
  23. ----------------:
  24. --
  25. -- Abstract     :  VDT100.SRC contains a package which provides a
  26. ----------------:  set of routines to interface with a VT100
  27. ----------------:  computer terminal, providing procedures for
  28. ----------------:  functions such as cursor positioning and clear
  29. ----------------:  screen.  Included is a test program, which is
  30. ----------------:  a solution to the Towers of Hanoi.
  31. ----------------:  This is a first student attempt and does not use
  32. ----------------:  the IMAGE attribute.
  33. --                                                           -*
  34. ------------------ Revision history ---------------------------
  35. --                                                           -*
  36. -- DATE         VERSION    AUTHOR                  HISTORY
  37. -- 15 Jan 85       1.0    Brenda Triplett        Initial Release
  38. --                                                           -*
  39. ------------------ Distribution and Copyright -----------------
  40. --                                                           -*
  41. -- This prologue must be included in all copies of this software.
  42. --
  43. -- This software is copyright by the author.
  44. --
  45. -- This software is released to the Ada community.
  46. -- This software is released to the Public Domain (note:
  47. --   software released to the Public Domain is not subject
  48. --   to copyright protection).
  49. -- Restrictions on use or distribution:  NONE
  50. --                                                           -*
  51. ------------------ Disclaimer ---------------------------------
  52. --                                                           -*
  53. -- This software and its documentation are provided "AS IS" and
  54. -- without any expressed or implied warranties whatsoever.
  55. -- No warranties as to performance, merchantability, or fitness
  56. -- for a particular purpose exist.
  57. --
  58. -- Because of the diversity of conditions and hardware under
  59. -- which this software may be used, no warranty of fitness for
  60. -- a particular purpose is offered.  The user is advised to
  61. -- test the software thoroughly before relying on it.  The user
  62. -- must assume the entire risk and liability of using this
  63. -- software.
  64. --
  65. -- In no event shall any person or organization of people be
  66. -- held responsible for any direct, indirect, consequential
  67. -- or inconsequential damages or lost profits.
  68. --                                                           -*
  69. -------------------END-PROLOGUE--------------------------------
  70.  
  71. ::::::::::
  72. VDT100.ADA
  73. ::::::::::
  74.  
  75. --name       : Screen Control
  76. --purpose    : to allow different screen manipulations from a program
  77. --exports    : see following
  78. --exceptions : Mode Error
  79. --             Attribute Error
  80. --imports    : Text IO
  81. --history    :
  82. --                 date      author                reason
  83. --             11-7-84    Brenda Triplett  midterm & for use in 401
  84. ---------------------------------------------------------------------------
  85. ---------------------------------------------------------------------------
  86. --exports
  87. ---------
  88. --name        : Gotoxy
  89. --purpose     : move to location on screen
  90. --parameters  : X => line num
  91. --              Y => column num
  92. -----------------------------------------------------------------------------
  93. --name        : Clear Screen
  94. --purpose     : blank screen
  95. -------------------------------------------------------------------------------
  96. --name        : Set Mode
  97. --purpose     : change from 80 to 132 columns and back
  98. --parameters  : Columns => number of columns desired
  99. --exceptions  : Mode Error => when columns is neither 80 of 132
  100. -------------------------------------------------------------------------------
  101. --name        : Clear Line
  102. --purpose     : Blank out from cursor to end of line
  103. -------------------------------------------------------------------------------
  104. --name        : Set Attributes
  105. --purpose     : change characteristics of the screen
  106. --parameters  : Which1 & Which2 => number corresponding to a characteristic
  107. --exceptions  : Attribute error => when Which1 or Which2 does not correspond
  108. --              to a characteristic
  109. -------------------------------------------------------------------------------
  110. --name        : Set Block
  111. --purpose     : define area when attributes are effective
  112. --parameters  : Width => number of columns in block
  113. --              Depth => number of lines in block
  114. -------------------------------------------------------------------------------
  115. --name        : Cursor Up, Cursor Down
  116. --purpose     : move cursor
  117. --parameters  : Lines => number of lines
  118. -------------------------------------------------------------------------------
  119. --name        : Cursor Forward, Cursor Backwar
  120. --purpose     : move cursor
  121. --parameters  : Columns => number of columns
  122. -------------------------------------------------------------------------------
  123. --name        : Next Line, Previous Line
  124. --purpose     : move cursor
  125. --parameters  : How Many => number of lines
  126. -------------------------------------------------------------------------------
  127. package Screen_Control is
  128.   procedure Gotoxy(X,Y:natural);
  129.  
  130.   procedure Clear_Screen;
  131.  
  132.   procedure Set_Mode(Columns:natural);
  133.   Mode_Error : Exception;
  134.  
  135.   procedure Clear_Line;
  136.  
  137.   procedure Set_Attributes(Which1,Which2 : Integer);
  138.   Attribute_Error : Exception;
  139.   --0 => normal intensity
  140.   --1 => increase intensity
  141.   --2 => decrease intensity
  142.   --3 => blank
  143.   --4 => underline
  144.   --5 => blink
  145.   --7 => reverse video
  146.  
  147.   procedure Set_Block(Width,Depth : Integer);
  148.  
  149.   procedure Cursor_Up(Lines : Integer);
  150.   procedure Cursor_Down(Lines : Integer);
  151.   procedure Cursor_Forward(Columns : Integer);
  152.   procedure Cursor_Backward(Columns : Integer);
  153.  
  154.   procedure Next_Line(How_Many : Integer);
  155.   procedure Previous_Line(How_Many : Integer);
  156. end Screen_Control;
  157.  
  158. with Text_IO;
  159. use Text_IO;
  160. package body Screen_Control is
  161.  
  162. procedure Gotoxy(X,Y:natural) is
  163. begin
  164.   Put(Ascii.ESC);
  165.   Put("[");
  166.   Integer_IO.Put(X,1);
  167.   Put(";");
  168.   Integer_IO.Put(Y,1);
  169.   Put("H");
  170. end Gotoxy;
  171.  
  172. procedure Clear_Screen is
  173. begin
  174.   Put(Ascii.ESC);
  175.   Put("[2J");
  176. end Clear_Screen;
  177.  
  178. procedure Set_Mode(Columns:natural) is
  179. begin
  180.   if Columns /=80 and Columns /=132
  181.   then raise Mode_Error;
  182.   elsif Columns = 80
  183.      then
  184.        Put(Ascii.ESC);
  185.        Put("[?");
  186.        Integer_IO.Put(3,1);
  187.        Put("l");
  188.      else
  189.        Put(Ascii.ESC);
  190.        Put("[?");
  191.        Integer_IO.Put(3,1);
  192.        Put("h");
  193.   end if;
  194. end Set_Mode;
  195.  
  196. procedure Clear_Line is
  197.  
  198. begin
  199.   Put(Ascii.ESC);
  200.   Put("[");
  201.   Integer_IO.Put(0,1);
  202.   Put("K");
  203. end Clear_Line;
  204.  
  205. procedure Set_Attributes(Which1,Which2 : Integer) is
  206. begin
  207.   if Which1 < 8 and Which1 >= 0 and Which1 /= 6 and
  208.      Which2 < 8 and Which2 >= 0 and Which2 /= 6
  209.   then
  210.     Put(Ascii.ESC);
  211.     Put("[");
  212.     Integer_IO.Put(Which1);
  213.     Put(";");
  214.     Integer_IO.Put(Which2);
  215.     Put("m");
  216.   else raise Attribute_Error;
  217.   end if;
  218. end Set_Attributes;
  219.  
  220. procedure Set_Block(Width,Depth : Integer) is
  221. begin
  222.   Put(Ascii.ESC);
  223.   Put("[");
  224.   Integer_IO.Put(Width,1);
  225.   Put(";");
  226.   Integer_IO.Put(Depth,1);
  227.   Put(" q");
  228. end Set_Block;
  229.  
  230. procedure Cursor_Up(Lines : Integer) is
  231. begin
  232.   Put(Ascii.ESC);
  233.   Put("[");
  234.   Integer_IO.Put(Lines,1);
  235.   Put("A");
  236. end Cursor_Up;
  237.  
  238. procedure Cursor_Down(Lines : Integer) is
  239. begin
  240.   Put(Ascii.ESC);
  241.   Put("[");
  242.   Integer_IO.Put(Lines,1);
  243.   Put("B");
  244. end Cursor_Down;
  245.  
  246. procedure Cursor_Forward(Columns : Integer) is
  247. begin
  248.   Put(Ascii.ESC);
  249.   Put("[");
  250.   Integer_IO.Put(Columns,1);
  251.   Put("C");
  252. end Cursor_Forward;
  253.  
  254. procedure Cursor_Backward(Columns : Integer) is
  255. begin
  256.   Put(Ascii.ESC);
  257.   Put("[");
  258.   Integer_IO.Put(Columns,1);
  259.   Put("D");
  260. end Cursor_Backward;
  261.  
  262. procedure Next_Line(How_Many : Integer) is
  263. begin
  264.   Put(Ascii.ESC);
  265.   Put("[");
  266.   Integer_IO.Put(How_Many,1);
  267.   Put("E");
  268. end Next_Line;
  269.  
  270. procedure Previous_Line(How_Many : Integer) is
  271. begin
  272.   Put(Ascii.ESC);
  273.   Put("[");
  274.   Integer_IO.Put(How_Many,1);
  275.   Put("F");
  276. end Previous_Line;
  277. end Screen_Control;
  278.  
  279.  
  280. ::::::::::
  281. HANOI.ADA
  282. ::::::::::
  283.  
  284. --name       : Towers of Hanoi
  285. --purpose    : to display the solution of the Towers of Hanoi problem
  286. --imports    : Screen Control, Text_IO
  287. --algorithms :
  288. --history    :
  289. --              date        author              reason
  290. --              11-5-84     Brenda Triplett     midterm
  291. -----------------------------------------------------------------------------
  292. --name       : Get Disk Num
  293. --purpose    : read in the number of disks to be moved
  294. --exports    : the number
  295. -----------------------------------------------------------------------------
  296. --name       : Set Up Screen
  297. --purpose    : set up initial screen for Towers of Hanoi problem
  298. -----------------------------------------------------------------------------
  299. --name       : Hanoi
  300. --purpose    : solve Towers of Hanoi problem
  301. --parameters : N => number of disks to be moved
  302. --             X => to be moved from
  303. --             Y => to be moved to
  304. --             Z => to use in moving
  305. --algorithms : if the number of disks to be moved is qreater than zero
  306. --             then
  307. --               call Hanoi with one less disk to be moved from x to z using y
  308. --               move the n-th disk
  309. --               call Hanoi with one less disk to be moved from z to y using x
  310. ------------------------------------------------------------------------------
  311. --name       : Move
  312. --purpose    : proform move in Hanoi algorithm
  313. --parameters : From => to be moved from
  314. --             To => to be moved to
  315. --             N => which disk is to be moved
  316. --algorithms : locate where on the screen the disk is to be moved from
  317. --             remove disk
  318. --             locate where on the screen the disk is to be moved to
  319. --             place disk
  320. ------------------------------------------------------------------------------
  321. --name       : Locate Disk
  322. --purpose    : locate where on the screen the disk is or will be
  323. --parameters : Where => is the disk on X, Y, or Z rod
  324. --             Which => which disk
  325. -------------------------------------------------------------------------------
  326. --name       : Remove Disk
  327. --purpose    : move disk from the screen location
  328. --parameters : Where => is the disk on X, Y, or Z rod
  329. -------------------------------------------------------------------------------
  330.  
  331. package Towers_Of_Hanoi is
  332. end Towers_Of_Hanoi;
  333.  
  334.  
  335. with Screen_Control,Text_IO;
  336. use Screen_Control,Text_IO;
  337.  
  338. package body Towers_Of_Hanoi is
  339.   subtype Num is integer range 0..8;
  340.   type Rod is (X,Y,Z);
  341.   N           :Num;
  342.   How_Many    :array (Rod) of Num;
  343.  
  344. --Locate_Disk & Remove_Disk & Place_Disk
  345.  
  346.   subtype Line is integer range 0..24;
  347.   subtype Column is integer range 0..80;
  348.   Start_Line,
  349.   Stop_Line        : Line;
  350.   Start_Col,
  351.   Stop_Col         : Column;
  352.   Center           : Column;
  353.  
  354.   procedure Get_Disk_Num(N: out Num) is
  355.   begin
  356.     Put("enter number of disks (1-8) ");
  357.     Integer_IO.Get(N,1);
  358.     exception
  359.       when Constraint_Error => Get_Disk_Num(N);
  360.   end Get_Disk_Num;
  361.  
  362.   procedure Set_Up_Screen is
  363.   begin
  364.     Clear_Screen;
  365.     Gotoxy(20,9);
  366.     Put("----------------");
  367.     Gotoxy(20,33);
  368.     Put("----------------");
  369.     Gotoxy(20,57);
  370.     Put("----------------");
  371.     Gotoxy(22,16);
  372.     Put("X");
  373.     Gotoxy(22,40);
  374.     Put("Y");
  375.     Gotoxy(22,64);
  376.     Put("Z");
  377.     for Line_Num in 2..19 loop
  378.       Gotoxy(Line_Num,16);
  379.       Put("|");
  380.       Gotoxy(Line_Num,40);
  381.       Put("|");
  382.       Gotoxy(Line_Num,64);
  383.       Put("|");
  384.     end loop;
  385.   end Set_Up_Screen;
  386.  
  387.   procedure Locate_Disk(Where:Rod; Which:Num) is
  388.     Largest  :Num;
  389.   begin
  390.     Largest := 8 - Which;
  391.     case Where is
  392.       when X => Center := 16;
  393.                 Start_Col := 9 + Largest;
  394.                 Stop_Col := 24 - Largest;
  395.       when Y => Center := 40;
  396.                 Start_Col := 33 + Largest;
  397.                 Stop_Col := 48 - Largest;
  398.       when Z => Center := 64;
  399.                 Start_Col := 57 + Largest;
  400.                 Stop_Col := 72 - Largest;
  401.     end case;
  402.     Start_Line := 19 - (How_Many(Where) * 2);
  403.     Stop_Line := Start_Line - 1;
  404.   end Locate_Disk;
  405.  
  406.   procedure Remove_Disk(Where:Rod) is
  407.   begin
  408.     How_Many(Where) := How_Many(Where) - 1;
  409.     Start_Line := Start_Line + 2;
  410.     Stop_Line := Stop_Line + 2;
  411.     Gotoxy(Start_Line,Start_Col);
  412.     Put(" ");
  413.     Gotoxy(Start_Line,Stop_Col);
  414.     Put(" ");
  415.     Gotoxy(Stop_Line,Start_Col);
  416.     for Position in Start_Col..Stop_Col loop
  417.       Put(" ");
  418.     end loop;
  419.     Gotoxy(Start_Line,Center);
  420.     Put("|");
  421.     Gotoxy(Stop_Line,Center);
  422.     Put("|");
  423.   end Remove_Disk;
  424.  
  425.   procedure Place_Disk(Where:Rod) is
  426.   begin
  427.     How_Many(Where) := How_Many(Where) + 1;
  428.     Gotoxy(Start_Line,Center);
  429.     Put(" ");
  430.     Gotoxy(Start_Line,Start_Col);
  431.     Put("|");
  432.     Gotoxy(Start_Line,Stop_Col);
  433.     Put("|");
  434.     Gotoxy(Stop_Line,Start_Col);
  435.     for Position in Start_Col..Stop_Col loop
  436.       Put("-");
  437.     end loop;
  438.   end Place_Disk;
  439.  
  440.   procedure Move(From,To:Rod; N:Num) is
  441.   begin
  442.     Gotoxy(24,1);
  443.     Put("move disk # ");
  444.     Integer_IO.Put(N,1);
  445.     Put("from ");
  446.     case From is
  447.       when X => Put("X");
  448.       when Y => Put("Y");
  449.       when Z => Put("Z");
  450.     end case;
  451.     Put(" to ");
  452.     case To is
  453.       when X => Put("X");
  454.       when Y => Put("Y");
  455.       when Z => Put("Z");
  456.     end case;
  457.     Locate_Disk(From,N);
  458.     Remove_Disk(From);
  459.     Locate_Disk(To,N);
  460.     Place_Disk(To);
  461.     for Counter in 1..Integer'last*(999/1000) loop
  462.       null;
  463.     end loop;
  464.   end Move;
  465.  
  466.   procedure Hanoi(N:Num; X,Y,Z:Rod) is
  467.   begin
  468.     if N /= 0
  469.     then
  470.       Hanoi(N-1,X,Z,Y);
  471.       Move(X,Y, N);
  472.       Hanoi(N-1,Z,Y,X);
  473.     end if;
  474.   end Hanoi;
  475.  
  476. --towers of hanoi
  477. begin
  478.   How_Many(X) := 0;
  479.   How_Many(Y) := 0;
  480.   How_Many(Z) := 0;
  481.   Clear_Screen;
  482.   Get_Disk_Num(N);
  483.   Set_Up_Screen;
  484.   for Number in reverse 1..N loop
  485.     Locate_Disk(X,Number);
  486.     Place_Disk(X);
  487.     for Counter in 1..Integer'last*(99/100) loop
  488.       null;
  489.     end loop;
  490.   Hanoi(N,X,Y,Z);
  491. end Towers_Of_Hanoi;
  492.  
  493.  
  494.