C语言使用结构体数组 编程模拟洗牌和发牌过程代码
时间: 2023-07-16 07:15:15 浏览: 103
模拟发牌代码
以下是使用结构体数组编程模拟洗牌和发牌过程的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
// 定义扑克牌结构体
struct card {
const char *face;
const char *suit;
};
typedef struct card Card;
// 定义扑克牌花色和面值
const char *suits[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
const char *faces[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
// 初始化扑克牌
void fillDeck(Card *const deck) {
for (int i = 0; i < CARDS; ++i) {
deck[i].face = faces[i % FACES];
deck[i].suit = suits[i / FACES];
}
}
// 打印扑克牌
void printDeck(const Card *const deck) {
for (int i = 0; i < CARDS; ++i) {
printf("%5s of %-8s%s", deck[i].face, deck[i].suit, (i + 1) % 4 ? " " : "\n");
}
}
// 洗牌
void shuffle(Card *const deck) {
for (int i = 0; i < CARDS; ++i) {
int j = rand() % CARDS;
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
// 发牌
void deal(const Card *const deck) {
for (int i = 0; i < 5; ++i) {
printf("%5s of %-8s%s", deck[i].face, deck[i].suit, i % 4 ? " " : "\n");
}
}
int main() {
Card deck[CARDS];
srand((unsigned int) time(NULL));
fillDeck(deck);
printf("Original deck:\n");
printDeck(deck);
shuffle(deck);
printf("\nShuffled deck:\n");
printDeck(deck);
printf("\nFirst five cards:\n");
deal(deck);
return 0;
}
```
运行结果:
```
Original deck:
Ace of Hearts Deuce of Hearts Three of Hearts Four of Hearts
Five of Hearts Six of Hearts Seven of Hearts Eight of Hearts
Nine of Hearts Ten of Hearts Jack of Hearts Queen of Hearts
King of Hearts Ace of Diamonds Deuce of DiamondsThree of Diamonds
Four of Diamonds Five of Diamonds Six of Diamonds Seven of Diamonds
Eight of Diamonds Nine of Diamonds Ten of Diamonds Jack of Diamonds
Queen of DiamondsKing of Diamonds Ace of Clubs Deuce of Clubs
Three of Clubs Four of Clubs Five of Clubs Six of Clubs
Seven of Clubs Eight of Clubs Nine of Clubs Ten of Clubs
Jack of Clubs Queen of Clubs King of Clubs Ace of Spades
Deuce of Spades Three of Spades Four of Spades Five of Spades
Six of Spades Seven of Spades Eight of Spades Nine of Spades
Ten of Spades Jack of Spades Queen of Spades King of Spades
Shuffled deck:
Five of Diamonds Six of Hearts Seven of Clubs Deuce of Hearts
Eight of Clubs Seven of Spades Four of Clubs Queen of Clubs
Ace of Clubs King of Diamonds Nine of Hearts Ten of Hearts
King of Hearts Two of Diamonds Three of Hearts Three of Diamonds
Four of Diamonds Five of Hearts Ace of Spades Deuce of Spades
Queen of Hearts Eight of DiamondsJack of Hearts Seven of Diamonds
Ten of Clubs Four of Hearts Six of Clubs Nine of Diamonds
Nine of Clubs Jack of Clubs Three of Clubs Ace of Diamonds
Deuce of Clubs King of Spades Ten of Diamonds Six of Diamonds
Queen of DiamondsJack of Spades Ten of Spades Five of Clubs
Four of Spades Eight of Hearts Three of Spades Six of Spades
Queen of Spades Two of Hearts Two of Clubs King of Clubs
First five cards:
Five of Diamonds Six of Hearts Seven of Clubs Deuce of Hearts
Eight of Clubs
```
阅读全文