home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_show / hanoi / hanoi.e next >
Text File  |  1999-06-05  |  3KB  |  98 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 HANOI
  17.    --
  18.    -- The classic Tower of Hanoi game.
  19.    --
  20.    -- Compile with :
  21.    --    compile -o hanoi hanoi -boost
  22.    -- Run with :
  23.    --    hanoi
  24.    --
  25.  
  26. inherit
  27.    ANY redefine out_in_tagged_out_memory
  28.       end;
  29.  
  30. creation make
  31.  
  32. feature {NONE}
  33.  
  34.    nb: INTEGER;
  35.  
  36.    t1, t2, t3 : TOWER;
  37.  
  38. feature
  39.  
  40.    make is
  41.       do
  42.          io.put_string("Type the number of discus, please : ");
  43.          io.flush;
  44.          io.read_integer;
  45.          nb := io.last_integer;
  46.          !!t1.full(nb);
  47.          !!t2.empty(nb);
  48.          !!t3.empty(nb);
  49.          io.put_string("Situation at the beginning : %N");
  50.          move(nb,t1,t2,t3);
  51.          io.put_string("Situation at the end : %N");
  52.          print_on(io);
  53.       end;
  54.  
  55.    move(how_many: INTEGER; source, intermediate, destination: TOWER) is
  56.       local
  57.          discus: INTEGER;
  58.       do
  59.          if (how_many > 0) then
  60.             move(how_many-1,source,destination,intermediate);
  61.             print_on(io);
  62.             discus := source.remove_discus;
  63.             destination.add_discus(discus);
  64.             move(how_many-1,intermediate,source,destination);
  65.          end;
  66.       end;
  67.  
  68.    out_in_tagged_out_memory is
  69.       local
  70.          i: INTEGER;
  71.       do
  72.          tagged_out_memory.extend('%N');
  73.          from
  74.             i := nb;
  75.          until
  76.             i = 0
  77.          loop
  78.             tagged_out_memory.extend(' ');
  79.             t1.show_a_discus(i,tagged_out_memory);
  80.             tagged_out_memory.extend(' ');
  81.             t2.show_a_discus(i,tagged_out_memory);
  82.             tagged_out_memory.extend(' ');
  83.             t3.show_a_discus(i,tagged_out_memory);
  84.             tagged_out_memory.extend('%N');
  85.             i := i - 1;
  86.          end;
  87.          from
  88.             i := (((2 * (nb + 1)) + 1) * 3) - 2;
  89.          until
  90.             i = 0
  91.          loop
  92.             tagged_out_memory.extend('-');
  93.             i := i - 1;
  94.          end;
  95.          tagged_out_memory.extend('%N');
  96.       end;
  97. end -- HANOI
  98.