this non-working piece of code came from my fingertips as a senior at Univ of Illinois. I had just gotten over the 'hey, this isn't scary after all - I KNOW C!' hump. Later, of course I learned I knew just the bare minimum about C. Anyway, I love to look at this old program for posterity's sake...

Pitch, incidentally is a wonderful trick-based card game, like Euchre and Spades, which I learned in college (sophomore year, playing with Troll, Tom, and the gang on the second floor of F? dorm) and since have rarely found a partner to play with.


#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "graph.h"

/* pitch standards:

trump=1 spades
trump=2 clubs
trump=3 diamonds
trump=4 hearts

variables:
cur_suit - PC - suit for current trick
deck[52] - PC - gets filled up with card numbers
hi_bid - PC - player number of highest bidder
i,j,k,l - OK - loop vars.
face[52][3] - DFW - gives A,K,J,... when drawing the hand from card #'s
hands[4][5] - PC - the 5 cards each of the 4 players hold
pot[4] - cards played by 4 players for current trick
suit[52] - DFW - either 1,2,3,4 - used to id suit of a card #
trump - PC - trump for current trick
winner - PC - player number of player who wins trick

*/

main ()


{
char face[52][3] = {"A","K","Q","J","10","9","8","7","6","5","4",
"3","2","A","K","Q","J","10","9","8","7","6","5","4","3","2",
"A","K","Q","J","10","9","8","7","6","5","4","3","2","A","K",
"Q","J","10","9","8","7","6","5","4","3","2"};

int suit[52] = {1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,
            3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4};

int hands[4][5];

int deck[52]={0};
int pot[4];
int i,j,k=0,l,winner,trump,cur_suit,card1,card2,play,card_num;
int hand[5];
int turn=0;
int hi_bid;

   _clearscreen( _GCLEARSCREEN );
   shuffle(deck,hands,suit);

      for (i=0;i<4;i++)
      drawhand(i,hands[i],suit,face);
   for (j=1;j<6;j++)  /* rounds 1 to 5 */
   {
       if (turn<=3)
       {
         if (k>3) k=k-4;  /* rollover from player 3 back to player 0 */
         do {
             _settextposition(10+i,30);
             printf("player %d play a card:",k+1);
             scanf("%d",&card_num);
            } while (card_num!=1 && card_num!=2 && card_num!=3 &&
                   card_num!=4 && card_num!=5); /* validate input */
             pot[k]=hands[k][card_num-1]; /* place play in trick array */

             if (k==winner || k==hi_bid) /*sets trump to you if reqd*/
                  {
                  cur_suit=suit[hands[k][card_num-1]];
                     if (j==1)  /*first trick sets trump*/
                             {
                             trump=suit[hands[k][card_num-1]];
                             _settextposition(13+i,30);
                             printf("current trump is %d",trump);
                             }
                  _settextposition(12+i,30);
                  printf("current suit is %d",cur_suit);
                  continue;
                  }
                  _settextposition(15,20);

               /* if you try to cheat! */
               if (!check(pot[k],hands[k],suit,cur_suit,trump))
               {
               k=k-1;
               turn=turn-1;
               }
            turn=turn+1;
            k=k+1;
            }

            /* determine the winner */
            winner=0;
            for (l=0;l<3;l++)
            if (compare(pot[winner],pot[l+1],suit,cur_suit,trump)
            ==0)
               winner=l+1;
            _settextposition(13+i,30);
            printf ("winning card is %d %d ",pot[winner],winner);
            k=winner;

   }
}


shuffle(int *deck,int hands[4][5],int *suit)
{
int i,j,k,l,temp,cardpick,cur_hi=0,prev_hi=0,cur_suit=0;
k=0;

for (i=0;i<52;i++)
deck[i]=0;

printf("press a key to shuffle\n");
while (!kbhit()) rand();
   for (i=1;i<52;i++)  /* mix cards up and put them in 'deck' */
   {
       cardpick= rand();
       cardpick= cardpick % 52;
       if (deck[cardpick]==0)
       deck[cardpick]=i;
       else i--;
   }

   for (i=0;i<4;i++)  /* assign cards to each of the 4 players by taking
                    first 20 cards off 'deck'   */
   {
      for (j=0;j<5;j++)
      {
      hands[i][j]=deck[k];
      k++;
      }
   }



   for (i=0;i<4;i++)    /*determine what suit player has most of */
   {
      for (j=1;j<5;j++)
      {
         cur_hi=0;
         for (k=0;k<5;k++)
            if (suit[hands[i][k]]==j) cur_hi=cur_hi+1;
         if (cur_hi>prev_hi) prev_hi=cur_hi,cur_suit=j;
      }


      for (k=0;k<5;k++)  /*put cards player has most of in order*/
         {
           for (l=0;l<5;l++)
           {
             if ((hands[i][k]<hands[i][l] &&
             suit[hands[i][l]]==cur_suit
             && suit[hands[i][k]]==cur_suit) ||
             (suit[hands[i][k]]==cur_suit &&
             suit[hands[i][l]]!=cur_suit))
             {
               temp=hands[i][k];
               hands[i][k]=hands[i][l];
               hands[i][l]=temp;
             }
           }
         }


   cur_suit=0;
   prev_hi=0;
   }



}


compare(int a,int b,int *suit,int cur_suit,int trump)
{
int out=3;
   if (suit[a]==suit[b])
   {
   if (a<b) /*lower numbered arrays are better cards!*/
   out=1;
   else
   out=0;
   }
   if (out==3)
   {
   if (suit[a]==trump) return (1);
   if (suit[a]==cur_suit) return (1);
   }
   return out;
   }

check(int play,int *hand,int *suit,int cur_suit,int trump)

/* use this only after first throw

play- the card number just played by the player 'k'
hand- the 1-d array of cards player 'k' is holding

*/



{
int i;
if (suit[play]==cur_suit || suit[play]==trump)
   {
   printf ("                           \n");
   return (1);
   }
      /* if you follow suit or play trump return TRUE */
   for (i=0;i<5;i++){
      if (suit[hand[i]]==cur_suit)
         {
         printf ("PULLED A BIRD! play a different card\n");
         return (0);
         }
   }
printf ("                           \n");
return (1);
}

drawhand(int player,int *hand,int *suit,char face[52][3],)
   {
    int x,y,t,i;
    switch(player) {
      case 0:
         x=26,y=20;
         break;
      case 1:
         x=1,y=10;
         break;
      case 2:
         x=27,y=2;
         break;
      case 3:
         x=55,y=10;
         break;
               }

   for (i=0;i<5;i++)
   {
      _settextposition(y,x);

       putchar(218);

       for (t=1;t<4;t++)
       {
          _settextposition(y,x+t);
          putchar(196);
       }

       putchar(191);

       for (t=1;t<5;t++)
       {
       _settextposition(y+t,x);
       putchar(179);
       _settextposition(y+t,x+4);
       putchar(179);
       }
       _settextposition(y+5,x);
       putchar(192);

       for (t=1;t<4;t++)
       {
       _settextposition(y+5,x+t);
       putchar(196);
       }

       putchar(217);
       _settextposition(y+1,x+1);
         if (suit[hand[i]]==1) putchar(6);
         if (suit[hand[i]]==2) putchar(5);
         if (suit[hand[i]]==3) putchar(4);
         if (suit[hand[i]]==4) putchar(3);
       puts(face[hand[i]]);
       _settextposition(y-1,x+2);
       putchar(i+49);
       x=x+5;


   }
}

-- MattWalsh - 11 Feb 2003

Topic revision: r1 - 11 Feb 2003 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback