-3

I have a code. It's a code for a card game. The problem is that cards should be distributed to game players, but they are not duplicated.

If it's possible, I need your help. Thank you.

#include<iostream>
#include<array>
#include<string>
#include<ctime>

using namespace std;

class Card {
public:
    static const size_t card1{ 4 };
    static const size_t card2{ 13 };

    Card(const array<string, card1>& card1n,
        const array<string, card2>& card2n)
        : arr1{ card1n }, arr2{ card2n } {}
    void disPlay() {
        for (int x = 0; x < arr2.size(); x++) {
            int i = rand() % 4;
            int j = rand() % 13;
            cout << arr1[i] + arr2[j] << " ";
        }
    }
private:
    array<string, card1> arr1;
    array<string, card2> arr2;
};
int main() {
    array<string, Card::card1> arr1{ "♥","◆","♠","♣" };
    array<string, Card::card2> arr2{ "A", "2", "3", "4", "5", "6", "7",     "8", "9", "10", "jack", "queen", "king" };
    Card p1(arr1, arr2);
    Card p2(arr1, arr2);
    srand(static_cast<unsigned int>(time(0)));

    cout << "player1's deck : ";
    p1.disPlay();

    cout << "\nplayer2's deck : ";
    p2.disPlay();
}

I predict players with overlapping cards

Norcino
  • 5,850
  • 6
  • 25
  • 42
  • 4
    Build an array of the 52 possible cards then shuffle that. For the latter use https://en.cppreference.com/w/cpp/algorithm/random_shuffle – Bathsheba May 23 '19 at 10:55
  • Both players have an entire deck of 52 cards? Or 13 cards? (From different packs, or the same pack?) – doctorlove May 23 '19 at 10:55
  • 1
    Possible duplicate of [How to deal a deck of cards in c++](https://stackoverflow.com/questions/49809896/how-to-deal-a-deck-of-cards-in-c) – doctorlove May 23 '19 at 11:05
  • Storing 52 cards as per @Bathsheba's suggestion seems the best method here, but another method is having a loop over the suit and another loop (inside the first) for the numbers. Then you pick randomly a number corresponding to a player to give that card to (if each player gets 26 cards). If each player gets fewer than 26 cards, shuffle both lists first. – Riddick May 23 '19 at 11:06
  • Please try to explain better what is the outcome of your code and how it differs from what you expect. Include expected and actual output in the question – 463035818_is_not_an_ai May 23 '19 at 11:19
  • I want each player to split 13 cards (without duplication) But my code is sharing duplicate cards. For example, p1 : heart1 p2 : heart 1 – Yoonsun Lee May 23 '19 at 11:51

2 Answers2

0

As far as I understand, you want the two players to have some cards that are the same. I ran your code and it did output this:

player1's deck : ♥Q ♠8 ♠10 ♥2 ◆K ♣3 ♥J ♣6 ◆5 ♥K ♥9 ♠10 ◆K 
player2's deck : ♠8 ◆7 ♥5 ◆2 ♠2 ♠2 ♥10 ◆3 ♥9 ♣Q ♣A ♥Q ◆5 

Notice that there is a ♥Q and ◆5 in both sets, but also that there are two ♠2 in player's 2 deck and two ◆K in player's 1 deck.

You could shuffle the array as @Bathsheba pointed out, and extract from the same point of reference for both players.

(Edit: std::move not even needed)

kaptsea
  • 113
  • 2
  • 11
0

You first create a deck of all 52 cards (all 4 suits, and all 13 ranks). This deck could be an array of 52 items, each item representing one card. Then you shuffle the deck with std::shuffle. Then you move first X cards from the deck to player's 1 hand. This hand could be another array of X items. Then you move next X cards to player's 2 hand.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • After creating a deck containing all cards You tell them to hand it out to the players in that deck. Thank you. I was looking for something else instead. That would be the best – Yoonsun Lee May 24 '19 at 13:17