Run ID:104471

提交时间:2025-01-05 10:40:10

#include <iostream> using namespace std; enum choices{ hammer, // 石头 scissors, // 剪刀 cloth // 布 }; // char choice_name[][9] = { "hammer", "scissors", "cloth" }; class Game { public: Game(); // 构造函数初始化数据成员 void run(); // 游戏运行入口 void initial(); // 游戏规则及操作说明 void instruct(); // 游戏规则 void helpp(); // 操作说明 void choose_mode(); // 选择游戏模式 void play(); // 进行游戏 choices choose(); // 选择下续操作 choices get_random(); // 获得随机选择 void show_result(int, int, int); // 显示对局结果 void compare(choices, choices); // 判断输赢 private: int win; // 玩家1赢的次数 int lose; // 玩家1输的次数 int tie; // 平局次数 char mode[2]; // 记录游戏模式 choices player1, player2; // 记录玩家做的选择 }; 主函数 #include<iostream> #include"game.h" using namespace std; int main() { Game g; g.run(); return 0; } 具体功能实现代码 #include "game.h" #include <string> #include <conio.h> Game::Game() { win = 0; lose = 0; tie = 0; } void Game::run() // 游戏运行入口 { initial(); // 游戏规则及操作说明 choose_mode(); // 选择游戏模式 play(); // 进行游戏 } void Game::initial() // 游戏规则及操作说明 { cout << "**** 石头剪刀布 游戏说明 **** " << endl; cout << endl; instruct(); helpp(); cout << "祝你好运!" << endl; cout << endl; } void Game::instruct() // 游戏规则 { cout << "游戏规则" << endl; cout << "\t 在这个游戏中" << endl; cout << "\t h 表示石头" << endl; cout << "\t s 表示剪刀" << endl; cout << "\t c 表示布" << endl; cout << "\t 若两玩家的选择相同, 那么这一局就是平局" << endl; cout << "\t 否则, 石头打剪刀, 布包石头, 剪刀剪布" << endl; cout << "\t 这个游戏一直重复到玩家退出为止." << endl; cout << endl; } void Game::helpp() // 操作说明 { cout << "操作说明" << endl; cout << "\t h 表示石头" << endl; cout << "\t s 表示剪刀" << endl; cout << "\t c 表示布" << endl; cout << "\t r 查看对局结果" << endl; cout << "\t p 查看操作说明" << endl; cout << "\t i 查看游戏规则" << endl; cout << "\t q 表示退出游戏" << endl; cout << endl; } void Game::choose_mode() // 选择游戏模式 { string step1 = "\n\t 1 人机模式 \n\t 2 双人模式 "; string step2 = "\n\t 1 三局两胜 \n\t 2 五局三胜 "; int i = 0; string step; while (i < 2) { step = (i == 0) ? step1 : step2; cout << "\n请选择: " << step << "\n>>> "; while ((cin >> mode[i]) && mode[i] == ' ' || mode[i] == '\n' || mode[i] == '\t'); // 未输入或输入为空字符 if (mode[i] == '1' || mode[i] == '2') i += 1; } } void Game::play() // 进行游戏 { int WIN_MAX, SUM_MAX; // 最多赢场, 最多局数 if (mode[1] == '1') // 三局两胜 { WIN_MAX = 2; SUM_MAX = 3; } else // 五局三胜 { WIN_MAX = 3; SUM_MAX = 5; } while ((win < WIN_MAX) && ((win + lose + tie) < SUM_MAX)) { cout << endl; cout << "***** 第" << win + lose + tie + 1 << "局 *****" << endl; cout << endl; cout << "***** 玩家1界面 *****" << endl; player1 = choose(); cout << "***** 玩家2界面 *****" << endl; if (mode[0] == '1') // 人机模式 { cout << " 电脑已作出选择" << endl; player2 = get_random(); } else // 双人模式 player2 = choose(); compare(player1, player2); // 判断输赢 } show_result(win, lose, tie); // 显示比赛结果 cout << endl; cout << "游戏结束!" << endl; exit(0); // 退出游戏 } choices Game::choose() // 选择下续操作 { char c; choices p; cout << endl; cout << "请选择: \n\t h 表示石头 \n\t s 表示剪刀\n\t c 表示布 \n>>> "; do { c = _getch(); cout << "*" << endl; } while (c == ' ' || c == '\n' || c == '\t'); switch (c) { case 'c': p = cloth; break; case 'h': p = hammer; break; case 's': p = scissors; break; case 'r': // 查看对局结果 show_result(win, lose, tie); p = choose(); break; case 'i': // 查看游戏规则 instruct(); p = choose(); break; case 'q': // 退出 cout << "已退出" << endl; exit(0); break; default: // 查看操作说明 helpp(); p = choose(); break; } return(p); } choices Game::get_random() // 获得随机选择 { static int i; i = ++i % 3; return(choices(i)); // 根据余数得随机值 } void Game::show_result(int win, int lose, int tie) // 显示比赛结果 { cout << endl; cout << "游戏结果如下:" << endl; cout << "\t 玩家1获胜的次数是: " << win << endl; cout << "\t 玩家2获胜的次数是: " << lose << endl; cout << "\t 平局的次数为: " << tie << endl; cout << "\t 游戏的总次数是: " << win + lose + tie << endl; } void Game::compare(choices player1, choices player2) // 判断输赢 { // cout << "玩家1: " << player1 << " 玩家2: " << player2 << endl; if (player1 == player2) { tie += 1; cout << "平局" << endl; }