Thread: Find Highest Card in Hand and swap with Other Hand

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    27

    Find Highest Card in Hand and swap with Other Hand

    I'm having some problems with creating a card game known as President. Part of the game requires me to grab the highest card from Hand4 and place it in Hand1 then grab the lowest card in Hand1 and place in Hand4.

    For my findHighest function, I want to grab the card from the hand with the highest value, then I wanna grab it's location.
    I will then grab the lowest card from a hand then grab it's location.
    I would then proceed to take the two locations and swap them in the two hands.

    I can't seem to get the code right. I'm having problems with getting the card's location so I could perform the swap. Any guidance? or easier method? Kindly help. I'll link the code below:


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECK_SIZE 52
    #define NFACES 13
    #define NSUITS 4
    #define NCARDS 13
    
    
    
    
    /*enum for card suits*/
    enum suits_e{
        SUIT_DIAMONDS,
        SUIT_CLUBS,
        SUIT_HEARTS,
        SUIT_SPADES
    };
    
    
    /*card structure definition*/
     typedef struct Card{
        int face; /* define face */
        int suit; /* define suit */
    }Card, *CardPtr;/* end structure card */
    
    
    
    
    /*function prototypes*/
    void createDeck(CardPtr deck);
    void printCard(CardPtr card);
    void shuffle(CardPtr deck);
    void shareCards(CardPtr deck, CardPtr Hand1, CardPtr Hand2, CardPtr Hand3, CardPtr Hand4);
    void findHighest(CardPtr Hand);
    
    
    int main(){
    
    
        Card deck[DECK_SIZE]; /*Creating deck array*/
    
    
        /*Creating hand arrays*/
        Card Hand1[NCARDS]; /*President Hand*/
        Card Hand2[NCARDS]; /*Prime Minister Hand*/
        Card Hand3[NCARDS]; /*Minister Hand*/
        Card Hand4[NCARDS]; /*Slave Hand*/
    
    
        /*contains the head pointer for each hand
        CardPtr hands[4];
        hands[0] = CardPtr Hand1;
        hands[1] = CardPtr Hand2;
        hands[2] = CardPtr Hand3;
        hands[3] = CardPtr Hand4;
        */
    
    
        int i,j;  /*counter*/
        int x = 1;
        int y=1;
    
    
        createDeck(deck);
    
    
        shuffle(deck);
    
    
    
    
        /*printf("Deck now: \n");
        for(i=0; i<DECK_SIZE; i++){
            printf("%d). ", y);
            printCard(&deck[i]);
            y++;
        }
        */
    
    
        int a=1;
        printf("PRESIDENT-Player 1 hand: \n");
        shareCards(deck, Hand1, Hand2, Hand3, Hand4);
         for(i=0; i<NCARDS; i++){
            printf("[%d]. ", a);
            printCard(&Hand1[i]);
            a++;
        }
        printf("\n");
        Card temp;
        findHighest(Hand1);
    
    
    
    
    
    
        int numCards = 0;   /*To keep track of how many cards are played in one turn*/
        printf("How many cards would you like to play: ");
        scanf("%d",&numCards);
    
    
        int num[numCards];  /*array to hold cards*/
        for(i=0;i<numCards;i++){
            printf("Enter the number index to the left for each card: ");
            scanf("%d", &num[i]);
        }
    
    
    
    
    return 0;
    }
    
    
    /*Creates deck*/
    void createDeck(CardPtr deck){
        int face, suit;
        int i=0;    /*counter*/
    
    
        for (suit=SUIT_DIAMONDS; suit<NSUITS; suit++){
    
    
                for (face=0; face<NFACES; face++){
                    deck[i].face= face;
                    deck[i].suit= suit;
                    i++;
                }
    
    
        }
    
    
    }
    
    
    /*Prints a Card*/
    void printCard(CardPtr card){
    
    
        /*Face array*/
        const char *face[NFACES] = {
            "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"
        };
    
    
        /*Suit array*/
        const char *suit[NSUITS] = {
            "Diamonds", "Clubs", "Hearts", "Spades"
        };
    
    
        printf("%s of %s\n", face[card->face], suit[card->suit]);
    }
    
    
    /*Shuffles deck*/
    void shuffle(CardPtr deck){
        int i, index;
        Card temp;
        srand(time(NULL)); // randomize seed with time
    
    
        for(i=DECK_SIZE-1; i>0; i--){
            index = rand()% (i + 1);
            temp = deck[index];
            deck[index]= deck[i];
            deck[i]= temp;
        }
    }
    
    
    /*Share cards*/
    void shareCards(CardPtr deck, CardPtr Hand1, CardPtr Hand2, CardPtr Hand3, CardPtr Hand4){
        int i=0;
        int j=0;
        int k=0;
        int l=0;  /*counters*/
        for (i=0;i<DECK_SIZE;i++){
            if (i<13){
                Hand1[i] = deck[i];
            }
                else if (i<26){
                    Hand2[j] = deck[i];
                    j++;
                }
    
    
                    else if (i<39){
                        Hand3[k] = deck[i];
                        k++;
                    }
    
    
                        else if (i<DECK_SIZE){
                            Hand4[l] = deck[i];
                            l++;
                        }
        }   /*end for*/
    }
    
    
    /*Find Card with highest value*/
    void findHighest(CardPtr Hand){
        int max=0;  /*holds highest number*/
        int i=0;    /*counter*/
        Card temp;
        CardPtr location;
    
    
        for (i=0;i<NCARDS;i++){
            if (max<Hand[i].face){
                max =  Hand[i].face;
                printf("Max is: %d\n");
                temp = Hand[i];
                location = Hand[i];
            }
        }
        for (i=0;i<NCARDS;i++){
            printf("%d\n",Hand[i].face);
        }
        printf("Hand1 highest value is: %d\n The location is %d,", temp , location);
    
    
    
    
    return;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need to put an ampersand before Hand[i] (i.e., take its address) in order to assign it to location.
    Also note that you have the following warnings (ignoring unused-variable warnings):
    Code:
    cb_president.c: In function ‘findHighest’:
    cb_president.c:174:13: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
                 printf("Max is: %d\n");
                 ^
    cb_president.c:182:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘Card’ [-Wformat=]
         printf("Hand1 highest value is: %d\n The location is %d,", temp , location);
         ^
    cb_president.c:182:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘CardPtr’ [-Wformat=]
    You should increase the warning level of your compiler if it isn't warning you about these problems.
    On gcc, add the flags -W -Wall -pedantic to get the maximum warning level. (-W is the same as -Wextra.)
    Last edited by algorism; 04-19-2016 at 01:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-09-2016, 02:37 PM
  2. how to sort a player's hand in seven card rummy
    By dex0391 in forum C Programming
    Replies: 4
    Last Post: 06-19-2010, 09:40 PM
  3. need a hand
    By eurikau in forum C Programming
    Replies: 0
    Last Post: 07-17-2007, 08:50 AM
  4. what are some other 'life skills' that can go hand in hand with programming
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-17-2003, 02:34 PM
  5. Hand Up
    By Rolf in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2001, 08:44 AM

Tags for this Thread