帮我写一个c++游戏
当然可以帮你写一个C++游戏。以下是一个简单的示例,名为“石头剪刀布”游戏的C++代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// 设置随机种子
srand(time(0));
int playerChoice;
int computerChoice;
cout << "欢迎来到石头剪刀布游戏!" << endl;
cout << "请选择:1. 石头,2.剪刀,3.布" << endl;
cin >> playerChoice;
// 生成计算机的选择
computerChoice = rand() % 3 + 1;
cout << "你选择了:";
switch (playerChoice) {
case 1:
cout << "石头" << endl;
break;
case 2:
cout << "剪刀" << endl;
break;
case 3:
cout << "布" << endl;
break;
default:
cout << "无效选择" << endl;
return 0;
}
cout << "计算机选择了:";
switch (computerChoice) {
case 1:
cout << "石头" << endl;
break;
case 2:
cout << "剪刀" << endl;
break;
case 3:
cout << "布" << endl;
break;
}
// 判断胜负
if (playerChoice == computerChoice) {
cout << "平局!" << endl;
} else if ((playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 3) ||
(playerChoice == 3 && computerChoice == 1)) {
cout << "你赢了!" << endl;
} else {
cout << "你输了!" << endl;
}
return 0;
}
这个游戏的规则很简单,玩家选择石头、剪刀或布,计算机也会随机选择。然后根据选择的不同,判断胜负并进行输出。
请注意,这只是一个简单的示例,你可以根据自己的需求和想法进行扩展和修改。希望这可以帮助到你
原文地址: https://www.cveoy.top/t/topic/hQft 著作权归作者所有。请勿转载和采集!