c语言程序设计21点扑克牌,C语言程序设计-21点扑克牌游戏.pdf
时间: 2023-11-09 14:12:44 浏览: 78
很抱歉,我是一个文本AI助手,无法提供PDF文件。不过,我可以为您提供一份基于C语言的21点扑克牌游戏代码供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_CARDS 52
#define FACE_CARD 10
#define ACE 11
int get_card_value(int card);
void shuffle_deck(int deck[]);
void deal_cards(int deck[], int *card1, int *card2);
int player_turn(int deck[], int player_total);
int dealer_turn(int deck[], int dealer_total);
int main() {
int deck[NUM_CARDS], player_total, dealer_total, card1, card2;
srand((unsigned) time(NULL));
shuffle_deck(deck);
deal_cards(deck, &card1, &card2);
player_total = get_card_value(card1) + get_card_value(card2);
printf("You have a %d and a %d.\n", card1, card2);
printf("Your total is %d.\n\n", player_total);
dealer_total = dealer_turn(deck, get_card_value(deck[2]));
if (dealer_total > 21 || dealer_total < player_total) {
printf("You win!\n");
} else {
printf("Dealer wins!\n");
}
return 0;
}
int get_card_value(int card) {
int value;
if (card > FACE_CARD) {
value = FACE_CARD;
} else if (card == 1) {
value = ACE;
} else {
value = card;
}
return value;
}
void shuffle_deck(int deck[]) {
int i, j, temp;
for (i = 0; i < NUM_CARDS; i++) {
deck[i] = i + 1;
}
for (i = 0; i < NUM_CARDS; i++) {
j = rand() % NUM_CARDS;
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
void deal_cards(int deck[], int *card1, int *card2) {
*card1 = deck[0];
*card2 = deck[1];
}
int player_turn(int deck[], int player_total) {
int card, value;
char hit;
while (player_total < 21) {
printf("Do you want another card? (y/n) ");
scanf(" %c", &hit);
if (hit == 'n') {
break;
}
card = deck[NUM_CARDS - 1];
value = get_card_value(card);
printf("You drew a %d.\n", card);
player_total += value;
if (player_total > 21) {
printf("You busted!\n");
break;
}
printf("Your total is %d.\n\n", player_total);
}
return player_total;
}
int dealer_turn(int deck[], int dealer_total) {
int card, value;
printf("The dealer's hole card is a %d.\n", deck[1]);
while (dealer_total < 17) {
card = deck[NUM_CARDS - 1];
value = get_card_value(card);
printf("The dealer drew a %d.\n", card);
dealer_total += value;
if (dealer_total > 21) {
printf("Dealer busted!\n");
break;
}
printf("Dealer's total is %d.\n\n", dealer_total);
}
return dealer_total;
}
```
这个代码使用一个数组来表示一副扑克牌,通过随机洗牌函数来打乱这副牌的顺序。然后,根据游戏规则,玩家和庄家各发两张牌,然后根据牌的点数来进行游戏。当玩家的点数小于或等于21并且没有爆牌时,可以选择继续要牌或者不要牌。当玩家停止要牌后,庄家也进行类似的操作。最后,比较玩家和庄家的点数,判断胜负。
阅读全文