使用c++编写一个21点游戏
时间: 2023-12-05 15:52:26 浏览: 37
好的,下面是一个简单的C++实现:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); // 初始化随机数种子
int sum = 0; // 记录总和
char choice = 'y'; // 是否继续游戏的选择
while (choice == 'y' || choice == 'Y') {
int num = rand() % 10 + 1; // 随机生成1-10之间的数
sum += num; // 加上当前数
cout << "当前总和为:" << sum << endl;
if (sum > 21) { // 判断是否爆掉
cout << "你爆掉了!" << endl;
break;
}
cout << "是否继续游戏?(y/n) ";
cin >> choice;
}
cout << "游戏结束,最终总和为:" << sum << endl;
return 0;
}
```
这个程序使用了一个while循环来不断询问用户是否继续游戏,每次循环生成一个1-10之间的随机数,并将其加到总和中。如果总和超过了21,就会打印出“你爆掉了!”并跳出循环。最终输出最终总和。
相关问题
用c++编写一个21点小游戏
好的,下面给出一个简单的 21 点小游戏的 C++ 代码,其中使用了随机数生成器和基础的控制结构:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); // 初始化随机数生成器
int playerTotal = 0; // 玩家手牌总点数
int dealerTotal = 0; // 庄家手牌总点数
char choice; // 玩家是否要牌的选择
// 发两张牌给玩家
playerTotal += rand() % 10 + 1;
playerTotal += rand() % 10 + 1;
cout << "你的牌是:" << playerTotal << endl;
// 发一张牌给庄家
dealerTotal += rand() % 10 + 1;
cout << "庄家的牌是:" << dealerTotal << endl;
// 玩家决定是否要继续要牌
while (playerTotal <= 21)
{
cout << "是否要牌?(Y/N)" << endl;
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
playerTotal += rand() % 10 + 1;
cout << "你的牌是:" << playerTotal << endl;
}
else
{
break;
}
}
// 庄家继续要牌,直到手牌总点数大于等于 17
while (dealerTotal < 17)
{
dealerTotal += rand() % 10 + 1;
cout << "庄家的牌是:" << dealerTotal << endl;
}
// 判断胜负
if (playerTotal > 21)
{
cout << "你爆了,庄家赢了!" << endl;
}
else if (dealerTotal > 21)
{
cout << "庄家爆了,你赢了!" << endl;
}
else if (playerTotal > dealerTotal)
{
cout << "你赢了!" << endl;
}
else if (playerTotal < dealerTotal)
{
cout << "庄家赢了!" << endl;
}
else
{
cout << "平局!" << endl;
}
return 0;
}
```
这个代码中,首先使用 `srand(time(0))` 进行了随机数生成器的初始化,然后用两个变量 `playerTotal` 和 `dealerTotal` 分别记录玩家和庄家的手牌总点数,用一个字符变量 `choice` 记录玩家是否要牌的选择。程序先发两张牌给玩家和一张牌给庄家,然后在一个 `while` 循环中,不断询问玩家是否要牌,如果玩家选择要牌,就发一张牌给玩家,并更新手牌总点数;如果玩家选择不要牌,就跳出循环。接下来,用另一个 `while` 循环,让庄家不断要牌,直到手牌总点数大于等于 17。最后,根据玩家和庄家的手牌总点数,判断胜负并输出结果。
用c++写一个21点游戏
好的,以下是一个简单的用 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;
}
希望这个程序能够帮到你!
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)