关于c:C语言实现随机抽取纸牌

6次阅读

共计 1048 个字符,预计需要花费 3 分钟才能阅读完成。

利用数组实现从一副牌中随机抽取纸牌,供大家参考,具体内容如下
一、我的项目要求
本程序负责发一副规范纸牌,每张规范纸牌都有一种花色(梅花、方块、黑桃、红桃)和一个等级(2,3,4,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 对应元素虚实,如果为真,则抽取其余纸牌,如果为假,记录到数组元素当中,揭示咱们这张牌曾经记录过了站长博客。
三、我的项目代码
我的项目的具体代码展现如下:

include <stdio.h>

include <ctype.h>

include <stdbool.h>

include <time.h>

include <stdlib.h>

define num_rates ((int) (sizeof(value)/sizeof(value[0])))

define initial_balance 100.00

define num_suits 4

define 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[rank],suit_code[suit]);
}
}
printf(“n”);
return 0;
}

正文完
 0