c语言程序设计21点扑克牌,C语言程序设计-21点扑克牌游戏.pdf
时间: 2023-11-09 18:59:15 浏览: 97
很抱歉,我无法提供您所需的pdf文件。但是,我可以为您提供一个简单的21点扑克牌游戏的C语言程序示例,您可以参考它来编写自己的程序。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int player, dealer, player_sum, dealer_sum;
char choice;
srand((unsigned)time(NULL)); // 初始化随机数种子
printf("Welcome to 21-point blackjack game!\n\n");
// 玩家和庄家各发两张牌
player = rand() % 10 + 1;
dealer = rand() % 10 + 1;
printf("Your first card is %d.\n", player);
printf("Dealer's first card is %d.\n", dealer);
player += rand() % 10 + 1;
dealer += rand() % 10 + 1;
printf("Your second card is %d.\n", player);
printf("Dealer's second card is hidden.\n");
// 计算点数
player_sum = player;
dealer_sum = dealer;
// 玩家回合
do {
printf("\nDo you want to hit (h) or stand (s)? ");
scanf(" %c", &choice);
if (choice == 'h') {
player += rand() % 10 + 1;
player_sum += player;
printf("Your new card is %d.\n", player);
if (player_sum > 21) {
printf("Busted! You lose.\n");
return 0;
}
}
} while (choice == 'h');
// 庄家回合
printf("\nDealer's turn.\n");
printf("Dealer's second card is %d.\n", dealer);
while (dealer_sum < 17) {
dealer += rand() % 10 + 1;
dealer_sum += dealer;
printf("Dealer's new card is %d.\n", dealer);
if (dealer_sum > 21) {
printf("Dealer busted! You win.\n");
return 0;
}
}
// 比较点数
printf("\nYour total is %d.\n", player_sum);
printf("Dealer's total is %d.\n", dealer_sum);
if (player_sum > dealer_sum) {
printf("You win!\n");
} else if (player_sum < dealer_sum) {
printf("You lose.\n");
} else {
printf("It's a tie.\n");
}
return 0;
}
```
这个程序使用随机数生成玩家和庄家的牌,在玩家回合中,玩家可以选择是否要继续要牌,直到选择停牌或者点数超过21点为止。在庄家回合中,庄家会一直要牌直到点数大于等于17点为止。最后比较点数大小,判断胜负。
请注意,这只是一个简单的示例程序,您可以根据自己的需求进行修改和优化。
阅读全文