c语言模拟扑克牌自动发牌
时间: 2023-07-09 21:26:03 浏览: 136
好的,下面是一个简单的 C 语言程序,可以模拟扑克牌自动发牌的过程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
#define NUM_CARDS 52
int main()
{
// 定义扑克牌的花色和点数
const char* suits[NUM_SUITS] = {"Spades", "Hearts", "Diamonds", "Clubs"};
const char* ranks[NUM_RANKS] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
// 定义一个扑克牌的数组
int deck[NUM_CARDS];
// 生成一个初始的扑克牌数组
int i, j, k;
for (i = 0; i < NUM_SUITS; i++) {
for (j = 0; j < NUM_RANKS; j++) {
deck[NUM_RANKS*i+j] = j;
}
}
// 打乱扑克牌数组的顺序
srand(time(NULL));
for (i = 0; i < NUM_CARDS; i++) {
j = rand() % NUM_CARDS;
k = deck[i];
deck[i] = deck[j];
deck[j] = k;
}
// 发牌
int num_players = 4;
int num_cards_per_player = NUM_CARDS / num_players;
for (i = 0; i < num_players; i++) {
printf("Player %d's cards:\n", i+1);
for (j = 0; j < num_cards_per_player; j++) {
int card_index = i*num_cards_per_player+j;
int suit_index = deck[card_index] / NUM_RANKS;
int rank_index = deck[card_index] % NUM_RANKS;
printf("%s of %s\n", ranks[rank_index], suits[suit_index]);
}
printf("\n");
}
return 0;
}
```
这个程序定义了扑克牌的花色和点数,然后生成一个初始的扑克牌数组。接着,使用 `srand()` 函数和 `rand()` 函数打乱扑克牌数组的顺序。最后,按照设定的玩家数和每个玩家的牌数,发牌并输出每个玩家手中的牌。
阅读全文