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