Home > OS >  Array, avoid pulling Duplicates, simple code, C
Array, avoid pulling Duplicates, simple code, C

Time:11-15

Its a card game, I draw 2 cards from the deck (the array) with the help of two functions.

Each element of the array represent one card with a symbol Spades, Hearts, Diamonds or Clubs.

These numbers in the array \5 \4 \3 \6, represent Clubs, Dimaond, Heatrs, spades (just if ur curious)

The problem!

When I draw a card two times I sometimes get Duplicates.. The same card twice.

How do I make sure the same card cant be drawn twice? How do I avoid getting duplicates??

This is how the code Looks like.

Some INFO about the array... The further in, in the array, the higher value the element has. I have shortened the array... for easy testing of a solution... later the array will be 52 elements.

array<string, 3> cards = { "Ess \5", "Ess \4", "Ess \3" };


//The function.
pair<string, int> draw_card()
{
    int random_index = rand() % 52;
    string card = cards[random_index];
    return { card, random_index };
}

int main()
{
// Seed, random.
 srand((unsigned int)time(NULL));



// Calling the function 2 times.
    pair<string, int> you_drew = draw_card();
    cout << "You drew: " << you_drew.first << endl;

    pair<string, int> comp_drew = draw_card();
    cout << "Computer drew: " << comp_drew.first << endl;



// Deciding the winner.
    int your_score{ 0 };
    int the_computers_score{ 0 };

    if (you_drew.second > comp_drew.second) {
        cout << "You Won!" << endl;
        your_score  ;
    }
    else if (you_drew.second < comp_drew.second) {
        cout << "You Lost!" << endl;
        the_computers_score  ;
    }

        return 0;
   }

Everything is working fine, EXCEPT sometimes I get duplicates... I want to make sure I can Not get that... Somehow when I draw a card the element in the array should not be able to be drawn.. I want to avoid getting duplicates. Please help me!

Shouldnt something like this work? its not but.. shouldnt it?

pair<string, int> comp_drew = draw_card();

        if (you_drew == comp_drew) {
            bool run8 = true;
            while (run8) {
                pair<string, int> comp_drew = draw_card();


                if (comp_drew != you_drew) {
                     cout << "Computer drew: " << comp_drew.first << endl;
                    run8 = false;
                }
            }

        }

Or maybe another solution.. Perhaps after calling the function one time i can delete the return index from the array?

CodePudding user response:

You can swap the drawn card to the end and only choose an index smaller than 51 the next time.

int array_len = 52; // global variables are not great, but it's easier here
pair<string, int> draw_card()
{
    int random_index = rand() % array_len;
    --array_len;
    string card = cards[random_index];
    std::swap(cards[random_index], cards[array_len]);
    
    return { card, random_index };
}

CodePudding user response:

There are many different ways to do this.

std::random_shuffle

#include <iostream>
#include <algorithm>

using namespace std;

class CardMachine {
    static unsigned const DECK_SIZE = 4;
    int cards[DECK_SIZE] = {1,2,3,4};
    int lastUsedCardIndex = -1;
public:
    int getCard() {
        if (lastUsedCardIndex == DECK_SIZE - 1) {
            shuffle();
            lastUsedCardIndex = -1;
        }
        return cards[  lastUsedCardIndex];
    }
    
    void shuffle() {
        random_shuffle(begin(cards), end(cards));
    }
};

int main()
{
    CardMachine cardMachine = CardMachine();
    
    cout << "unhsuffled - 1,2,3,4 without duplicates:" << endl;
    cout << cardMachine.getCard() << endl;
    cout << cardMachine.getCard() << endl;
    cout << cardMachine.getCard() << endl;
    cout << cardMachine.getCard() << endl;
    
    cout << "Shuffled. Random order, and still no duplicates:" << endl;
    cardMachine.shuffle(); // or call getCard which can shuffle
    cout << cardMachine.getCard() << endl;
    cout << cardMachine.getCard() << endl;
    cout << cardMachine.getCard() << endl;
    cout << cardMachine.getCard() << endl;

    return EXIT_SUCCESS;
}

Mark Card as Taken

#include <iostream>
#include <random>

using namespace std;

class CardMachine {
    static unsigned const DECK_SIZE = 4;
    int cards[DECK_SIZE] = {1,2,3,4};
    bool cardIsGone[DECK_SIZE] = {false, false, false, false};
public:
    int getCard() {
        while(true) {
            int const RANDOM_INDEX = rand()           
  • Related