I having a problem which i cannot compare the deck 1 and deck 2 anyone can help me?

#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 ");
    }
}

It would help to know why you're using 2 decks.

Part of your problem might be linked to duplicating your code in several places. If you pass the address of the deck to the displaydeck function you only need code to deal with the deck that's passed. Also this means that you can pass the address of the card to showcard, instead of the index, and it won't have to worry about which deck it's from.

This puts the logic for deciding which deck you're acting upon in the main menu function.

Also you can eliminate one bool variable. if deck1 is false you must be reading upon deck2.

Edited 9 Months Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.