home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 605b.lha / Spades_v2.10 / Source / Input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-06  |  11.9 KB  |  423 lines

  1. /*
  2.    FILE: Input.c
  3.    PURPOSE: Routines for getting user input.
  4.    AUTHOR: Gregory M. Stelmack
  5. */
  6.  
  7. #ifndef GLOBALS_H
  8. #include "Globals.h"
  9. #endif
  10.  
  11. extern struct Menu *SpadesMenu;
  12.  
  13. struct EasyStruct    AboutES =
  14. {
  15.    sizeof(struct EasyStruct),
  16.    0,
  17.    "About Spades 2.10",
  18.    "Spades 2.10 by:\nGregory M. Stelmack\n8723 Del Rey Ct. #11-A\nTampa, FL 33617",
  19.    "OK"
  20. };
  21.  
  22. /**********************************************************
  23. * Function: ReadMouse                                     *
  24. * Parameters: none                                        *
  25. * Return Values: none                                     *
  26. * Purpose: Wait for mouse input, and update the mouse     *
  27. *          variables accordingly.                         *
  28. **********************************************************/
  29. void ReadMouse()
  30. {
  31.    ULONG class,menunum,itemnum,subnum;
  32.    USHORT code,selection,flags;
  33.    BOOL gotmouse=FALSE;
  34.    BOOL QuitFlag=FALSE,NewFlag=FALSE;
  35.    struct IntuiMessage  *Message;
  36.    
  37.    /* Loop until we have a mouse message */
  38.   
  39.    while(!gotmouse)
  40.    {
  41.       /* Wait for Intuition Message */
  42.       
  43.       Wait(1L<<Wdw->UserPort->mp_SigBit);    
  44.         while(Message=(struct IntuiMessage *) GetMsg(Wdw->UserPort))
  45.         {
  46.          /* Interpret Message */
  47.     
  48.          class = Message->Class;
  49.          code = Message->Code;
  50.          ReplyMsg((struct Message *)Message);
  51.  
  52.          switch(class)
  53.          {
  54.             case MENUPICK:
  55.                selection = code;
  56.                while (selection != MENUNULL)
  57.                {
  58.                   menunum = MENUNUM(selection);
  59.                   itemnum = ITEMNUM(selection);
  60.                   subnum  = SUBNUM(selection);
  61.                   flags = ((struct MenuItem *)ItemAddress(SpadesMenu,
  62.                            (LONG)selection))->Flags;
  63.                   switch(menunum)
  64.                   {
  65.                      case 0:     /* Project menu */
  66.                         switch(itemnum)
  67.                         {
  68.                            case 0:     /* New Game */
  69.                               NewFlag = TRUE;
  70.                               break;
  71.                            case 1:     /* Save Hand */
  72.                               if (flags&CHECKED)
  73.                                     SaveHand = TRUE;
  74.                               else
  75.                                     SaveHand = FALSE;
  76.                               break;
  77.                            case 2:     /* Print Hand */
  78.                               break;
  79.                            case 3:     /* About */
  80.                               (void)EasyRequest(Wdw,&AboutES,NULL,NULL);
  81.                               break;
  82.                            case 4:     /* Quit */
  83.                               QuitFlag = TRUE;
  84.                               break;
  85.                            default:
  86.                               break;
  87.                         }
  88.                         break;
  89.                      case 1:     /* Game menu */
  90.                         switch(itemnum)
  91.                         {
  92.                            case 0:     /* Bags */
  93.                               if (flags&CHECKED)
  94.                                     BagsRule = TRUE;
  95.                               else
  96.                                     BagsRule = FALSE;
  97.                               break;
  98.                            case 1:     /* Nil */
  99.                               if (flags&CHECKED)
  100.                                     NilRule = TRUE;
  101.                               else
  102.                                     NilRule = FALSE;
  103.                               break;
  104.                            case 2:     /* Suggest */
  105.                               Button = MRIGHT;
  106.                               gotmouse = TRUE;
  107.                               break;
  108.                            default:
  109.                               break;
  110.                         }
  111.                         break;
  112.                      default:
  113.                         break;
  114.                   }
  115.                   selection = ((struct MenuItem *)
  116.                               ItemAddress(SpadesMenu,(LONG)selection))
  117.                               ->NextSelect;
  118.                }
  119.                break;
  120.                
  121.                 case MOUSEBUTTONS:
  122.                switch(code)
  123.                {
  124.                   case SELECTUP:
  125.                      Button=MLEFT;
  126.                      gotmouse=TRUE;
  127.                      Mx=((SHORT) Message->MouseX);
  128.                      My=((SHORT) Message->MouseY);
  129.                      break;
  130.                   default:
  131.                      break;
  132.                }
  133.     
  134.             default:
  135.                break;
  136.                 
  137.          }  /* end switch */
  138.       }  /* end while message to process */
  139.         
  140.       if (QuitFlag == TRUE)
  141.       {
  142.          WrapUp();
  143.       }
  144.       else if (NewFlag == TRUE)
  145.       {
  146.          Spades();
  147.       }
  148.    }  /* end while not gotmouse */
  149.  
  150. /**********************************************************
  151. * Function: FinishRoutine                                 *
  152. * Parameters: none                                        *
  153. * Return Values: none                                     *
  154. * Purpose: Display final score, ask to play again.        *
  155. **********************************************************/
  156. void FinishRoutine()
  157. {
  158.    BOOL haveinput;
  159.  
  160.    SetRast(RP,BLUP);
  161.    SetAPen(RP,WHTP);
  162.    SetBPen(RP,BLUP);
  163.  
  164.    Move(RP,112,56);
  165.    Text(RP,"FINAL SCORE:",12);
  166.    Move(RP,80,72);
  167.    Text(RP,"YOU:",4);
  168.    Move(RP,184,72);
  169.    Text(RP,"ME:",3);
  170.    SetAPen(RP,YELP);
  171.    itoa(PlayerScore,String);
  172.    Move(RP,116,72);
  173.    Text(RP,String,strlen(String));
  174.    itoa(CompScore,String);
  175.    Move(RP,212,72);
  176.    Text(RP,String,strlen(String));
  177.    
  178.    Move(RP,112,96);
  179.    Text(RP,"PLAY AGAIN ?",12);
  180.    SetAPen(RP,BLKP);
  181.    SetBPen(RP,WHTP);
  182.    Move(RP,112,112);
  183.    Text(RP,"YES",3);
  184.    Move(RP,188,112);
  185.    Text(RP,"NO",2);
  186.    
  187.    haveinput=FALSE;
  188.    while (!haveinput)
  189.    {
  190.       ReadMouse();
  191.       if ((Mx>111)&&(Mx<136)&&(My>105)&&(My<114))
  192.       {
  193.          haveinput=TRUE;
  194.          AllDone=FALSE;
  195.       }
  196.       if ((Mx>187)&&(Mx<204)&&(My>105)&&(My<114))
  197.       {
  198.          haveinput=TRUE;
  199.          AllDone=TRUE;
  200.       }
  201.    }
  202. }
  203.  
  204. /**********************************************************
  205. * Function: GetPlayerCard                                 *
  206. * Parameters: none                                        *
  207. * Return Values: card picked to play                      *
  208. * Purpose: Allow player to pick card to play.             *
  209. **********************************************************/
  210. int GetPlayerCard()
  211. {
  212.    int i,x,card;
  213.   
  214.    /* Let player know that it's his/her turn */
  215.   
  216.    SetBPen(RP,BLUP);
  217.    SetAPen(RP,YELP);
  218.    Move(RP,200,150);
  219.    Text(RP,"PLAY A CARD",11);
  220.   
  221.    /* Loop until we get a good card */
  222.   
  223.    FOREVER
  224.    {
  225.       ReadMouse();                        /* Wait for mouse click */ 
  226.       if (Button==MRIGHT) SuggestCard();  /* Player wants a suggestion */
  227.       if (Button==MLEFT)                  /* Did player pick a card ? */
  228.       {
  229.          for (i=12 ; i>=0 ; i--)          /* Check from right to left */
  230.          {
  231.             x=(i*10)+21;                  /* Set left corner for card */
  232.         
  233.             /* See if clicked inside this card and if card is
  234.                still unplayed */
  235.         
  236.             if ((My<187)&&(My>144)&&(Mx<(x+42))&&(Mx>=x)&&(Hand[0][i]))
  237.             {
  238.                if (ValidCard(i))  /* Was this a playable card ? */
  239.                {
  240.                   card=Hand[0][i]-1;
  241.                   Hand[0][i]=0;   /* Mark card as played */
  242.                   
  243.                   /* Check to see if Spades were broken */
  244.                   
  245.                   if ((card/13)==SPADES) SpadePlayed=TRUE;
  246.             
  247.                   /* Erase suggestion '*' */
  248.             
  249.                   SetAPen(RP,BLUP);
  250.                   SetOPen(RP,BLUP);
  251.                   RectFill(RP,21,136,170,144);
  252.             
  253.                   DrawCard(CardX[0],CardY[0],card); /* Draw played card */
  254.             
  255.                   /* Erase prompt message */
  256.             
  257.                   SetBPen(RP,BLUP);
  258.                   Move(RP,200,150);
  259.                   Text(RP,"           ",11);
  260.             
  261.                   /* Send back played card */
  262.             
  263.                   return(card);
  264.                }
  265.                else     /* chosen card was not valid, need a new card */
  266.                      i=-1;
  267.             }
  268.          }
  269.       }
  270.    }
  271. }
  272.  
  273. /**********************************************************
  274. * Function: ValidCard                                     *
  275. * Parameters: card -- card in player's hand to check      *
  276. * Return Values: was card valid or not?                   *
  277. * Purpose: To determine if the card chosen by the player  *
  278. *   was valid or not.                                     *
  279. **********************************************************/
  280. BOOL ValidCard(card)
  281. int card;
  282. {
  283.    int i,suit,leadsuit;
  284.   
  285.    SuitNumber[DIAMONDS]=0;
  286.    SuitNumber[CLUBS]   =0;
  287.    SuitNumber[HEARTS]  =0;
  288.    SuitNumber[SPADES]  =0;
  289.   
  290.    /* Count number of cards player has in each suit */
  291.   
  292.    for (i=0 ; i<13 ; i++)
  293.    {
  294.       if (Hand[0][i]) SuitNumber[(Hand[0][i]-1)/13]++;
  295.    }
  296.   
  297.    suit=(Hand[0][card]-1)/13; /* Find suit of played card */
  298.   
  299.    if (!TrickLead)            /* Player is leading */
  300.    {
  301.       /* If he didn't lead a spade, it was a good play */
  302.       if (suit!=SPADES) return(TRUE);
  303.     
  304.       /* If he only has spades, he has no choice */ 
  305.       if ((!SuitNumber[0])&&(!SuitNumber[1])&&(!SuitNumber[2]))
  306.             return(TRUE);
  307.     
  308.       /* If spades have been played, he can lead anything */
  309.       if (SpadePlayed) return(TRUE);
  310.     
  311.       /* Must have lead a spade when it was illegal to */
  312.       return(FALSE);
  313.    }
  314.   
  315.    /* Player doesn't lead */
  316.   
  317.    leadsuit=Card[TrickLead]/13;       /* Find suit that was lead */
  318.   
  319.    /* If he played the suit that was lead, he is OK */
  320.    if (suit==leadsuit) return(TRUE);
  321.   
  322.    /* If he didn't have any, he's OK */
  323.    if (!SuitNumber[leadsuit]) return(TRUE);
  324.   
  325.    /* Must have had some but didn't play them */
  326.    return(FALSE);
  327. }
  328.  
  329. /**********************************************************
  330. * Function: GetPlayerBid                                  *
  331. * Parameters: none                                        *
  332. * Return Values: bid -- number of tricks bid              *
  333. * Purpose: Get the human player's bid. Could use gadgets, *
  334. *          but this is easier to program.                 *
  335. **********************************************************/
  336. int GetPlayerBid()
  337. {
  338.    int bid=1,length;
  339.    BOOL havebid=FALSE;
  340.  
  341.    ShowHand();
  342.  
  343.    /* Draw input box */
  344.   
  345.    SetAPen(RP,YELP);
  346.    SetBPen(RP,BLKP);
  347.    Move(RP,258,142);
  348.    Text(RP,"BID",3);
  349.    Move(RP,250,150);
  350.    Text(RP,"  +  ",5);
  351.    Move(RP,250,158);
  352.    Text(RP,"   OK",5);
  353.    Move(RP,250,166);
  354.    Text(RP,"  -  ",5);
  355.   
  356.    /* Loop until OK is clicked */
  357.   
  358.    while(!havebid)
  359.    { 
  360.       /* Draw Current Bid */
  361.     
  362.       SetAPen(RP,GRNP);
  363.       SetBPen(RP,BLKP);
  364.       Move(RP,250,158);
  365.       Text(RP,"  ",2);
  366.       itoa(bid,String);
  367.       length=strlen(String);
  368.       Move(RP,(258-(4*length)),158);
  369.       Text(RP,String,length);
  370.     
  371.       /* Wait for Mouse input */
  372.     
  373.       ReadMouse();
  374.       if (Button==MLEFT)   /* Left Button Pressed */
  375.       {
  376.          /* plus sign clicked */
  377.          if ((Mx>265)&&(Mx<274)&&(My>143)&&(My<152)) bid++;
  378.             
  379.          /* OK clicked */
  380.          if ((Mx>273)&&(Mx<290)&&(My>151)&&(My<160)) havebid=TRUE;
  381.          
  382.          /* minus sign clicked */
  383.          if ((Mx>265)&&(Mx<274)&&(My>159)&&(My<168)) bid--;
  384.       }
  385.       if (Button==MRIGHT)  /* Right Button Pressed */
  386.       {
  387.          bid=CalcBid(0);   /* Suggest a Bid */
  388.       }
  389.     
  390.       /* Make sure bid is valid */
  391.     
  392.       if (NilRule)
  393.       {
  394.          if (bid<0) bid=0;
  395.          if (bid>13) bid=13;
  396.       }
  397.       else
  398.       {
  399.          if (bid<1) bid=1;
  400.          if (bid>12) bid=12;
  401.       }
  402.    }
  403.   
  404.    /* Erase Input Box */
  405.   
  406.    SetAPen(RP,BLUP);
  407.    SetOPen(RP,BLUP);
  408.    RectFill(RP,250,135,291,168);
  409.   
  410.    /* Display the bid */
  411.   
  412.    SetAPen(RP,YELP);
  413.    SetBPen(RP,BLUP);
  414.    itoa(bid,String);
  415.    Move(RP,MsgX[0],MsgY[0]);
  416.    Text(RP,String,strlen(String));
  417.   
  418.    /* Send the bid back */
  419.   
  420.    return(bid);
  421. }
  422.