c - 如何有功能发出单独的牌手来分开人

标签 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;
}

最佳答案

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

关于c - 如何有功能发出单独的牌手来分开人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54024163/

相关文章:

java - 如何打乱数组

javascript - 使用 JavaScript 编程 Aces

java - 我所有的 @FXML 引用都是 null

c - 关于 round_up 宏的问题

c - c语言中如何比较两个矩阵的元素

visual-studio - 部署 .NET Compact Framework 应用程序的所有文件的 Visual Studio 问题

visual-studio - Visual Studio 的 Xsd2Code 类生成器加载项发生了什么变化?

c - 在arduino atmega2560中上传代码时出现问题

c - setsockopt 返回 errno=2

c# - VS 无法清理/构建/部署 - 损坏的 .NetFramework?