Thread: Snap Card Game

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

    Snap Card Game

    This is bunch of code i have for my Snap Game the problem that i face was it only comparing one deck only. May help me on how I can make it to comparing each card from both deck?

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdbool.h> // #include the std boolean library
    
    #define CARDS 52
    #define DRAW 104
    
    //The program will first open Deck 1 then Deck 2, after Deck 2 it will open Deck 1 back and repeat.
    bool Deck1 = true;
    bool Deck2 = false;
    
    //The program contains two Decks
    int deck1[CARDS];
    int deck2[CARDS];
    int UserSelection, a, b, c, d;
    char *suit[4] = { "Spades", "Hearts", "Clubs", "Diamonds" }; // Pattern of card
    
    void showcard(int card);
    void DisplayDeck();
    void MainMenu();
    
    int main()
    {
        MainMenu();
    
        return 0;
    }
    
    void DisplayDeck()
    {
        for (int a = 0; a < CARDS; a++) //initialize the first deck, to check whether the number has been taken
            deck1[a] = 0;
        for (int c = 0; c < CARDS; c++) //second deck
            deck2[c] = 0;
    
    
        while (1) // infinite loop
        {
            // when the deck one is open, deck two is close see the card in deck one
            //if (Deck1 == true && Deck2 == false)
            {
                srand((unsigned)time(NULL)); //first deck
                for (int a = 0; a < DRAW; a++)
                {
                    for (;;)//infinite loop
                    {
                        b = rand() % 52;
                        if (deck1[b] == 0)
                        {
                            deck1[b] = 1;
    
                            showcard(b);
                            break;
                        }
                    }
    
                }
    
            }
            // when the deck two is open, deck one is close see the card in deck one
            //else if (Deck2 == true && Deck1 == false)
            {
            srand((unsigned)time(NULL));
            for (int c = 0; c < DRAW; c++) //second deck
            {
                for (;;)
                {
                    d = rand() % 52;
                    if (deck2[d] == 0)
                    {
                        deck2[d] = 1;
    
                        showcard(d);
                        break;
                    }
                }
    
            }
    
        }
    
        }
        printf("Press any key to return back to the Game Menu. ");
        system("pause");
    
    }
    void MainMenu()
    {
        system("CLS");
        printf("\t-----------------------------------------\n");
        printf("\t         Welcome to Snap Game.         \n");
        printf("\t-----------------------------------------\n");
        printf("\t1. New Game                                         \n");
        printf("\t2. Record                              \n");
        printf("\t3. Quit                                                   \n");
        printf("\t-----------------------------------------\n");
        printf("\tSelection: ");
        scanf("%d", &UserSelection);
    
    
    
        switch (UserSelection)
        {
        case 1:
            DisplayDeck();
        case 2:
    
        case 3:
            exit(0);
        default:
            MainMenu();
        }
    
    
    }
    
    void showcard(int card)
    {
    
        char *suit[4] = { "Spades", "Hearts", "Clubs", "Diamonds" };
    
        if (Deck1 == true) // the program will run here when the deck one is open
        {
            printf("Deck 1 = ");
            switch (card % 13)
            {
            case 0:
                printf("%2s", "A");
                break;
            case 10:
                printf("%2s", "J");
                break;
            case 11:
                printf("%2s", "Q");
                break;
            case 12:
                printf("%2s", "K");
                break;
            default:
                printf("%2d", card % 13 + 1);
            }
            printf(" of %s\n", suit[card / 13]);
            // after finish display the card in deck one
            Deck1 = false; // close deck one
            Deck2 = true; // open deck two
        }
        else if (Deck2 == true) // the program will run here when deck one is open
        {
            printf("Deck 2 = ");
            switch (card % 13)
            {
            case 0:
                printf("%2s", "A");
                break;
            case 10:
                printf("%2s", "J");
                break;
            case 11:
                printf("%2s", "Q");
                break;
            case 12:
                printf("%2s", "K");
                break;
            default:
                printf("%2d", card % 13 + 1);
            }
            printf(" of %s\n", suit[card / 13]);
            // after displaying card in deck one
    
            //Check whether the card is the same
            if (deck1 == deck2)
            {
                printf("Snap\n\n");
            }
            else
            {
                /*for (int a = 0; a < CARDS; a++) //ensure the card is return to the array
                deck1[a] = 0;
                for (int c = 0; c < CARDS; c++) //same as above
                deck2[c] = 0;*/
    
                printf("No Snap\n\n");
            }
    
            Deck2 = false; // close deck two
            Deck1 = true; // open deck one
        }
        else
        {
            printf(" End Game ");
        }
    }
    Last edited by ressenjack; 03-02-2016 at 12:28 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    srand should only be called once in the program. Put is as the first statement in main.

    Switch cases need to end with a break (in general).

    if (deck1 == deck2)
    will never be true (they are separate arrays with different addresses)

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    8
    Quote Originally Posted by algorism View Post
    srand should only be called once in the program. Put is as the first statement in main.

    Switch cases need to end with a break (in general).

    if (deck1 == deck2)
    will never be true (they are separate arrays with different addresses)
    The srand which i should add in at and the if (deck1 == deck2) what i need to change?

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    8
    the problem i facing is the result only show the card from 1 deck only but not 2 deck

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (deck1 == deck2)
    This doesn't compare each element of both arrays.

    If they were two arrays of char, each with a \0, then you would write
    if ( strcmp(deck1,deck2) == 0 )

    But these are two arrays of int, so you need to hand-roll your own for loop to compare each deck1[i] and deck2[i] for equality.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ressenjack View Post
    The srand which i should add in at and the if (deck1 == deck2) what i need to change?
    The overall comparison result will depend on how each card in deck1 compares to deck2. So, you will need to write a function that contains a loop comparing each card.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    I don't think it correct to keep rand-ing until you get a card you have not gotten yet.
    When it's only 1 or 2 cards left it could take 100's of tries to get it.

    Set up cards in order and then shuffle them from spot1(rand()%52) to spot2(rand()%52) done 1000 times at the start seems more correct.

  8. #8
    Registered User
    Join Date
    Feb 2016
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    The overall comparison result will depend on how each card in deck1 compares to deck2. So, you will need to write a function that contains a loop comparing each card.
    But currently when i run the code it only show the result of comparing card in one of the deck

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Since rand() is such a ........ty generator, some permutations will never appear, but at least you don't have to do a swap 1000 times... Ben Pfaff: How can I shuffle the contents of an array?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ressenjack View Post
    But currently when i run the code it only show the result of comparing card in one of the deck
    Actually it doesn't even do that. You are just insisting on doing it wrong currently.

    A function is not even that hard to implement correctly. Just make the comparison for each card and stop if they ever different:
    Code:
    int deck_equal(int deck1[], int deck2[])
    {
       int flag = 1, i;
       for (i = 0; flag && i < CARDS; i++) 
          if (deck1[i] != deck2[i]) 
              flag = 0;
    
       return flag;
    }

  11. #11
    Registered User
    Join Date
    Feb 2016
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    Actually it doesn't even do that. You are just insisting on doing it wrong currently.

    A function is not even that hard to implement correctly. Just make the comparison for each card and stop if they ever different:
    Code:
    int deck_equal(int deck1[], int deck2[])
    {
       int flag = 1, i;
       for (i = 0; flag && i < CARDS; i++) 
          if (deck1[i] != deck2[i]) 
              flag = 0;
    
       return flag;
    }
    is this for the make a new function or?

  12. #12
    Registered User
    Join Date
    Feb 2016
    Posts
    8
    Quote Originally Posted by Salem View Post
    > if (deck1 == deck2)
    This doesn't compare each element of both arrays.

    If they were two arrays of char, each with a \0, then you would write
    if ( strcmp(deck1,deck2) == 0 )

    But these are two arrays of int, so you need to hand-roll your own for loop to compare each deck1[i] and deck2[i] for equality.


    what do you mean it?

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ressenjack View Post
    is this for the make a new function or?
    The entire point is that you need a custom function to get the comparison right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Snap Card Game
    By ressenjack in forum C Programming
    Replies: 3
    Last Post: 02-16-2016, 09:57 AM
  2. War card game
    By bigzcoder in forum C Programming
    Replies: 3
    Last Post: 04-19-2012, 09:38 PM
  3. Need help with Snap card game loop.
    By Kuroneko in forum C Programming
    Replies: 2
    Last Post: 04-03-2011, 09:54 AM
  4. Card game war
    By Dr Saucie in forum C Programming
    Replies: 3
    Last Post: 02-11-2010, 11:25 PM
  5. Card game help
    By aaroroge in forum Game Programming
    Replies: 9
    Last Post: 07-16-2005, 06:37 PM

Tags for this Thread