通过多个函数实现一个猜字母游戏

来源:百度知道 编辑:UC知道 时间:2024/05/05 21:43:38
通过多个函数实现一个猜字母游戏要求:
1:菜单格式如下:
1 play game
2 set game
3 quit game
当用户输入1时,开始玩游戏;
当用户输入2时,要求输入最多允许猜词的次数,例如用户输入3表示如果连续3次猜不中则结束游戏; 当用户输入3时,退出程序. ②游戏规则:猜26个小写字母,如果猜对,则打印"right"结束游戏,并打印所猜的次数,猜错则打印"wrong"并允许重新猜词;超过游戏设置允许的最大猜词次数时,打印"too many times"结束游戏.
用C语言回答.随机字母的生成方法:rand()%('z'-'a'+1)+'a',其中,使用随机函数rand()需要用#include<stdlib.h>作文件包含

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <string>
using namespace std;

void show_menu() {
string msg[] = {"1 play game", "2 set game", "3 quit game "};
for (int i = 0; i < 3; ++i) {
cout << msg[i] << endl;
}
}
void guess_game(int try_time) {
char ch1, ch2;
int cnt = 0;
srand(time(NULL));
ch1 = (char)(random() % 26 + 'a');
do {
cin >> ch2;
if(ch1 == ch2) {
cout << "right" << endl;
}
else {
cout << "wrong" << endl;
}
++cnt;
}
while (cnt < try_time && ch1 != ch2);
}
int main() {

int user_opt = 0;
unsigned int try_time = 3;

show_menu();
cin >>user_opt;
switch(user_opt) {
case 1:
guess_gam