I need some ideas please>>

This is a discussion on I need some ideas please>> within the C Programming forums, part of the General Programming Boards category; hi can someone please help me out with this card game excercise in C. I have attempted most of the ...

  1. #1
    Unregistered
    Guest

    I need some ideas please>>

    hi can someone please help me out with this card game excercise in C. I have attempted most of the code, there is no output as yet because i havent completed the code. So i just need someone to fill in the missing pieces of code whereever there is a comment, and give me some hints and tips.

    kind regards
    Atif.





    code:--------------------------------------------------------------------------------
    /*****************************************/

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

    /* handy typedefs */
    typedef unsigned char card;
    typedef unsigned char pairs;

    /* arrays for the names of things */
    static char *suits[] = {"Hearts","Diamonds","Clubs","Spades"};
    static char *face[] = {"Ace","Two","Three","Four","Five","Six",\
    "Seven","Eight","Nine","Ten","Jack",\
    "Queen","King"};
    static char *colour[]= {"Black","Red"};


    /* function prototypes */
    void printcard(card c,int showcolour); /* Displays the value of a card*/

    void printdeck(card deck[52]); /* prints an entire deck of cards*/

    void filldeck(card deck[52]); /* Populates a deck of cards */

    void shuffle(card deck[52]); /* Randomizes the order of cards */

    int compareface(const void* c1,const void *c2);
    /* compares the face value of 2 cards, suitable to pass to qsort as the fourth argument */

    pairs findpairs(card *hand); /* finds any pairs in a hand */

    int main()
    {

    card deck[52],*deckp;
    card hands[5][5],handssorted[5][5];
    pairs numpairs[5],highest;
    int hand,cd,winner;

    srand(time(NULL)); /* seed the random number generator */


    exit(0);



    /*populate and shuffle the deck */
    filldeck(deck);
    printdeck(deck);
    shuffle(deck);
    printdeck(deck);

    for(cd=0;cd<5;cd++)
    {
    for(hand=0;hand<5;hand++)
    {

    int cur_card = 0;
    handssorted[cd][hand] = deck[cur_card];
    cur_card++;
    }
    }


    for(hand=0;hand<5;hand++)
    {
    qsort(handssorted[hand], 5, sizeof(card), compareface);

    numpairs[hand]=findpairs(handssorted[hand]);

    printf("Hand %i:",hand+1);

    /* print the hands here */

    }

    /* determine the winner and print it */
    return 0;
    }
    }
    pairs findpairs(card *hand)
    {
    pairs numpairs=0;

    /* find the pairs here */
    return numpairs;

    }

    void filldeck(card deck[52])
    {
    int i;
    for(i = 0; i < 52; i++)
    {

    deck[i] = suits[i/13];
    deck[i] = face[i % 13];
    deck[i] = colour[i /26];

    }
    return;
    }

    void printdeck(card deck[52])
    {


    for(i=0; i < 52; i++)
    {
    printf(deck[i]);
    printf(" ");
    }

    return;
    }


    void printcard(card c, int showcolour)
    {
    /* print the value of the card here */
    return;

    }


    void shuffle(card deck[52])
    {

    int i,j, rnd;
    card c;

    for(i=0;i<52;i++)
    {
    for(j=0; j< 52; j++)
    {
    rnd=rand()%52;
    c = deck[i];
    deck[i]=deck[j];
    deck[j] = c;

    }
    }

    return;
    }

    int compareface(const void* c1, const void *c2)
    {
    card cd1,cd2;

    cd1=*((card*) c1);
    cd2=*((card*) c2);

    cd1= (cd1&0x3c)>>2;
    cd2= (cd2&0x3c)>>2;

    if(cd1>cd2)
    return 1;
    if(cd1==cd2)
    return 0;

    return -1;
    }

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Be more specific as to where exactly in the code you need help.

  3. #3
    Unregistered
    Guest
    Hi, thanks for replying.. I need help with the following.

    - printing the hands
    - determining the winner and printing it
    - void printcard(card c, int showcolour){
    - find the pairs
    - determining the winner and printing it

    This game is similar to poker, whereas in this game 5 hands of 5 cards are dealt. There is no swapping of cards and the winner is determined by who has the highest pair. A pair of cards are cards with the same value, e.g, the ACE of hearts and the ACE of spades are a pair.

    The following should be the output generated:
    kind regards,
    Atif

    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

    Hand 3:
    Three of Clubs , is Black
    Seven of Hearts , is Red
    Nine of Clubs , is Black
    Jack of Clubs , is Black
    Jack of Spades , is Black
    Number of pairs: 1
    Highest pair is: Jack

    Hand 4:
    Two of Diamonds, is Red
    Four of Spades , is Black
    Seven of Clubs , is Black
    Eight of Diamonds, is Red
    King of Hearts , is Red
    Number of pairs: 0

    Hand 5:
    Ace of Hearts , is Red
    Six of Clubs , is Black
    Seven of Spades , is Black
    Nine of Spades , is Black
    Nine of Hearts , is Red
    Number of pairs: 1
    Highest pair is: Nine

    Winner is hand 3 with a pair of Jacks

    Figure 1: Example output of a game with a winner


    Hand 1:
    Three of Clubs , is Black
    Five of Hearts , is Red
    Seven of Spades , is Black
    Queen of Clubs , is Black
    King of Spades , is Black
    Number of pairs: 0

    Hand 2:
    Three of Diamonds, is Red
    Five of Diamonds, is Red
    Seven of Hearts , is Red
    Queen of Diamonds, is Red
    King of Hearts , is Red
    Number of pairs: 0

    Hand 3:
    Ace of Hearts , is Red
    Two of Clubs , is Black
    Seven of Diamonds, is Red
    Nine of Spades , is Black
    Jack of Clubs , is Black
    Number of pairs: 0

    Hand 4:
    Ace of Clubs , is Black
    Five of Clubs , is Black
    Nine of Diamonds, is Red
    Jack of Diamonds, is Red
    King of Clubs , is Black
    Number of pairs: 0

    Hand 5:
    Three of Hearts , is Red
    Four of Hearts , is Red
    Ten of Hearts , is Red
    Queen of Hearts , is Red
    King of Diamonds, is Red
    Number of pairs: 0

    Drawn game

    Figure 2: Example output of a game without a winner

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Give each card a number. Zero through 51. Decide what order your suits are. Now then:
    Code:
    char *getCard( int c )
    {
        char *card[] =
        {
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten",
            "Jack",
           "Queen",
            "King",
            "Ace",
        };
        return card[c%13];
    }
    
    char *getSuit( int c )
    {
        return c<13? "Diamond" : c<26? "Club" : c<39? "Heart" : "Spade";
    }
    There you go. Now you know how to get the suit and the card itself. Using the same formulae, you could get yourself a numeric value of each card so you know how to check for pairs. Given the above you should also know how to print the card:

    printf("%s of %ss", getCard( x ), getSuit( x ) );

    You can do the rest.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 02:53 AM
  2. any beginner ideas?
    By gunder in forum C Programming
    Replies: 6
    Last Post: 11-25-2005, 09:09 AM
  3. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  4. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 07:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21