home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_show / hanoi / tower.e < prev   
Text File  |  1999-06-05  |  4KB  |  157 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://www.loria.fr/SmallEiffel
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class TOWER
  17.  
  18. creation full, empty
  19.  
  20. feature {NONE}
  21.  
  22.    t:ARRAY[INTEGER];
  23.  
  24.    top:INTEGER;
  25.  
  26. feature {NONE}
  27.  
  28.    full(n:INTEGER) is
  29.       require
  30.          n >= 1
  31.       local
  32.          i:INTEGER;
  33.       do
  34.          !!t.make(1,n);
  35.          from
  36.             i := n;
  37.          until
  38.             i = 0
  39.          loop
  40.             t.put(n-i+1,i);
  41.             i := i - 1;
  42.          end;
  43.          top := n;
  44.       ensure
  45.          nb = n;
  46.          top = nb;
  47.          t.item(top) = 1
  48.       end;
  49.  
  50.    empty(n:INTEGER) is
  51.       require
  52.          n >= 1
  53.       do
  54.          !!t.make(1,n);
  55.          top := 1;
  56.       ensure
  57.          nb = n;
  58.          top = 1
  59.       end;
  60.  
  61. feature {HANOI}
  62.  
  63.    nb: INTEGER is
  64.       do
  65.          Result := t.upper;
  66.       end;
  67.  
  68.    show_a_discus(d: INTEGER; picture: STRING) is
  69.       require
  70.          1 <= d;
  71.          d <= nb;
  72.          picture /= Void
  73.       local
  74.          nb_of_free_slots, nb_of_used_slots, i : INTEGER;
  75.       do
  76.          nb_of_used_slots := t.item(d);
  77.          nb_of_free_slots := nb - nb_of_used_slots;
  78.          from
  79.             i := nb_of_free_slots;
  80.          until
  81.             i = 0
  82.          loop
  83.             picture.extend(' ');
  84.             i := i - 1;
  85.          end;
  86.          from
  87.             i := nb_of_used_slots;
  88.          until
  89.             i = 0
  90.          loop
  91.             picture.extend('=');
  92.             i := i - 1;
  93.          end;
  94.          picture.extend('|');
  95.          from
  96.             i := nb_of_used_slots;
  97.          until
  98.             i = 0
  99.          loop
  100.             picture.extend('=');
  101.             i := i -1;
  102.          end;
  103.          from
  104.             i := nb_of_free_slots;
  105.          until
  106.             i = 0
  107.          loop
  108.             picture.extend(' ');
  109.             i := i - 1;
  110.          end;
  111.       end;
  112.  
  113.    remove_discus: INTEGER is
  114.       do
  115.          debug
  116.             if t.item(top) = 0 then
  117.                std_error.put_string("Error in 'remove_discus'.%N");
  118.                crash;
  119.             end;
  120.          end;
  121.          Result := t.item(top);
  122.          t.put(0,top);
  123.          if top > 1 then
  124.             top := top - 1;
  125.          end;
  126.       ensure
  127.          top >= 1
  128.       end;
  129.  
  130.    add_discus(d: INTEGER) is
  131.       do
  132.          debug
  133.             if (top = nb) then
  134.                std_error.put_string("Error in 'add_discus', %
  135.                                     %the tower was already full.%N")
  136.                crash;
  137.             end;
  138.             if (d > t.item(top)) then
  139. --           std_error.put_string("Error in 'add_discus', the %
  140. --                    %discus you wanted to put is larger %
  141. --                    %than allowed.");
  142. --           crash;
  143.             end;
  144.          end;
  145.          if t.item(top) > d then
  146.             top := top + 1;
  147.             t.put(d,top);
  148.          end;
  149.          if t.item(top) = 0 then
  150.             t.put(d,top);
  151.          end;
  152.       ensure
  153.          top <= nb
  154.       end;
  155.  
  156. end -- TOWER
  157.