// 结果类型枚举
enum ResultType { WIN, LOSE, TIE };
const char* ResultName[] = {"平局", "你输了", "你赢了"};
// 游戏类
class RockPaperScissors {
public:
RockPaperScissors() {
srand(static_cast(time(nullptr)));
wins = losses = ties = 0;
}
// 玩一局
ResultType playRound(SelectType playerChoice) {
SelectType computerChoice = static_cast(rand() % 3);
cout << "电脑出拳:" << MoveName[computerChoice] << endl;
if (playerChoice == computerChoice) {
ties++;
return TIE;
}
// 判断胜负
if ((playerChoice == STONE && computerChoice == SCISSORS) ||
(playerChoice == SCISSORS && computerChoice == CLOTH) ||
(playerChoice == CLOTH && computerChoice == STONE)) {
wins++;
return WIN;
} else {
losses++;
return LOSE;
}
}
void showStats() const {
cout << "当前统计:赢 " << wins << " 局,输 " << losses << " 局,平 " << ties << " 局。\n";
}
private:
int wins, losses, ties;
};
int main() {
RockPaperScissors game;
cout << "石头-剪刀-布游戏开始!\n";
cout << "请输入你的选择:0(石头),1(剪刀),2(布),或其他数字退出。\n";
while (true) {
try {
int input;
cout << "你的出拳: ";
if (!(cin >> input)) throw runtime_error("输入错误");
if (input < 0 || input > 2) {
cout << "游戏结束。\n";
break;
}
SelectType playerMove = static_cast(input);
cout << "你出拳:" << MoveName[playerMove] << endl;
ResultType result = game.playRound(playerMove);
cout << "结果:" << ResultName[result] << "。\n";
game.showStats();
} catch (exception &e) {
cout << "输入无效,请输入0、1或2。\n";
cin.clear();
cin.ignore(10000, '\n');
}
}
return 0;
}
进行功能扩展,使程序达到300行,尽量少进行批注
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <stdexcept>
#include <iomanip>
using namespace std;
// 出拳类型枚举
enum SelectType { STONE = 0, SCISSORS = 1, CLOTH = 2 };
const char* MoveName[] = {"石头", "剪刀", "布"};
// 结果类型枚举
enum ResultType { WIN, LOSE, TIE };
const char* ResultName[] = {"平局", "你输了", "你赢了"};
// 游戏类
class RockPaperScissors {
public:
RockPaperScissors() {
srand(static_cast<unsigned>(time(nullptr)));
wins = losses = ties = rounds = 0;
playerHistory.clear();
computerHistory.clear();
}
// 玩一局
ResultType playRound(SelectType playerChoice) {
SelectType computerChoice = static_cast<SelectType>(rand() % 3);
cout << "电脑出拳:" << MoveName[computerChoice] << endl;
playerHistory.push_back(playerChoice);
computerHistory.push_back(computerChoice);
rounds++;
if (playerChoice == computerChoice) {
ties++;
return TIE;
}
// 判断胜负
if ((playerChoice == STONE && computerChoice == SCISSORS) ||
(playerChoice == SCISSORS && computerChoice == CLOTH) ||
(playerChoice == CLOTH && computerChoice == STONE)) {
wins++;
return WIN;
} else {
losses++;
return LOSE;
}
}
void showStats() const {
cout << "当前统计:\n";
cout << "总轮数:" << rounds << "\n";
cout << "赢 " << wins << " 局,输 " << losses << " 局,平 " << ties << " 局。\n";
cout << "胜率:" << fixed << setprecision(2) << (static_cast<double>(wins) / rounds * 100) << "%\n";
}
void showHistory() const {
if (rounds == 0) {
cout << "暂无历史记录。\n";
return;
}
cout << "历史记录(最近 " << min(rounds, 5) << " 轮):\n";
for (int i = max(0, static_cast<int>(rounds - 5)); i < rounds; ++i) {
cout << "第 " << i + 1 << " 轮:你出 " << MoveName[playerHistory[i]]
<< ",电脑出 " << MoveName[computerHistory[i]] << "\n";
}
}
private:
int wins, losses, ties, rounds;
vector<SelectType> playerHistory;
vector<SelectType> computerHistory;
};
void displayMenu() {
cout << "\n菜单选项:\n";
cout << "1. 开始新一局\n";
cout << "2. 查看统计信息\n";
cout << "3. 查看历史记录\n";
cout << "4. 退出游戏\n";
}
int main() {
RockPaperScissors game;
cout << "欢迎来到石头-剪刀-布游戏!\n";
while (true) {
displayMenu();
int choice;
cout << "请选择操作(1-4):";
cin >> choice;
try {
switch (choice) {
case 1: {
cout << "请输入你的选择:0(石头),1(剪刀),2(布)。\n";
int input;
cout << "你的出拳: ";
cin >> input;
if (input < 0 || input > 2) throw runtime_error("无效输入");
SelectType playerMove = static_cast<SelectType>(input);
cout << "你出拳:" << MoveName[playerMove] << endl;
ResultType result = game.playRound(playerMove);
cout << "结果:" << ResultName[result] << "。\n";
break;
}
case 2: {
game.showStats();
break;
}
case 3: {
game.showHistory();
break;
}
case 4: {
cout << "感谢游玩,再见!\n";
return 0;
}
default: {
throw runtime_error("无效选项");
}
}
} catch (exception &e) {
cout << "错误:" << e.what() << "\n";
cin.clear();
cin.ignore(10000, '\n');
}
}
return 0;
}