/*                               gameloop.c                             */
/*                                                                      */
/* Okay, now it's time to play...                                       */


int pairs;     /* Must be global! */
int playercolor;
int movingplayer;
static int bonus;

gameloop()
   {
   extern int selectbutton;
   extern int turn1, turn2;
   extern int score1, score2;
   extern struct Screen *s;
   extern struct Window *w;
   extern struct RastPort *rp;
   extern int enemy;
   extern int percentage;

   int success;
   int whichplayer;



/* This is where the game is played! */

   pairs = 16;    /* pairs available */
   bonus = 0;



/* It's better to move first, so we must decide which player will be    */
/* the first to move: we pick a number in the range (1 - 100) and if    */
/* this number is uneven than the player that moves first is the blue   */
/* one. Otherwise the first to move is the yellow one...                */

   whichplayer = customrand(1, 100);      /* establish player to move */

   if (whichplayer & 1)          /* Uneven: blue player moves first */
      {
      while(pairs > 0)
         {
         success = TRUE;
      
         while(success == TRUE && pairs > 0)
            {
            selectbutton = FALSE;
            playercolor = COLOR1;
            movingplayer = 1;
            success = picktwocards(FIRST);
            if (success == PICKED_QUIT)
               return(success);
            }

         success = TRUE;
         if ((enemy) && (pairs > 0))
            {
            writestring(rp, "Now it's my turn!", COLOR2);
            Delay(70);
            writestring(rp, "Now it's my turn!", FALSE);
            }

         while(success == TRUE && pairs > 0)
            {
            selectbutton = FALSE;
            playercolor = COLOR2;
            movingplayer = 2;
            if (enemy)     /* If I'm playing against my Amiga... */
               {
               OffMenu(w, 0x3f00);
               OffMenu(w, 0x3f01);
               success = amigamoves(); /* Let her make a move */
               OnMenu(w, 0x3f00);
               OnMenu(w, 0x3f01);
               }
            else
               {
               success = picktwocards(SECOND);
               if (success == PICKED_QUIT)
                  return(success);
               }
            }
         }
      }



   else                          /* Even: yellow player moves first */
      {
      while(pairs > 0)
         {
         success = TRUE;
         if (enemy)
            {
            writestring(rp, "Now it's my turn!", COLOR2);
            Delay(70);
            writestring(rp, "Now it's my turn!", FALSE);
            }

         while(success == TRUE && pairs > 0)
            {
            selectbutton = FALSE;
            playercolor = COLOR2;
            movingplayer = 2;
            if (enemy)              /* If I'm playing against my amiga */
               {
               OffMenu(w, 0x3f00);
               OffMenu(w, 0x3f01);
               success = amigamoves(); /* Let her make a move */
               OnMenu(w, 0x3f00);
               OnMenu(w, 0x3f01);
               }
            else
               {
               success = picktwocards(SECOND);
               if (success == PICKED_QUIT)
                  return(success);
               }
            }

         success = TRUE;

         while(success == TRUE && pairs > 0)
            {
            selectbutton = FALSE;
            playercolor = COLOR1;
            movingplayer = 1;
            success = picktwocards(FIRST);
            if (success == PICKED_QUIT)
               return(success);
            }
         }
      }



/* Game over: display informations about the winner (if any...) */

   if (score1 > score2)
      writestring(rp, "Player 1, you are the winner!", COLOR1);
   else if (score2 > score1)
      {
      if (enemy)
         writestring(rp, "Yeah! I Am The Winner!", COLOR2);
      else
         writestring(rp, "Player 2, you are the winner!", COLOR2);
      }
   else if (turn1 < turn2)
      writestring(rp, "Player 1, you are the winner!", COLOR1);
   else if (turn1 > turn2)
      {
      if (enemy)
         writestring(rp, "Yeah! I Am The Winner!", COLOR2);
      else
         writestring(rp, "Player 2, you are the winner!", COLOR2);
      }
   else
      writestring(rp, "No winner this time...", COLOR3);

   OnMenu(w, 0x3f02);   /* Re-enable menus */
   OnMenu(w, 0x3f03);
   OnMenu(w, 0x0001);
   OffMenu(w, 0x0021);
   OffMenu(w, 0x0041);

   if (levelitem[0].Flags & CHECKED)
      percentage = 10;
   else if (levelitem[1].Flags & CHECKED)
      percentage = 20;
   else if (levelitem[2].Flags & CHECKED)
      percentage = 30;
   }



picktwocards(flag)
   int flag;
   {
   extern struct Window *w;
   extern int turn1, turn2;
   extern int score1, score2;
   extern int selectbutton;
   extern int memory;

   int myvalue;
   int cardnumber1;
   int cardnumber2;
   int cardimage1;
   int cardimage2;
   int situation = FALSE;

   writestring(rp, PICK_FIRST_STRING, playercolor);
   OnMenu(w, 0x0021);               /* First card: I can peek */

   do
      {
      cardnumber1 = 1000;           /* Default illegal values */
      cardimage1 = 1000;            /* Default illegal values */
      myvalue = waitformess();
      if (myvalue == PICKED_QUIT)
         return(myvalue);
      if (selectbutton)
         {
         cardnumber1 = pickacard();
         if (cardnumber1 != 1000)
            cardimage1 = showcard(cardnumber1);
         }
      }
   while(cardnumber1 == 1000 || cardimage1 == 1000);
   writestring(rp, PICK_FIRST_STRING, FALSE);

   writestring(rp, PICK_SECOND_STRING, playercolor);
   OffMenu(w, 0x0021);              /* Second card: I can't peek */

   do
      {
      cardnumber2 = 1000;           /* Default illegal value */
      cardimage2 = 1000;            /* Default illegal value */
      myvalue = waitformess();
      if (myvalue == PICKED_QUIT)
         return(myvalue);
      if (selectbutton)
         {
         cardnumber2 = pickacard();
         if ((cardnumber2 != 1000) && (cardnumber2 != cardnumber1))
            cardimage2 = showcard(cardnumber2);
         }
      }
   while(cardnumber2 == 1000 || cardimage2 == 1000);
   writestring(rp, PICK_SECOND_STRING, FALSE);

   if (cardimage1 == cardimage2)
      {
      bonus += 1000;
      if (pairs > 1)
         writestring(rp, RIGHT_STRING, COLOR3);
      Delay(50);
      deletecard(cardnumber1);
      deletecard(cardnumber2);

      if (flag == FIRST)
         {
         score1 += bonus;
         turn1 += 1;
         pairs -= 1;
         update(flag, turn1, score1);
         }
      else
         {
         score2 += bonus;
         turn2 += 1;
         pairs -= 1;
         update(flag, turn2, score2);
         }
      situation = TRUE;
      if (pairs > 0)
         writestring(rp, RIGHT_STRING, FALSE);
      return(situation);
      }

   bonus = 0;
   writestring(rp, WRONG_STRING, COLOR3);
   Delay(50);
   covercard(cardnumber1);
   covercard(cardnumber2);

   if (flag == FIRST)
      {
      turn1 += 1;
      update(flag, turn1, score1);
      }
   else
      {
      turn2 += 1;
      update(flag, turn2, score2);
      }
   situation = FALSE;
   writestring(rp, WRONG_STRING, FALSE);
   return(situation);
   }



pickacard()
   {
   extern int currentmousex;
   extern int currentmousey;
   int row, col;
   int cardnumber;

   if (currentmousex >= 2 && currentmousex <= 30)
      col = 0;
   else if (currentmousex >= 33 && currentmousex <= 61)
      col = 1;
   else if (currentmousex >= 64 && currentmousex <= 92)
      col = 2;
   else if (currentmousex >= 95 && currentmousex <= 123)
      col = 3;
   else if (currentmousex >= 126 && currentmousex <= 154)
      col = 4;
   else if (currentmousex >= 157 && currentmousex <= 185)
      col = 5;
   else if (currentmousex >= 188 && currentmousex <= 216)
      col = 6;
   else if (currentmousex >= 219 && currentmousex <= 247)
      col = 7;
   else
      return(1000);


   if (currentmousey >= 14 && currentmousey <= 52)
      row = 0;
   else if (currentmousey >= 55 && currentmousey <= 93)
      row = 1;
   else if (currentmousey >= 96 && currentmousey <= 134)
      row = 2;
   else if (currentmousey >= 137 && currentmousey <= 175)
      row = 3;
   else
      return(1000);

   cardnumber = col + row * 8;
   return(cardnumber);
   }



showcard(card)
   int card;
   {
   extern UWORD mycardsposition[];
   extern struct Image mycardsimage[];
   extern struct RastPort *rp;
   int left = 2;
   int top = 13;
   int toffset;
   int loffset;
   int cardimage;

   toffset = card / 8;
   loffset = card %8 ;
   top = top + 41 * toffset;
   left = left + 31 * loffset;
   card += 1;
   cardimage = mycardsposition[card];

   if (cardimage != 1000)
      placecard(rp, &mycardsimage[cardimage], left, top);
   return(cardimage);
   }

covercard(card)
   int card;
   {
   extern struct Image mycardsimage[];
   extern struct RastPort *rp;
   int left = 2;
   int top = 13;
   int toffset;
   int loffset;

   toffset = card / 8;
   loffset = card %8 ;
   top = top + 41 * toffset;
   left = left + 31 * loffset;

   placecard(rp, &mycardsimage[0], left, top);
   return(0);
   }
