Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C programming - Making the 25 card game

  • 06-01-2019 12:24pm
    #1
    Registered Users, Registered Users 2 Posts: 68 ✭✭


    i need some help with a homework assignment where i haved to make the card game 25 in C with visual studio 2017. I can kind of deal cards to multiple people but am unable to make a trump card or to let players place down a card. I'm sure i could figure out how to see who wins the points once i have a way to let players place down cards, so please help. This is what i have so far

    /* Deals a random hand of cards */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define  TRUE 1
    #define  FALSE 0
    #define  BOOL int
    #define NUM_SUITS 4
    #define NUM_RANKS 13
    int DealCards(int i);
    int playCard(int i);
    int getTrumpCard();
    int main()
    {
        int i;
        int NumOfPlayers;
    
        printf("Please Enter the Number Of Players: ");
        scanf("%d", &NumOfPlayers);
    
        for (i = 1; i <= NumOfPlayers; i++)
        {
            DealCards(i);
            
        }
        for (i = 1; i <= NumOfPlayers; i++)
        {
            
            playCard(i);
        }
        
    }
    
    int getTrumpCard() {
    
    
        
    }
    int playCard(int i) {
        int cardPlayed;
        int pcard;
        printf("\nPlayer %d, choose a card to play: ",i);
        
        scanf("%d",&cardPlayed);
        if (cardPlayed == 1)
        {
            pcard = 0;
    
        }
        return pcard;
    
    
    
    } //player chooses a card to play
    int DealCards(int i) {
        BOOL in_hand[NUM_SUITS][NUM_RANKS] = { FALSE };
        int num_cards = 5, rank, suit;
        const char rank_code[] = { '2','3','4','5','6','7','8',
                                  '9','10','11','12','13','A' };
        const char suit_code[] = { 'C','D','H','S' };
        //srand(time(NULL));
        printf("\n\nPlayer %d's hand :  ", i);
        while (num_cards > 0) {
            suit = rand() % NUM_SUITS;
            rank = rand() % NUM_RANKS;
            if (!in_hand[suit][rank])
            {
                in_hand[suit][rank] = TRUE;
                num_cards--;
                printf(" %cof%c,", rank_code[rank], suit_code[suit]);
            }
            printf("  ");
        }
        return 0;
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    from a brief glance over the code, and i could be wrong as I am under the influence, but I believe you have implemented the in_hand array incorrectly.

    It should by declared globally as already_dealt, to ensure that two players don't get dealt the same card.

    to explain further (because I'm not sure of your level) in your DealCards function, for every player you initialise (and reinitialise) the in_hand 2d array to be FALSE at every position. This means that each time into DealCards the same hand could be dealt to every player because as far as this function is concerned no cards have been dealt yet (I'm assuming that only one deck is used for this game, but again under the influence).

    if instead you declare a global 2d array called, say, cards_dealt and initialise all as FALSE, then instead of "if (!in_hand[suit][rank])" you could use "if (!cards_dealt[suit][rank])" to ensure that each card is only dealt once.

    Actually, having rechecked the code in_hand is necessary to keep track of each players hands, but cards_dealt is necessary to ensure cards are only dealt once. In saying that the in_hand 2d array needs to be global too (or at least returned to the calling function in some way) in order to be visible to the game logic at large, it's not good enough to simply print the hand, you need to be able to access it as the game progresses.

    Another error is "const char rank_code[] = { '2','3','4','5','6','7','8','9','10','11','12','13','A' };"

    Within single quotes in c, you are only meant to include a single character, so '10', '11', '12 and '13' are outside the scope of that. It's been a good while since I coded c, but afair any characters represented in more than one character side by side is a string, so '10' should really be "10", but then that's definitely not a char...

    These are the types of occurrences that the c language, maddeningly, used to refer as undefined. They wouldn't raise an error at compile necessarily, and because most of the options conform to a single char, you might not even see what happens, but once you run your program enough, you start to see strange behaviour that would have you pulling your hair out!!!

    To be honest, your far better off mapping face values to ints from 0 to 12 (1 to ace) and similarly suits to ints 0 to 3 (take your pick on order).

    I know I haven't even come close to answering your question, but I think you need to address these issues first and then we can drill down to what you need answered...

    Another note: You pass i to DealCards as the player number, this is for the sole purpose of printing the player number to the screen. In fact DealCards should do no printing at all, it should merely deal a hand and return that hand to it's calling function.

    Sorry for pointing out all the flaws I see, I know it's disheartening. However, I want to help you get to your solution.


Advertisement