要让c++++石头剪刀布游戏的电脑选择更智能,可通过记录玩家历史选择调整电脑出招概率;若仅需视觉上的“思考”,可引入延迟;避免无效输入的方法包括使用循环持续提示或支持字符串输入转换;扩展游戏功能如多局比赛和得分记录可通过引入循环与变量实现。

C++石头剪刀布游戏的核心在于如何让电脑随机选择,以及如何根据玩家和电脑的选择判断胜负。这涉及到随机数生成和条件语句的运用。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main() {
// 初始化随机数种子
srand(time(0));
// 1 - 石头, 2 - 剪刀, 3 - 布
int computerChoice = (rand() % 3) + 1;
int userChoice;
cout << "请选择:1 - 石头, 2 - 剪刀, 3 - 布" << endl;
cin >> userChoice;
string computerSelection;
string userSelection;
switch (computerChoice) {
case 1:
computerSelection = "石头";
break;
case 2:
computerSelection = "剪刀";
break;
case 3:
computerSelection = "布";
break;
}
switch (userChoice) {
case 1:
userSelection = "石头";
break;
case 2:
userSelection = "剪刀";
break;
case 3:
userSelection = "布";
break;
default:
cout << "无效的选择,请重新运行程序。" << endl;
return 1; // 退出程序,表示出错
}
cout << "电脑选择了: " << computerSelection << endl;
cout << "你选择了: " << userSelection << endl;
// 判断胜负
if (userChoice == computerChoice) {
cout << "平局!" << endl;
} else if ((userChoice == 1 && computerChoice == 2) ||
(userChoice == 2 && computerChoice == 3) ||
(userChoice == 3 && computerChoice == 1)) {
cout << "你赢了!" << endl;
} else {
cout << "你输了!" << endl;
}
return 0;
}其实“智能”在这里更多的是指增加一些随机性,让玩家感觉电脑不是完全随机的。一种方式是记录玩家之前的选择,然后稍微调整电脑的选择概率。但这其实增加了游戏的复杂度,对于初学者来说,完全随机已经足够了。另一种方法是引入延迟,让电脑“思考”一下,但这更多是视觉上的效果,并没有真正改变随机性。
代码中已经加入了基本的输入验证,如果玩家输入了1、2、3之外的数字,程序会报错并退出。但还可以更进一步,例如使用循环来不断提示玩家输入,直到输入有效为止。或者,可以允许玩家输入字符串,然后将字符串转换为对应的数字。
立即学习“C++免费学习笔记(深入)”;

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main() {
srand(time(0));
int computerChoice = (rand() % 3) + 1;
int userChoice;
while (true) {
cout << "请选择:1 - 石头, 2 - 剪刀, 3 - 布" << endl;
cin >> userChoice;
if (userChoice >= 1 && userChoice <= 3) {
break; // 输入有效,退出循环
} else {
cout << "无效的选择,请重新输入。" << endl;
}
}
string computerSelection;
string userSelection;
switch (computerChoice) {
case 1:
computerSelection = "石头";
break;
case 2:
computerSelection = "剪刀";
break;
case 3:
computerSelection = "布";
break;
}
switch (userChoice) {
case 1:
userSelection = "石头";
break;
case 2:
userSelection = "剪刀";
break;
case 3:
userSelection = "布";
break;
}
cout << "电脑选择了: " << computerSelection << endl;
cout << "你选择了: " << userSelection << endl;
if (userChoice == computerChoice) {
cout << "平局!" << endl;
} else if ((userChoice == 1 && computerChoice == 2) ||
(userChoice == 2 && computerChoice == 3) ||
(userChoice == 3 && computerChoice == 1)) {
cout << "你赢了!" << endl;
} else {
cout << "你输了!" << endl;
}
return 0;
}增加局数和记录得分,只需要引入循环和变量即可。在主循环中,每一轮游戏结束后更新得分,然后询问玩家是否继续。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main() {
srand(time(0));
int userScore = 0;
int computerScore = 0;
char playAgain = 'y';
while (playAgain == 'y' || playAgain == 'Y') {
int computerChoice = (rand() % 3) + 1;
int userChoice;
while (true) {
cout << "请选择:1 - 石头, 2 - 剪刀, 3 - 布" << endl;
cin >> userChoice;
if (userChoice >= 1 && userChoice <= 3) {
break;
} else {
cout << "无效的选择,请重新输入。" << endl;
}
}
string computerSelection;
string userSelection;
switch (computerChoice) {
case 1:
computerSelection = "石头";
break;
case 2:
computerSelection = "剪刀";
break;
case 3:
computerSelection = "布";
break;
}
switch (userChoice) {
case 1:
userSelection = "石头";
break;
case 2:
userSelection = "剪刀";
break;
case 3:
userSelection = "布";
break;
}
cout << "电脑选择了: " << computerSelection << endl;
cout << "你选择了: " << userSelection << endl;
if (userChoice == computerChoice) {
cout << "平局!" << endl;
} else if ((userChoice == 1 && computerChoice == 2) ||
(userChoice == 2 && computerChoice == 3) ||
(userChoice == 3 && computerChoice == 1)) {
cout << "你赢了!" << endl;
userScore++;
} else {
cout << "你输了!" << endl;
computerScore++;
}
cout << "当前得分:你 - " << userScore << ", 电脑 - " << computerScore << endl;
cout << "再玩一次吗? (y/n)" << endl;
cin >> playAgain;
}
cout << "游戏结束!最终得分:你 - " << userScore << ", 电脑 - " << computerScore << endl;
return 0;
}
以上就是C++石头剪刀布游戏怎么做 随机选择与条件判断练习的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号