利用数组实现从一副牌中随机抽取纸牌,供大家参考,具体内容如下
一、项目要求
本程序负责发一副标准纸牌,每张标准纸牌都有一种花色(梅花、方块、黑桃、红桃)和一个等级(2whatsapp网页版,3,4telegram中文版,5,6…K,A)。程序需要用户指明手机有几张牌,格式为:
Enter number of cards in hand:____
your hand: _
二、原理
1.使用库函数
time函数返回当前时间,用一个数表示,srand函数初始化C语言的随机数生成器。通过把time函数返回值传递给srand可以避免程序每次运行发同样的牌。rand函数产生随机数,通过%缩放。
2.利用二维数组记录
程序采用in_hand二维数组对已经选择的牌进行记录,4行表示每种花色,13列表示每种等级。
程序开始时,数组元素都为false,每随机抽取一张纸牌时,检查in_hand对应元素真假,如果为真,则抽取其他纸牌whatsapp网页版登录,如果为假,记录到数组元素当中,提醒我们这张牌已经记录过了站长博客。
三、项目代码
项目的具体代码展示如下:
include include include include include define num_rates ((int) (sizeof(value)/sizeof(value)))define initial_balance 100.00define num_suits 4define num_ranks 13
int main(){
bool in_handnum_suits = {false};
int num_cards,rank,suit;
const char rank_code = { '2','3','4','5','6','7','8','9',
't','j','q','k','a'};
const char suit_code = { 'c','d','h','s'};
printf("enter numbern");
scanf("%d",&num_cards);
printf("your handsn");
while(num_cards>0){
suit = rand()%num_suits;
rank = rand()%num_ranks;
if(!in_handsuit){
in_handsuit = true;
num_cards--;
printf(" %c%c",rank_code,suit_code);
printf("n");
return 0;
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。