#include <stdio.h>
#include "genlib.h"



/*   Enumeration to keep track of Ringo's situation.   */
typedef enum {
   OCTOPUS_LAND,
   EEL_LAND,
   CRAB_LAND,
} Location;


Location Move(Location current, char act)
{
   Location next = OCTOPUS_LAND;

   /*   Branch out for the location.   */
   switch(current)
   {
   case OCTOPUS_LAND:
      if(act != 'e')
      {
         printf("Nothing interesting in this direction.\n");
         printf("Let's go to the east.\n");
      }
      printf("This area is packed full of electric eels.\n");
      next = EEL_LAND;
      break;

   case EEL_LAND:
      if(act == 'w')
      {
         printf("You don't want to go back to the octopus' garden...\n");
         printf("Let's go to the north.\n");
      }
      else if(act != 'n')
      {
         printf("Nothing interesting in this drection.\n");
         printf("Let's go to the north.\n");
      }
      printf("You swim north, away from the electric eels.\n");
      printf("There is a crab here.\n");
      next = CRAB_LAND;
      break;

   case CRAB_LAND:
      if(act != 's')
      {
         printf("Nothing interesting in this drection.\n");
         printf("Let's go to the south.\n");
      }
      printf("This area is packed full of electric eels.\n");
      next = EEL_LAND;
      break;
         
   default:
      /*   Shouldn't happen.   */
      printf("Unknown situation: %d\n", current);
      exit(1);
      break;
   }
   return next;
}


void main()
{
   /*   These boolean variables keep track of Ringo's situations.   */
   bool holdingOctopus = FALSE;
   bool octopusDazed = FALSE;
   bool new_holdingOctopus = FALSE;
   bool new_octopusDazed = FALSE;

   Location location = OCTOPUS_LAND ;

   char act;
   string dirstr;

   /*   Introductory text.   */
   printf("You're in an octopus' garden, just like in your ");
   printf("song, except the octopus is\nthrowing things at ");
   printf("you and frightening you.\n");

   while(1)
   {
      /*   Receive input from the user.   */
      printf("Your action: ");
      act = getchar();
      getchar();

      /*   These will have proposed changes.   */
      new_holdingOctopus = holdingOctopus;
      new_octopusDazed = octopusDazed;


      /*   Check for the quit command as a special case.   */
      if(act == 'q')
      {
         printf("You quit, weary of undersea life already.\n");
         break;
      }

      /*   Branch out depending on the user's input.   */
      switch(act)
      {
      case 'n':
      case 's':
      case 'e':
      case 'w':
         /*   Let's take care of all the cases for swiming in one shot.   */
         /*   Find out the string corresponding to the direction.   */
         switch(act)
         {
         case 'n': dirstr = "north"; break;
         case 's': dirstr = "south"; break;
         case 'e': dirstr = "east"; break;
         case 'w': dirstr = "west"; break;
         }

         if ( location==OCTOPUS_LAND )
         {
           if ( !octopusDazed )
           {
             printf("You try to swim %s, but the octopus gets in ", dirstr);
             printf("your way.\n");
             new_holdingOctopus = new_octopusDazed = FALSE;
           }
           else
           {
             printf("You swim %s, finally free of the ", dirstr);
             printf("octopus' garden.\n");
             location = Move ( location, act ) ;
           }
         }
         else
         {
           location = Move ( location, act ); 
         }
         break;

      case 't':
         /*   Throw.   */
         switch(location)
         {
         case OCTOPUS_LAND:
            if(holdingOctopus)
            {
               printf("You throw the octopus onto the ground.  It is ");
               printf("momentarily dazed.\n");
               new_octopusDazed = TRUE;
               new_holdingOctopus = FALSE;
            }
            else
            {
               printf("You try to throw something, but you have nothing ");
               printf("in your hands.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            break;
        default:
            {
               printf("Nothing happens.\n");
            }
            break;
         }
         break;

      case 'g':
         /*   Grab.   */
         switch(location)
         {
         case OCTOPUS_LAND:
            if(!holdingOctopus && !octopusDazed)
            {
               printf("You grab the octopus with your mighty European ");
               printf("arms.\n");
               new_holdingOctopus = TRUE;
            }
            else if(holdingOctopus)
            {
               printf("You tried to hold another octopus and failed.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            else
            {
               /*   Octopus should be dazed.   */
               printf("You tried to hold the dazed octopus and failed.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            break;
         case EEL_LAND:
            printf("You grab some eels.  Their ultra-conductive skin ");
            printf("makes them very slippery and\npainful to hold, ");
            printf("but the thought of all that free electricity ");
            printf("strengthens your\ngrip.\n");
            break;
         case CRAB_LAND:
            printf("You grab the crab.\n");
            printf("This will taste good with a Diet Coke, or a Tab.\n");
            break;
         default:
            /*   Shouldn't happen.   */
            printf("Unknown location: %d\n", location);
            exit(1);
            break;
         }
         break;

      case 'y':
         /*   Yell.   */
         if (location==OCTOPUS_LAND)
         {
             printf("You yell at the octopus, \"go watch TV!\"\n");
             new_holdingOctopus = new_octopusDazed = FALSE;
         }
         else
         {
             printf("You yell, \"I hate the ocean!\"\n");
             printf("Nothing happens.\n");
         }
         break;

      case 'h':
         /*   Hide.   */
         printf("You try to hide, but there is nothing to hide ");
         printf("behind.\n");
         if (location==OCTOPUS_LAND)
         {
            new_holdingOctopus = new_octopusDazed = FALSE;
         }
         break;

      default:
         /*   Error case.  Just let the user know.   */
         printf("Unknown action, type n, s, e, w, y, h, g, t, or q.\n");
         break;
         }


      /*   Set the situation variables to new values.   */
      if(holdingOctopus && !new_holdingOctopus && !octopusDazed
         && !new_octopusDazed)
      {
         printf("The octopus wriggles free of your grasp.\n");
      }
      if(octopusDazed && !new_octopusDazed)
      {
         printf("The octopus recovers from its fall.\n");
      }
      holdingOctopus = new_holdingOctopus;
      octopusDazed = new_octopusDazed;

      /*   Check if Ringo is still being harrased.   */
      if(!holdingOctopus && !octopusDazed)
      {
         /*   No progress made yet.   */
         printf("The octopus keeps harassing you.\n");
      }
   }
}


