C语言 如何让功能将单独的牌手分开给分开的人



对于家庭作业,我需要创建一个程序,让你玩爱尔兰纸牌游戏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("nnPlayer %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)作为参数,因此种子每秒只变化一次,玩家得到的牌是相同的种子。每次程序启动只需设置一次种子。

您当前的方法是绘制带有替换的卡片,然后检查是否已绘制。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(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("nnPlayer %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%cn", rank_code[rank], suit_code[suit]);
        }
    }
    return 0;
    }

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

srandrand来自<cstdlib>标头。

在 C++11 及更高版本中,最好使用标头<random>函数。

此外,C++还提供了许多使用面向对象编程的可能性,并具有广泛的数据结构和算法库。我建议使用 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 << "nnPlayer " << player_num << "'s hand :n";
        for (auto card_count = 0; card_count < cards_to_deal; card_count++)
        {
            if (deck.cards_count() == 0)
            {
                std::cout << "nnNo 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);
}
//////////////////////////////////////////////////////////////////////////////////////////

相关内容

  • 没有找到相关文章

最新更新