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



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

   char act;

   /*   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();

      /*   If we escaped already, we check the input only for quit.   */
      if(act == 'q')
      {
         printf("You quit, weary of undersea life already.\n");
         break;
      }
      if(!escapedFromOctopus)
      {
         /*   These will have proposed changes.   */
         new_holdingOctopus = holdingOctopus;
         new_octopusDazed = octopusDazed;

         /*   Branch out depending on the user's input.   */
         switch(act)
         {
         case 'n':
            /*   Swim north.   */
            if(octopusDazed)
            {
               printf("You swim north, finally free of the ");
               printf("octopus' garden.\n");
               escapedFromOctopus = TRUE;
            }
            else
            {
               printf("You try to swim north, but the octopus gets in ");
               printf("your way.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            break;

         case 's':
            /*   Swim south.   */
            if(octopusDazed)
            {
               printf("You swim south, finally free of the ");
               printf("octopus' garden.\n");
               escapedFromOctopus = TRUE;
            }
            else
            {
               printf("You try to swim south, but the octopus gets in ");
               printf("your way.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            break;

         case 'e':
            /*   Swim east.   */
            if(octopusDazed)
            {
               printf("You swim east, finally free of the ");
               printf("octopus' garden.\n");
               escapedFromOctopus = TRUE;
            }
            else
            {
               printf("You try to swim east, but the octopus gets in ");
               printf("your way.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            break;

         case 'w':
            /*   Swim west.   */
            if(octopusDazed)
            {
               printf("You swim west, finally free of the ");
               printf("octopus' garden.\n");
               escapedFromOctopus = TRUE;
            }
            else
            {
               printf("You try to swim west, but the octopus gets in ");
               printf("your way.\n");
               new_holdingOctopus = new_octopusDazed = FALSE;
            }
            break;

         case 't':
            /*   Throw.   */
            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;

         case 'g':
            /*   Grab.   */
            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 'y':
            /*   Yell.   */
            printf("You yell at the octopus, \"go watch TV!\"\n");
            new_holdingOctopus = new_octopusDazed = FALSE;
            break;

         case 'h':
            /*   Hide.   */
            printf("You try to hide, but there is nothing to hide ");
            printf("behind.\n");
            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");
         }
      }
   }
}

