Search code examples
c++arraysplaying-cards

How do I compare printed array index to see if they're a pair


Draw 2 cards from the deck and check if they are a pair (having the same rank). Repeat this at least 1000 times and calculate the probability of drawing a pair from a deck of cards.

#include <iostream>
#include <cstdlib> 
#include <cstdio>
#include <string>

using namespace std;

int counter;


string facevalue[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight","Nine", "Ten", "Jack", "Queen", "King", "Ace" };


string suit [] = { "Diamonds", "Hearts", "Spades", "Clubs" };

string getcard() {
    string card;
    int cardvalue = rand() % 13;
    int cardsuit = rand() % 4;

    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];

    if(cardvalue = cardvalue){
        counter = counter + 1;
    }


    return card;
}

int main() {
    int numberofcards = 2;
    int times = 1000;


    for (int y =0; y < times; y++){

    cout<<" "<<endl;

    for (int i = 0; i < numberofcards; i++) {
        cout << "You drew a " << getcard() << endl;

     }
     }

     cout<<"counter: "<<counter<<endl;




} 

So here, cardvalue controls what gets printed out from the array. So if cardvalue is 1 then facevalue[cardvalue] will be facevalue[1], which will print out "Two."

So right now I am trying to determine how many times will cardvalue be the same when picking 2 cards randomly from the deck.

I did

    if(cardvalue = cardvalue){
        counter = counter + 1;
    }

The result I get is 926, meaning there are 926 times that the card value will be the same when drawing 2 cards out of a deck 1000 times. That doesn't seem right, would be much appreciated if anyone can correct my program or guide me through the process.

I tried (cardvalue == cardvalue)
but I get counter : 2000 instead.


Solution

  • If you work directly with index instead of string, you may have something like:

    int color_of_card(int i)
    {
        return i / 13;
    }
    
    int value_of_card(int i)
    {
        return i % 13;
    }
    
    std::string card_as_string(int i)
    {
        static const std::string facevalues[] = {
            "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
        };
        static const std::string suits[] = { "Diamonds", "Hearts", "Spades", "Clubs" };
    
        return facevalues[value_of_card(i)] + " of " + suits[color_of_card(i)];
    }
    
    int getcard() {
        return rand() % 52;
    }
    
    int main() {
        const int times = 1000;
    
        int counter = 0;
        for (int y = 0; y != times; y++)
        {
            auto card1 = getcard();
            auto card2 = getcard();
            while (card1 == card2) { card2 = getcard(); } // Ensure cards differ.
    
            if (value_of_card(card1) == value_of_card(card2)) {
                ++counter;
            }
        }
        std::cout << counter << std::endl;  // 58 or 59 normally
        // Once you took a card, there are only 3 card on same value
        // and there is 51 remaining cards.
        std::cout << 3 / 51.f << std::endl; // 0.0588235
    }
    

    Demo