c语言程序设计21点扑克牌,c语言程序设计 21点扑克牌游戏
时间: 2023-12-05 18:01:08 浏览: 77
一个21点的扑克牌游戏_C++课程设计_源代码_亲测可用.zip
5星 · 资源好评率100%
以下是一个简单的C语言程序设计21点扑克牌游戏的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int player_score = 0, dealer_score = 0;
int player_cards[5], dealer_cards[5];
int player_card_count = 0, dealer_card_count = 0;
int i, card;
srand(time(NULL));
// 初始化玩家和庄家的手牌
player_cards[player_card_count++] = rand() % 13 + 1;
dealer_cards[dealer_card_count++] = rand() % 13 + 1;
// 玩家先抽一张牌
player_cards[player_card_count++] = rand() % 13 + 1;
player_score = player_cards[0] + player_cards[1];
printf("你的手牌: %d %d (总分: %d)\n", player_cards[0], player_cards[1], player_score);
// 庄家抽一张牌
dealer_cards[dealer_card_count++] = rand() % 13 + 1;
dealer_score = dealer_cards[0];
printf("庄家的手牌: %d ? (总分: ?)\n", dealer_cards[0]);
// 玩家决定是否继续抽牌
while (player_score < 21) {
printf("是否继续抽牌 (y/n)? ");
char choice;
scanf(" %c", &choice);
if (choice == 'n') {
break;
}
card = rand() % 13 + 1;
player_cards[player_card_count++] = card;
player_score += card;
printf("你抽到了 %d (总分: %d)\n", card, player_score);
}
// 庄家继续抽牌直到总分大于等于17
while (dealer_score < 17) {
card = rand() % 13 + 1;
dealer_cards[dealer_card_count++] = card;
dealer_score += card;
}
// 输出庄家的手牌和总分
printf("庄家的手牌: ");
for (i = 0; i < dealer_card_count; i++) {
printf("%d ", dealer_cards[i]);
}
printf("(总分: %d)\n", dealer_score);
// 判断胜负
if (player_score > 21) {
printf("你爆了,庄家获胜!\n");
} else if (dealer_score > 21) {
printf("庄家爆了,你获胜!\n");
} else if (player_score > dealer_score) {
printf("你的总分大于庄家,你获胜!\n");
} else if (player_score < dealer_score) {
printf("你的总分小于庄家,庄家获胜!\n");
} else {
printf("你和庄家的总分相同,平局!\n");
}
return 0;
}
```
这个程序使用了随机数生成器来模拟扑克牌的抽取。玩家先抽两张牌,然后可以选择是否继续抽牌,直到总分大于等于21或者选择停止抽牌。庄家会先抽一张牌,然后根据规则继续抽牌,直到总分大于等于17。最后程序会判断双方的总分,输出胜负结果。
阅读全文