用c++写一个21点游戏
时间: 2023-05-14 22:07:12 浏览: 254
c语言小游戏 21点
5星 · 资源好评率100%
好的,以下是一个简单的用 C 语言编写的 21 点游戏:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int player_score = 0, dealer_score = 0;
char choice;
srand(time(NULL)); // 初始化随机数生成器
// 发两张牌给玩家和庄家
player_score += rand() % 10 + 1;
player_score += rand() % 10 + 1;
dealer_score += rand() % 10 + 1;
dealer_score += rand() % 10 + 1;
printf("你的牌是:%d\n", player_score);
printf("庄家的牌是:%d\n", dealer_score);
// 玩家决定是否要继续要牌
while (player_score < 21) {
printf("是否要牌?(y/n) ");
scanf(" %c", &choice);
if (choice == 'y') {
player_score += rand() % 10 + 1;
printf("你的牌是:%d\n", player_score);
} else {
break;
}
}
// 判断玩家是否爆牌
if (player_score > 21) {
printf("你爆牌了!\n");
return 0;
}
// 庄家决定是否要继续要牌
while (dealer_score < 17) {
dealer_score += rand() % 10 + 1;
printf("庄家的牌是:%d\n", dealer_score);
}
// 判断庄家是否爆牌
if (dealer_score > 21) {
printf("庄家爆牌了!\n");
return 0;
}
// 比较双方点数大小
if (player_score > dealer_score) {
printf("你赢了!\n");
} else if (player_score < dealer_score) {
printf("庄家赢了!\n");
} else {
printf("平局!\n");
}
return 0;
}
希望这个程序能够帮到你!
阅读全文