请帮我用c++写一个石头剪刀布的游戏
当然可以帮你写一个石头剪刀布的游戏。以下是一个简单的示例代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // 初始化随机种子
int playerChoice;
int computerChoice;
cout << "石头剪刀布游戏" << endl;
cout << "------------------" << endl;
cout << "1. 石头" << endl;
cout << "2. 剪刀" << endl;
cout << "3. 布" << endl;
cout << "请输入您的选择(1-3): ";
cin >> playerChoice;
computerChoice = rand() % 3 + 1; // 生成计算机的随机选择
cout << "计算机选择了: " << computerChoice << endl;
// 判断胜负
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;
}
这个代码会首先让玩家输入自己的选择(1代表石头,2代表剪刀,3代表布),然后计算机会随机生成一个选择。接下来,根据玩家和计算机的选择进行判断,输出胜负结果
原文地址: https://www.cveoy.top/t/topic/h5Rx 著作权归作者所有。请勿转载和采集!