对于二维数组例子中个别代码的疑问
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
#define TRUE 1
#define FALSE 0
int main()
{
int in_hand[NUM_SUITS][NUM_RANKS]={0};
int num_cards,rank,suit;
char rank_code[]={'2','3','4','5','6','7','8','9','10','J','Q','K','A'};
char suit_code[]={'c','d','h','s'};
srand((unsigned) time(NULL));
printf("Enter number of cards in hand:");
scanf("%d",&num_cards);
printf("Your hand:");
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(" %c%c",rank_code[rank],suit_code[suit]);
}
}
printf("\n");
}
suit=rand()%NUM_SUITS;
rank=rand()%NUM_RANKS;
if(!in_hand[suit][rank])
{
in_hand[suit][rank]=TRUE;
num_cards--;
printf(" %c%c",rank_code[rank],suit_code[suit]);
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
#define TRUE 1
#define FALSE 0
int main()
{
int in_hand[NUM_SUITS][NUM_RANKS]={0};
int num_cards,rank,suit;
char rank_code[]={'2','3','4','5','6','7','8','9','10','J','Q','K','A'};
char suit_code[]={'c','d','h','s'};
srand((unsigned) time(NULL));
printf("Enter number of cards in hand:");
scanf("%d",&num_cards);
printf("Your hand:");
while(num_cards>0)
{
suit=rand()%NUM_SUITS;//0~3的数字
rank=rand()%NUM_RANKS;//0~12的数字
//因为你初始化的都是0,这里判断是为了让它进去执行里面的语句
//也就是为了给你发牌
if(!in_hand[suit][rank])
{
in_hand[suit][rank]=TRUE;//变成1
num_cards--;
printf(" %c%c",rank_code[rank],suit_code[suit]);
}
}
printf("\n");
}