怎么有功能散手分开人

时间:2019-01-03 14:21:31

标签: c visual-studio playing-cards

对于一项家庭作业,我需要创建一个程序让您玩爱尔兰纸牌游戏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;
}

4 个答案:

答案 0 :(得分:3)

问题是您在向每位玩家发牌之前调用了srand()函数。您使用time(NULL)作为参数,因此种子仅每秒改变一次,玩家获得具有相同种子的牌。 您只需为每个程序启动设置一次种子即可。

答案 1 :(得分:3)

您当前的方法是抽签更换卡片,然后检查是否已抽签。 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);
    }
}

答案 2 :(得分:1)

在赠送卡片之前只需初始化您的srand(time(NULL))

unsigned int length

答案 3 :(得分: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 << "\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);
}

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