剪刀石头布代码

信息化聊科技 2025-01-19 14:06:47
#include #include #include #include #include // Windows系统需要这个头文件来设置控制台编码using namespace std;class RockPaperScissors {private: int playerScore; int computerScore; int rounds; string getComputerChoice() { int choice = rand() % 3; switch (choice) { case 0: return "rock"; case 1: return "scissors"; case 2: return "paper"; default: return "rock"; } } string getChineseChoice(const string& choice) { if (choice == "rock") return "石头"; if (choice == "scissors") return "剪刀"; if (choice == "paper") return "布"; return ""; } bool determineWinner(const string& playerChoice, const string& computerChoice) { if (playerChoice == computerChoice) { cout << "平局!\n"; return false; } if ((playerChoice == "rock" && computerChoice == "scissors") || (playerChoice == "scissors" && computerChoice == "paper") || (playerChoice == "paper" && computerChoice == "rock")) { cout << "你赢了!\n"; playerScore++; return true; } else { cout << "电脑赢了!\n"; computerScore++; return true; } } void displayScore() { cout << "\n当前比分:\n"; cout << "玩家: " << playerScore << " 分\n"; cout << "电脑: " << computerScore << " 分\n"; cout << "剩余回合数: " << rounds << "\n"; cout << "------------------------\n"; } string convertInputToEnglish(const string& input) { if (input == "石头" || input == "1") return "rock"; if (input == "剪刀" || input == "2") return "scissors"; if (input == "布" || input == "3") return "paper"; return ""; } bool isValidChoice(const string& choice) { return (choice == "石头" || choice == "剪刀" || choice == "布" || choice == "1" || choice == "2" || choice == "3"); }public: RockPaperScissors() : playerScore(0), computerScore(0), rounds(5) { srand(time(0)); } void startGame() { cout << "欢迎来到剪刀石头布游戏!\n"; cout << "游戏规则:\n"; cout << "1. 总共5个回合\n"; cout << "2. 输入选项:\n"; cout << " 石头 (或输入1)\n"; cout << " 剪刀 (或输入2)\n"; cout << " 布 (或输入3)\n"; cout << "3. 获胜最多的一方获得最终胜利\n"; cout << "------------------------\n"; while (rounds > 0) { string playerInput; cout << "\n请输入你的选择 (石头/剪刀/布 或 1/2/3): "; cin >> playerInput; if (!isValidChoice(playerInput)) { cout << "无效的选择,请重新输入!\n"; continue; } string playerChoice = convertInputToEnglish(playerInput); string computerChoice = getComputerChoice(); cout << "你的选择: " << getChineseChoice(playerChoice) << endl; cout << "电脑的选择: " << getChineseChoice(computerChoice) << endl; determineWinner(playerChoice, computerChoice); rounds--; displayScore(); } announceWinner(); } void announceWinner() { cout << "\n游戏结束!\n"; cout << "最终比分:\n"; cout << "玩家: " << playerScore << " 分\n"; cout << "电脑: " << computerScore << " 分\n"; if (playerScore > computerScore) { cout << "恭喜你获得最终胜利!\n"; } else if (playerScore < computerScore) { cout << "电脑获得最终胜利!\n"; } else { cout << "游戏最终平局!\n"; } } void resetGame() { playerScore = 0; computerScore = 0; rounds = 5; }};int main() { // 设置控制台编码为 UTF-8 SetConsoleOutputCP(65001); char playAgain; RockPaperScissors game; do { game.startGame(); cout << "\n是否要再玩一次?(y/n): "; cin >> playAgain; if (playAgain == 'y' || playAgain == 'Y') { game.resetGame(); cout << "\n新游戏开始!\n"; } } while (playAgain == 'y' || playAgain == 'Y'); cout << "感谢游玩!再见!\n"; return 0;}
0 阅读:0

信息化聊科技

简介:感谢大家的关注