如何有功能发牌分开人手

How to have have function give out seperate card hands to seperate people

对于家庭作业,我需要创建一个程序,让你玩爱尔兰纸牌游戏 25。我可以让我的代码向一个人伸出一只手,但如果我尝试让多个人,代码向其他人伸出相同的手。我如何给别人不同的、独特的手?

我试过使用循环,认为该函数会简单地重置数组,但它没有

/* Deals a random hand of cards */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TRUE 1
#define FALSE 0
#define BOOL int
#define NUM_SUITS 4
#define NUM_RANKS 13

int DealCards(int i);

int main()
{
    int i;
    int NumOfPlayers;

    printf("Please Enter the Number Of Players: ");
    scanf("%d", &NumOfPlayers);

    for (i = 1; i <= NumOfPlayers; i++)
    {
        DealCards(i);
    }
}

int DealCards(int i)
{
    BOOL in_hand[NUM_SUITS][NUM_RANKS] = { FALSE };
    int        num_cards   = 5, rank, suit;
    const char rank_code[] = { '2', '3',  '4',  '5',  '6',  '7', '8',
                               '9', '10', '11', '12', '13', 'A' };
    const char suit_code[] = { 'C', 'D', 'H', 'S' };
    srand(time(NULL));
    printf("\n\nPlayer %d's hand :\n", i);
    while (num_cards > 0)
    {
        suit = rand() % NUM_SUITS;
        rank = rand() % NUM_RANKS;
        if (!in_hand[suit][rank])
        {
            in_hand[suit][rank] = TRUE;
            num_cards--;
            printf(" %cof%c ", rank_code[rank], suit_code[suit]);
        }
        printf("\n");
    }
    return 0;
}

问题是你在给每个玩家卡片之前调用了srand()函数。您使用 time(NULL) 作为参数,因此种子仅每秒更改一次,并且玩家获得具有相同种子的卡片。 您只需为每个程序启动设置一次种子。

在给卡片之前初始化你的 srand(time(NULL))

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define  TRUE 1
    #define  FALSE 0
    #define  BOOL int
    #define NUM_SUITS 4
    #define NUM_RANKS 13
    int DealCards(int i);
    int main()
    {
    int i;
    int NumOfPlayers;

    printf("Please Enter the Number Of Players: ");
    scanf("%d",&NumOfPlayers);
   //here for example
     srand(time(NULL));
    for (i = 1; i <= NumOfPlayers; i++)
    {
        DealCards(i);
    }
    }
    int DealCards(int i) {
    BOOL in_hand[NUM_SUITS][NUM_RANKS] = { FALSE };
    int num_cards = 5, rank, suit;
    const char rank_code[] = { '2','3','4','5','6','7','8',
                              '9','10','11','12','13','A' };
    const char suit_code[] = { 'C','D','H','S' };
    printf("\n\nPlayer %d's hand :\n",i);
    while (num_cards > 0) {

        suit = rand() % NUM_SUITS;
        rank = rand() % NUM_RANKS;
        if (!in_hand[suit][rank])
        {  
            in_hand[suit][rank] = TRUE;
            num_cards--;
            printf(" %cof%c\n", rank_code[rank], suit_code[suit]);

        }
    }
    return 0;
    }

您目前的方法是抽取替换卡片,然后检查是否已抽出。 shuffle 套牌相当简单,也是更好的游戏模型。

你应该做的是定义一个编码特定卡片的类型,填充该类型的集合,使用每张卡片的值,洗牌,然后从洗牌的牌中分配卡片。

草图

#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iostream>

const std::vector<std::string> rank_code = { "2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace" };
const std::vector<std::string> suit_code = { "Clubs","Diamonds","Hearts","Spades" };

const int num_cards = 5;

struct Card
{
    Card(char s, char r) : suit(s), rank(r) {}
    char suit;
    char rank;
};

std::ostream & operator<< (std::ostream & os, const Card & c)
{
    return os << rank_code[c.rank] << " of " << suit_code[c.suit];
}

using Deck = std::vector<Card>;

Deck freshDeck()
{
    Deck result;
    for (std::size_t s = 0; s < suit_code.size(); ++s)
        for (std::size_t r = 0; r < rank_code.size(); ++r)
            result.emplace_back(s, r);
    return result;
}

void dealCards(int player, Deck & deck)
{
    std::string joiner;
    std::cout << std::endl << "Player " << player << "'s hand" << std::endl;
    for (int c = 0; c < num_cards; ++c)
    {
        std::cout << joiner << deck.back();
        deck.pop_back();
        joiner = ", ";
    }
    std::cout << std::endl;        
}

int main ()
{
    std::mt19937 gen{ std::random_device{}() };

    Deck deck = freshDeck();
    std::shuffle(deck.begin(), deck.end(), gen);

    std::cout << "Enter number of players" << std::endl;
    int num_players;
    std::cin >> num_players;

    for (int p = 1; p <= num_players; ++p)
    {
        dealCards(p, deck);
    }
}

问题是 srand 每次都是 运行 时都用相同的值初始化。 所以相同的随机值以相同的顺序生成。这就是为什么所有的手都一样。

srandrand 来自 <cstdlib> header.

在 C++11 及更高版本中,最好使用 <random> header.

中的函数

此外,C++ 还提供了许多使用面向 object 编程的可能性,并且具有广泛的数据结构和算法库。我建议使用 std::vectorstd::array 而不是普通数组。并且还使用 std::shuffle 来获得随机顺序的卡片。


#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iostream>
#include <exception>

//////////////////////////////////////////////////////////////////////////////////////////
class Rank
{
    std::string value;
    Rank(std::string value) : value(value) {}

public:
    static const std::vector< Rank >& get_all()
    {
        static std::vector< Rank > suits = { { "2" }, { "3" }, { "4" }, { "5" },  { "6" },
                                             { "7" }, { "8" }, { "9" }, { "10" }, { "J" },
                                             { "Q" }, { "K" }, { "A" } };

        return suits;
    }

    const std::string& to_string() const { return value; }
};

//////////////////////////////////////////////////////////////////////////////////////////
class Suit
{
    std::string value;
    Suit(std::string value) : value(value) {}

public:
    static const std::vector< Suit >& get_all()
    {
        static std::vector< Suit > ranks = {
            { "Clubs" }, { "Diamonds" }, { "Hearts" }, { "Spades" }
        };

        return ranks;
    }

    const std::string& to_string() const { return value; }
};

//////////////////////////////////////////////////////////////////////////////////////////
class Card
{
    Suit suit;
    Rank rank;

public:
    Card(const Suit& suit, const Rank& rank) : suit(suit), rank(rank) {}
    std::string to_string() const { return rank.to_string() + " of " + suit.to_string(); }
};

//////////////////////////////////////////////////////////////////////////////////////////
class Deck
{
    std::vector< Card > cards;

public:
    Deck()
    {
        const auto& ranks = Rank::get_all();
        const auto& suits = Suit::get_all();

        cards.reserve(ranks.size() * suits.size());

        for (const Suit& s : suits)
            for (const Rank& r : ranks)
                cards.emplace_back(s, r);
    }

    void shuffle()
    {
        static std::random_device rd;
        static std::mt19937       g(rd());

        std::shuffle(cards.begin(), cards.end(), g);
    }

    std::size_t cards_count() const { return cards.size(); }

    Card get_top_card()
    {
        if (cards_count() == 0)
            throw std::logic_error("No more cards!");

        const auto card = cards.back();
        cards.pop_back();

        return card;
    }
};

//////////////////////////////////////////////////////////////////////////////////////////
int get_player_count()
{
    std::cout << "Please enter the number of players:" << std::endl;

    int player_count;
    std::cin >> player_count;

    return player_count;
}

//////////////////////////////////////////////////////////////////////////////////////////
void deal_cards(int player_count, int cards_to_deal, Deck& deck)
{
    for (auto player_num = 1; player_num <= player_count; player_num++)
    {
        std::cout << "\n\nPlayer " << player_num << "'s hand :\n";
        for (auto card_count = 0; card_count < cards_to_deal; card_count++)
        {
            if (deck.cards_count() == 0)
            {
                std::cout << "\n\nNo more cards to deal!" << std::endl;
                return;
            }

            std::cout << deck.get_top_card().to_string() << ", ";
        }
    }

    std::cout << std::endl;
}

//////////////////////////////////////////////////////////////////////////////////////////
int main()
{
    Deck deck;
    deck.shuffle();

    const auto player_count  = get_player_count();
    const auto cards_to_deal = 5;

    deal_cards(player_count, cards_to_deal, deck);
}

//////////////////////////////////////////////////////////////////////////////////////////