开发者

how to fill, shuffle, deal the card game

I some how got it working but I still have a problem with the sorting and making pairs so that I can determine the winner.

Pair them(Pairs are cards with the same value.)e.g Ace of Hearts & Ace of Spades make a pair. I then count those pairs. hand with highest pairs wins.

This was what I was trying for the pairing but.. am still rellyy stuck with how I start the comparison to make the pairing.

This is the way I expect the results to be for every hand:

Hand 1: Six of Spades, is Black Seven of Diamonds, is Red Eight of Spades, is Black Ten of Hearts, is Red Queen of Spades, is Black Number of pairs: 0

Hand 2: Three of Spades, is Black Five of Diamonds, is Red Five of Clubs, is Black Nine of Diamonds, is Red Queen of Diamonds, is Red Number of pairs: 1 Highest pair is: Five

       #include <stdio.h>
       #include <stdlib.h>
        #include <time.h>

    struct card { 
      const char *face;
     const char *suit;
        const char *color;
         };

       typedef struct card Card;
       typedef unsigned char pairs;

        void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
        void shuffle( Card * const );
     void print( const Card * const );
     pairs findpairs(card *hand);  /* finds any pairs in a hand */

  int main()
   {
     int hand,cd,winner;
     card hands[5][5],handssorted[5][5];
      pairs numpairs[5],highest;
      Card deck[52];
       const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven",
                              "Eight", "Nine", "Ten","Jack", "Queen", "King"};
       const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
       const char *color[]= {"Black","Red"};
       srand( time( NULL ) );
       fillDeck( deck, face, suit, color );
       print( deck );
       printf("\n ----------------------------------------------------------           \n");
       shuffle( deck );
       print( deck );
       for(cd=0;cd<5;cd++)
    {

    }

   for(hand=0;hand<5;hand++)
    {
        /* sort the hands here */
        numpairs[hand]=findpairs(handssorted[hand]);
        printf("Hand %i:\n",hand+1);
        /* print the hands here */
        /* print the number and value of any pairs here */
   }

   /* determine the winner and print it */
     system("pause");
     return 0;

  }
        //-----------------------------------------------------------------------------         void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[],  
     const char * wColor[])
     {
       int i;
       for ( i = 0; i <= 51; i++ ) { 
          wDeck[i].face  = wFace[ i % 13 ];
          wDeck[i].suit  = wSuit[ i / 13 ];
          wDeck[i].color = wColor[i%2];
      //    if ()
             //           wDeck[i].suit = wSuit[ i / 13 ];
              }
         }
       //------------------------------------------------------------------
       void shuffle( Card * const wDeck )
            {
       int i, j;
       Card temp;
       for ( i = 0; i <= 51; i++ ) { 
          j = rand() % 52;
          temp = wDeck[ i ];
          wDeck[ i ] = wDeck[ j ];
          wDeck[ j ] = temp;
             }
        }
    //---------------------------------
     void print( const C开发者_如何学Goard * const wDeck )
     {
       int i;
       for ( i = 0; i <= 51; i++ ){
          printf( "\t%s\t of \t%-8s is \t%s \n \t", wDeck[i].face, 
                  wDeck[i].suit,wDeck[i].color,
                 ( i + 1 ) % 2 ? '\t' : '\n' );}
        }
      //--------------------------------------------------------------------------
          pairs findpairs(card *hand)
           {
            pairs numpairs=0;  
            for ( int i = 0; i <= 5; i++ ){
            if (hand[i].face == )

             }

      return numpairs;

        }


  • You have a call to print(deck) in there immediately after srand() which uses the uninitialised deck array.
  • numpairs, findpairs, and handssorted are undefined
  • you are printing the entire deck for each hand. Is that really what you intend?
  • onst is not valid (presumably you meant const)
  • fillDeck() is not filling in the .color member of each Card
  • your shuffle algorithm is suspect. See Fisher-Yates shuffle
  • in print(), you are using the %c format specifier with a const char * type


This isn't an answer to your question (I'm not actually sure what you're asking), but I will point out that your shuffling algorithm is not random.

First, using % to limit rand to a range of values is usually a bad idea. (See Q13.16 from the comp.lang.c FAQ.)

Second, it looks like you might be trying to implement the Fisher-Yates shuffling algorithm, but your implementation is wrong. It's easy to see that your approach of swapping with previously-swapped elements is problematic; consider shuffling a deck of three elements { 0, 1, 2 }:

First iteration     Second iteration
------------------------------------
{ 0, 1, 2 }         { 1, 0, 2 }
                    { 0, 1, 2 }
                    { 0, 2, 1 }
------------------------------------
{ 1, 0, 2 }         { 0, 1, 2 }
                    { 1, 0, 2 }
                    { 1, 2, 0 }
------------------------------------
{ 2, 1, 0 }         { 1, 2, 0 }
                    { 2, 1, 0 }
                    { 2, 0, 1 }

The first column represents the possible states of the deck after the first iteration (where you swap deck[0] with deck[rand() % 3]).

The second column represents the possible states of the deck after the second iteration (where you swap deck[1] with deck[rand() % 3]).

There are 9 equally probable states---but this is obviously wrong since there should be only 3! = 6 permutations. And indeed, several states are duplicated, which means that they have a higher probability of occurring. (Note that even if I had proceeded to the third iteration, you'd end up with 27 states for only 6 permutations, and since 27 is not divisible by 6, you'd clearly end up with some states occurring more than others.)


This is the way I expect the results to be for every hand

Then assign statically to the hands the desired cards. In your model every card has already unique number from 0 to 51. If deck and hand are both set of numbers then the operation is as simple as removing number from one set and adding it to another.

You might want to implement functions to work with such int sets, representing deck, hands, etc.

j = rand() % 52;

If you are serious into making a game, you might want to check how to generate random numbers from a range too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜