WITH Spider;
WITH Ada.Numerics.Discrete_Random;
PROCEDURE Drunken_Spider IS
------------------------------------------------------------------------
--| Spider tries to tour its room but has drunk too much, so
--| takes a random number of steps and may hit the wall. If the
--| spider hits the wall, it turns around and keeps going.
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------
 
  SUBTYPE RandomSteps IS Positive RANGE 1..20;
 
  PACKAGE Random_20 IS NEW Ada.Numerics.Discrete_Random
    (Result_Subtype => RandomSteps);
 
  G: Random_20.Generator;  -- funny variable; we must keep passing it
                           -- to Random, but can't use it.
 
  HowMany: RandomSteps;
 
BEGIN -- Drunken_Spider
 
  Spider.Start;
 
  LOOP                     -- keep going forever
 
    HowMany := Random_20.Random(Gen => G);
 
    -- Spider will count steps correctly but might change direction
    FOR Count IN 1..HowMany LOOP
 
      BEGIN   -- to handle exception
        Spider.Step;
      EXCEPTION
        WHEN Spider.Hit_the_Wall =>  -- turn around 180 degrees
          Spider.Right;
          Spider.Right;
      END;
 
    END LOOP;
 
    Spider.Right;
 
  END LOOP;
 
END Drunken_Spider;