home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / aijournl / 1987_03 / actor.mar < prev    next >
Text File  |  1987-02-19  |  3KB  |  74 lines

  1.  
  2.  
  3.                         "Actor Goes on Stage"
  4.                          by Andrew P. Bernat
  5.                          March 1987 AI EXPERT
  6.  
  7.  
  8. Listing 1.  
  9. A Comparison of ACTOR and Smalltalk-80
  10.  
  11.  
  12.  
  13.           /*  ACTOR solution to Towers of Hanoi  */ 
  14.           /*  Create a new class named TowerOfHanoi to handle the problem.
  15.               The new class is directly beneath Object in the hierarchy;
  16.               Object being at the top of the class hierarchy.*/
  17.           inherit(Object, #TowerOfHanoi, nil, nil, nil);
  18.           /*  Here we set the compiler's curClass instance variable,
  19.               which specifies which class the following messages are to
  20.               apply to. */
  21.           Compiler.curClass := TowerOfHanoi;
  22.           /*  Define a new message named moveTower */
  23.           Def moveTower(self, height, from, to, use) 
  24.           {  if height > 0 
  25.              then  moveTower(self, height - 1, from, use, to); 
  26.                 moveDisk(self, from, to); 
  27.                 moveTower(self, height - 1, use, to, from); 
  28.              endif; 
  29.           } 
  30.           /*  Define a new message named moveDisk */
  31.           Def moveDisk(self, from, to) 
  32.           {  eol(ThePort); 
  33.              print(from); print("->"); print(to); 
  34.           } 
  35.           /*  Add a new member of class Object to the system dictionary,
  36.               which is also named Actor, at key location sample */
  37.           Actor[#sample] := new(Object);
  38.           /*  Send message moveTower to the object sample.  sample, being
  39.               of class Object, will respond using the code defined above.
  40.               The parameters say to use three disks, and move them from
  41.               tower 1 to tower 3 using tower 2 */
  42.           moveTower(sample, 3, 1, 3, 2); 
  43.  
  44.           "  Tower of Hanoi code as implemented in Smalltalk/V.  Note that
  45.              these messages and methods would be implemented directly
  46.              through the System Browser. "
  47.           "  Declare a new sublcass of Object."
  48.           Object subclass: #TowerOfHanoi
  49.           "  Instance variables would be declared here, but none are
  50.              needed. "
  51.            TowerOfHanoi methods
  52.           "  Declare the moveTower message. "
  53.              moveTower: height 
  54.                   from: ftower to: ttower using: utower
  55.                (> height 0)
  56.                 ifTrue: [self moveTower: height - 1 
  57.                               from: ftower to: utower using: ttower.
  58.                          self moveDisk: ftower to: ttower.
  59.                          self moveTower: height - 1
  60.                               from: utower to: ttower using: ftower]
  61.           "  Declare the moveDisk message.  Transcript is a the standard
  62.              output window."
  63.             moveDisk: ftower to: ttower
  64.               Transcript cr.
  65.               Transcript show: ftower.
  66.               Transcript show: '->'.
  67.               Transcript show: ttower.  
  68.           "  To execute, evaluate 
  69.              (new TowerOfHanoi) moveTower: 3 from: 1 to: 3 using: 2"
  70.            
  71.  
  72. r.  
  73.           "  To execute, evaluate 
  74.              (new TowerOfHanoi) moveTower: 3 from: 1 to: 3 using: 2"