home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d186 / cards'o'rama.lha / Cards'O'Rama / Sources / sources.zoo / gameloop.c < prev    next >
C/C++ Source or Header  |  1989-02-25  |  9KB  |  372 lines

  1. /*                               gameloop.c                             */
  2. /*                                                                      */
  3. /* Okay, now it's time to play...                                       */
  4.  
  5.  
  6. int pairs;     /* Must be global! */
  7. int playercolor;
  8. int movingplayer;
  9. static int bonus;
  10.  
  11. gameloop()
  12.    {
  13.    extern int selectbutton;
  14.    extern int turn1, turn2;
  15.    extern int score1, score2;
  16.    extern struct Screen *s;
  17.    extern struct Window *w;
  18.    extern struct RastPort *rp;
  19.    extern int enemy;
  20.    extern int percentage;
  21.  
  22.    int success;
  23.    int whichplayer;
  24.  
  25.  
  26.  
  27. /* This is where the game is played! */
  28.  
  29.    pairs = 16;    /* pairs available */
  30.    bonus = 0;
  31.  
  32.  
  33.  
  34. /* It's better to move first, so we must decide which player will be    */
  35. /* the first to move: we pick a number in the range (1 - 100) and if    */
  36. /* this number is uneven than the player that moves first is the blue   */
  37. /* one. Otherwise the first to move is the yellow one...                */
  38.  
  39.    whichplayer = customrand(1, 100);      /* establish player to move */
  40.  
  41.    if (whichplayer & 1)          /* Uneven: blue player moves first */
  42.       {
  43.       while(pairs > 0)
  44.          {
  45.          success = TRUE;
  46.       
  47.          while(success == TRUE && pairs > 0)
  48.             {
  49.             selectbutton = FALSE;
  50.             playercolor = COLOR1;
  51.             movingplayer = 1;
  52.             success = picktwocards(FIRST);
  53.             if (success == PICKED_QUIT)
  54.                return(success);
  55.             }
  56.  
  57.          success = TRUE;
  58.          if ((enemy) && (pairs > 0))
  59.             {
  60.             writestring(rp, "Now it's my turn!", COLOR2);
  61.             Delay(70);
  62.             writestring(rp, "Now it's my turn!", FALSE);
  63.             }
  64.  
  65.          while(success == TRUE && pairs > 0)
  66.             {
  67.             selectbutton = FALSE;
  68.             playercolor = COLOR2;
  69.             movingplayer = 2;
  70.             if (enemy)     /* If I'm playing against my Amiga... */
  71.                {
  72.                OffMenu(w, 0x3f00);
  73.                OffMenu(w, 0x3f01);
  74.                success = amigamoves(); /* Let her make a move */
  75.                OnMenu(w, 0x3f00);
  76.                OnMenu(w, 0x3f01);
  77.                }
  78.             else
  79.                {
  80.                success = picktwocards(SECOND);
  81.                if (success == PICKED_QUIT)
  82.                   return(success);
  83.                }
  84.             }
  85.          }
  86.       }
  87.  
  88.  
  89.  
  90.    else                          /* Even: yellow player moves first */
  91.       {
  92.       while(pairs > 0)
  93.          {
  94.          success = TRUE;
  95.          if (enemy)
  96.             {
  97.             writestring(rp, "Now it's my turn!", COLOR2);
  98.             Delay(70);
  99.             writestring(rp, "Now it's my turn!", FALSE);
  100.             }
  101.  
  102.          while(success == TRUE && pairs > 0)
  103.             {
  104.             selectbutton = FALSE;
  105.             playercolor = COLOR2;
  106.             movingplayer = 2;
  107.             if (enemy)              /* If I'm playing against my amiga */
  108.                {
  109.                OffMenu(w, 0x3f00);
  110.                OffMenu(w, 0x3f01);
  111.                success = amigamoves(); /* Let her make a move */
  112.                OnMenu(w, 0x3f00);
  113.                OnMenu(w, 0x3f01);
  114.                }
  115.             else
  116.                {
  117.                success = picktwocards(SECOND);
  118.                if (success == PICKED_QUIT)
  119.                   return(success);
  120.                }
  121.             }
  122.  
  123.          success = TRUE;
  124.  
  125.          while(success == TRUE && pairs > 0)
  126.             {
  127.             selectbutton = FALSE;
  128.             playercolor = COLOR1;
  129.             movingplayer = 1;
  130.             success = picktwocards(FIRST);
  131.             if (success == PICKED_QUIT)
  132.                return(success);
  133.             }
  134.          }
  135.       }
  136.  
  137.  
  138.  
  139. /* Game over: display informations about the winner (if any...) */
  140.  
  141.    if (score1 > score2)
  142.       writestring(rp, "Player 1, you are the winner!", COLOR1);
  143.    else if (score2 > score1)
  144.       {
  145.       if (enemy)
  146.          writestring(rp, "Yeah! I Am The Winner!", COLOR2);
  147.       else
  148.          writestring(rp, "Player 2, you are the winner!", COLOR2);
  149.       }
  150.    else if (turn1 < turn2)
  151.       writestring(rp, "Player 1, you are the winner!", COLOR1);
  152.    else if (turn1 > turn2)
  153.       {
  154.       if (enemy)
  155.          writestring(rp, "Yeah! I Am The Winner!", COLOR2);
  156.       else
  157.          writestring(rp, "Player 2, you are the winner!", COLOR2);
  158.       }
  159.    else
  160.       writestring(rp, "No winner this time...", COLOR3);
  161.  
  162.    OnMenu(w, 0x3f02);   /* Re-enable menus */
  163.    OnMenu(w, 0x3f03);
  164.    OnMenu(w, 0x0001);
  165.    OffMenu(w, 0x0021);
  166.    OffMenu(w, 0x0041);
  167.  
  168.    if (levelitem[0].Flags & CHECKED)
  169.       percentage = 10;
  170.    else if (levelitem[1].Flags & CHECKED)
  171.       percentage = 20;
  172.    else if (levelitem[2].Flags & CHECKED)
  173.       percentage = 30;
  174.    }
  175.  
  176.  
  177.  
  178. picktwocards(flag)
  179.    int flag;
  180.    {
  181.    extern struct Window *w;
  182.    extern int turn1, turn2;
  183.    extern int score1, score2;
  184.    extern int selectbutton;
  185.    extern int memory;
  186.  
  187.    int myvalue;
  188.    int cardnumber1;
  189.    int cardnumber2;
  190.    int cardimage1;
  191.    int cardimage2;
  192.    int situation = FALSE;
  193.  
  194.    writestring(rp, PICK_FIRST_STRING, playercolor);
  195.    OnMenu(w, 0x0021);               /* First card: I can peek */
  196.  
  197.    do
  198.       {
  199.       cardnumber1 = 1000;           /* Default illegal values */
  200.       cardimage1 = 1000;            /* Default illegal values */
  201.       myvalue = waitformess();
  202.       if (myvalue == PICKED_QUIT)
  203.          return(myvalue);
  204.       if (selectbutton)
  205.          {
  206.          cardnumber1 = pickacard();
  207.          if (cardnumber1 != 1000)
  208.             cardimage1 = showcard(cardnumber1);
  209.          }
  210.       }
  211.    while(cardnumber1 == 1000 || cardimage1 == 1000);
  212.    writestring(rp, PICK_FIRST_STRING, FALSE);
  213.  
  214.    writestring(rp, PICK_SECOND_STRING, playercolor);
  215.    OffMenu(w, 0x0021);              /* Second card: I can't peek */
  216.  
  217.    do
  218.       {
  219.       cardnumber2 = 1000;           /* Default illegal value */
  220.       cardimage2 = 1000;            /* Default illegal value */
  221.       myvalue = waitformess();
  222.       if (myvalue == PICKED_QUIT)
  223.          return(myvalue);
  224.       if (selectbutton)
  225.          {
  226.          cardnumber2 = pickacard();
  227.          if ((cardnumber2 != 1000) && (cardnumber2 != cardnumber1))
  228.             cardimage2 = showcard(cardnumber2);
  229.          }
  230.       }
  231.    while(cardnumber2 == 1000 || cardimage2 == 1000);
  232.    writestring(rp, PICK_SECOND_STRING, FALSE);
  233.  
  234.    if (cardimage1 == cardimage2)
  235.       {
  236.       bonus += 1000;
  237.       if (pairs > 1)
  238.          writestring(rp, RIGHT_STRING, COLOR3);
  239.       Delay(50);
  240.       deletecard(cardnumber1);
  241.       deletecard(cardnumber2);
  242.  
  243.       if (flag == FIRST)
  244.          {
  245.          score1 += bonus;
  246.          turn1 += 1;
  247.          pairs -= 1;
  248.          update(flag, turn1, score1);
  249.          }
  250.       else
  251.          {
  252.          score2 += bonus;
  253.          turn2 += 1;
  254.          pairs -= 1;
  255.          update(flag, turn2, score2);
  256.          }
  257.       situation = TRUE;
  258.       if (pairs > 0)
  259.          writestring(rp, RIGHT_STRING, FALSE);
  260.       return(situation);
  261.       }
  262.  
  263.    bonus = 0;
  264.    writestring(rp, WRONG_STRING, COLOR3);
  265.    Delay(50);
  266.    covercard(cardnumber1);
  267.    covercard(cardnumber2);
  268.  
  269.    if (flag == FIRST)
  270.       {
  271.       turn1 += 1;
  272.       update(flag, turn1, score1);
  273.       }
  274.    else
  275.       {
  276.       turn2 += 1;
  277.       update(flag, turn2, score2);
  278.       }
  279.    situation = FALSE;
  280.    writestring(rp, WRONG_STRING, FALSE);
  281.    return(situation);
  282.    }
  283.  
  284.  
  285.  
  286. pickacard()
  287.    {
  288.    extern int currentmousex;
  289.    extern int currentmousey;
  290.    int row, col;
  291.    int cardnumber;
  292.  
  293.    if (currentmousex >= 2 && currentmousex <= 30)
  294.       col = 0;
  295.    else if (currentmousex >= 33 && currentmousex <= 61)
  296.       col = 1;
  297.    else if (currentmousex >= 64 && currentmousex <= 92)
  298.       col = 2;
  299.    else if (currentmousex >= 95 && currentmousex <= 123)
  300.       col = 3;
  301.    else if (currentmousex >= 126 && currentmousex <= 154)
  302.       col = 4;
  303.    else if (currentmousex >= 157 && currentmousex <= 185)
  304.       col = 5;
  305.    else if (currentmousex >= 188 && currentmousex <= 216)
  306.       col = 6;
  307.    else if (currentmousex >= 219 && currentmousex <= 247)
  308.       col = 7;
  309.    else
  310.       return(1000);
  311.  
  312.  
  313.    if (currentmousey >= 14 && currentmousey <= 52)
  314.       row = 0;
  315.    else if (currentmousey >= 55 && currentmousey <= 93)
  316.       row = 1;
  317.    else if (currentmousey >= 96 && currentmousey <= 134)
  318.       row = 2;
  319.    else if (currentmousey >= 137 && currentmousey <= 175)
  320.       row = 3;
  321.    else
  322.       return(1000);
  323.  
  324.    cardnumber = col + row * 8;
  325.    return(cardnumber);
  326.    }
  327.  
  328.  
  329.  
  330. showcard(card)
  331.    int card;
  332.    {
  333.    extern UWORD mycardsposition[];
  334.    extern struct Image mycardsimage[];
  335.    extern struct RastPort *rp;
  336.    int left = 2;
  337.    int top = 13;
  338.    int toffset;
  339.    int loffset;
  340.    int cardimage;
  341.  
  342.    toffset = card / 8;
  343.    loffset = card %8 ;
  344.    top = top + 41 * toffset;
  345.    left = left + 31 * loffset;
  346.    card += 1;
  347.    cardimage = mycardsposition[card];
  348.  
  349.    if (cardimage != 1000)
  350.       placecard(rp, &mycardsimage[cardimage], left, top);
  351.    return(cardimage);
  352.    }
  353.  
  354. covercard(card)
  355.    int card;
  356.    {
  357.    extern struct Image mycardsimage[];
  358.    extern struct RastPort *rp;
  359.    int left = 2;
  360.    int top = 13;
  361.    int toffset;
  362.    int loffset;
  363.  
  364.    toffset = card / 8;
  365.    loffset = card %8 ;
  366.    top = top + 41 * toffset;
  367.    left = left + 31 * loffset;
  368.  
  369.    placecard(rp, &mycardsimage[0], left, top);
  370.    return(0);
  371.    }
  372.