下面是一个简单的金木水火土五行克制游戏的C++代码示例。在这个游戏中,玩家需要选择一个五行,然后根据选择的五行来攻击或防御其他五行。
#include <iostream>
#include <string>
int main() {
std::string playerChoice;
std::string computerChoice;
std::string result;
std::cout << "请选择你的五行(金木水火土):";
std::cin >> playerChoice;
// 计算机随机选择一个五行
std::string possibleChoices[] = {"金", "木", "水", "火", "土"};
int randomIndex = rand() % 5;
computerChoice = possibleChoices[randomIndex];
// 根据五行相生相克关系判断胜负
if (playerChoice == "金") {
if (computerChoice == "木") {
result = "你赢了!金克木。";
} else if (computerChoice == "水") {
result = "平局!金生水。";
} else if (computerChoice == "火") {
result = "你输了!火克金。";
} else if (computerChoice == "土") {
result = "平局!土生金。";
} else {
result = "无效的选择。";
}
} else if (playerChoice == "木") {
if (computerChoice == "水") {
result = "你赢了!木克水。";
} else if (computerChoice == "火") {
result = "你输了!火克木。";
} else if (computerChoice == "土") {
result = "平局!木生土。";
} else if (computerChoice == "金") {
result = "你输了!金克木。";
} else {
result = "无效的选择。";
}
} else if (playerChoice == "水") {
if (computerChoice == "火") {
result = "你赢了!水克火。";
} else if (computerChoice == "土") {
result = "平局!水生土。";
} else if (computerChoice == "金") {
result = "平局!水生金。";
} else if (computerChoice == "木") {
result = "你输了!木克水。";
} else {
result = "无效的选择。";
}
} else if (playerChoice == "火") {
if (computerChoice == "木") {
result = "你赢了!火克木。";
} else if (computerChoice == "金") {
result = "你输了!火怕金。";
} else if (computerChoice == "水") {
result = "你输了!水克火。";
} else if (computerChoice == "土") {
result = "平局!火生土。";
} else {
result = "无效的选择。";
}
} else if (playerChoice == "土") {
if (computerChoice == "金") {
result = "你赢了!土生金。";
} else if (computerChoice == "木") {
result = "平局!木克土。";
} else if (computerChoice == "水") {
result = "你输了!土克水。";
} else if (computerChoice == "火") {
result = "平局!火生土。";
} else {
result = "无效的选择。";
}
} else {
result = "无效的选择。";
}
std::cout << result << std::endl;
return 0;
}