C++ 猜单词 C++ 实战开发一个猜单词的小游戏
、薛定谔的猫~ 人气:0想了解C++ 实战开发一个猜单词的小游戏的相关内容吗,、薛定谔的猫~在本文为您仔细讲解C++ 猜单词的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++,猜单词,C++,游戏,C++,实战,下面大家一起来学习吧。
前言
程序内的单词全部保存于word.txt的文本文档中,玩家排名保存在rand.txt文本文档中。运行程序时,会自动读取文本中的内容。
游戏规则:①先请用户输入猜的单词数量,可以有一个默认值。②随机抽取单词,对每个单词,系统根据谜底单词长度在屏幕上显示相应个数'#',假设谜底单词为"hello",则在屏幕上输出"#####"。③玩家输入一个字母进行猜测,如果这个字母不在单词中,系统提示玩家不对;如果猜对字母,比如玩家输入了一个'l',则在屏幕上输出"--ll-"。④重复③,直到玩家在规定的次数内猜出了单词或者超过次数游戏失败。⑤显示玩家每个单词猜对与猜错次数等统计信息。如果玩家猜出单词,计算成绩,如进入前五名提示玩家并记录存储到记录文件中。⑥询问玩家是否开始新一轮猜词,如果玩家选“否”,则系统退到外面的菜单。
效果展示
一、函数接口
enum { EXIT=0, START, CHECK, CLEAR }; //玩家结构体声明 typedef struct { string name; int right;//猜对单词个数 int wrong;//猜错个数 int score;//得分 }GamePlayer; void Show_Menu();//展示菜单内容 void exitsystem();//退出系统 void PlayGame(char File[200][100], vector<GamePlayer>& v);//开始游戏 void Check();//查看排名 void OpenFile(char File[200][100]);//打开单词文档,导入到char数组中 void Clear();//清空玩家名单 int GuessWordNum(int& GWN);//设置猜单词的数量 string InputName(string& name);//输入玩家的姓名 void Sort(vector<GamePlayer>& v);//将vector数组中的玩家按分数排名 //对自定义类型的数组排序的前置比较函数 static bool myCompare(const GamePlayer& player1, const GamePlayer& player2); void InFile(vector<GamePlayer>& v);//将排好序的玩家排名写入到"rand.txt"中
二、重要函数接口详解
1.菜单内容
void Show_Menu() { cout << "*****************************************" << endl; cout << "**********欢迎使用猜单词小程序!*********" << endl; cout << "*************0.退出单词小程序************" << endl; cout << "*************1.开始游戏******************" << endl; cout << "*************2.查看玩家排名**************" << endl; cout << "*************3.清空玩家排名**************" << endl; cout << endl; }
2.退出程序
void exitsystem() { cout << "欢迎下次使用" << endl; system("pause"); exit(0);
3.打开单词文件
void OpenFile(char File[200][100]) { ifstream ifs; int iline = 0; ifs.open("word.txt", ios::in); if (ifs) { while (!ifs.eof()) { ifs >> File[iline]; iline++; if (iline >= 200) { break; } } } else cout << "对不起,读取的单词本为空" << endl; }
4.开始游戏
void PlayGame(char File[200][100], vector<GamePlayer>& v) { int flag = 0; OpenFile(File); string name; InputName(name); int GWN = 0; GuessWordNum(GWN); int right = 0; int wrong = 0; while (GWN) { int Rand = 0;//设置随机数,放入到FiIe数组中 srand(time(NULL));//设置一个随机种子 Rand = rand() % 199;//随机取出单词 cout << "————————您一共有10次猜的机会——————" << endl; cout << "————下面是所猜单词的长度->用#来代替——————" << endl; int length = strlen(File[Rand]); for (int i = 0; i < length; i++) { cout << "#"; } cout << endl; int chance = 10; while (chance) { string guessword; cin >> guessword; if (guessword == File[Rand]) { cout << "恭喜你,猜对了" << endl; right++; flag = 1; break; } else { chance--; if (chance >= 1) { cout << "对不起,您猜错了" << endl; cout << "您还有" << chance << "次机会,请好好把握" << endl; } else { cout << "对不起,本轮您已经没有机会了" << endl; cout << "很遗憾,没猜出..." << endl; cout << "正确单词为" << File[Rand] << endl; break; } } } GWN--; if (flag == -1) { wrong++; } if (GWN == 0) { cout << "您是否要进行下一轮游戏" << endl; cout << "如果确定请输入Y,如果退出请按任意键" << endl; char s; cin >> s; if (s == 'y' || s == 'Y') { cout << "请输入您要猜单词的个数" << endl; int i = 0; cin >> i; GWN = i; } else { int score = 20 * right - 10 * wrong; cout << "本轮游戏您一共猜对了" << right << "个单词" << "猜错了" << wrong << "个单词" << endl; cout << "本轮游戏您一共得分为" << score << endl; GamePlayer GP; GP.name = name; GP.right = right; GP.wrong = wrong; GP.score = score; v.push_back(GP); cout << endl; cout << endl; cout << endl; cout << endl; } } } }
5.查看玩家排名
void Check() { ifstream ifs("rand.txt"); vector<string> show; string line; while (getline(ifs, line)) //判断排名文本是否为空 { show.push_back(line); } int count = show.size(); if (show.size() >= 1) { int i = 0; for (; i < count; i++) { cout << show[i] << endl; } cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; return; } else { cout << "对不起,暂时没有排名" << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; }
6.清空玩家排名
void Clear() { cout << "您确定要删除所有玩家的记录吗?" << endl; cout << "如果确定请输入Y,如果退出请按任意键" << endl; char s; cin >> s; if (s == 'y' || s == 'Y') { ofstream file("rand.txt", ios::trunc); if (!file) { cout << "清空文本失败" << endl; exit(0); } file.close(); return; } else { return; } }
7.玩家排名
这里对玩家的分数进行排序,利用qsort库函数
static bool myCompare(const GamePlayer& player1, const GamePlayer& player2) { return player1.score > player2.score; } void Sort(vector<GamePlayer>& v) { sort(v.begin(), v.end(), myCompare); InFile(v); }
全部代码展示
#include<fstream> #include<iostream> #include<string> #include<vector> #include<time.h> #include <algorithm> using namespace std; enum { EXIT=0, START, CHECK, CLEAR }; //玩家结构体声明 typedef struct { string name; int right;//猜对单词个数 int wrong;//猜错个数 int score;//得分 }GamePlayer; void Show_Menu();//展示菜单内容 void exitsystem();//退出系统 void PlayGame(char File[200][100], vector<GamePlayer>& v);//开始游戏 void Check();//查看排名 void OpenFile(char File[200][100]);//打开单词文档,导入到char数组中 void Clear();//清空玩家名单 int GuessWordNum(int& GWN);//设置猜单词的数量 string InputName(string& name);//输入玩家的姓名 void Sort(vector<GamePlayer>& v);//将vector数组中的玩家按分数排名 //对自定义类型的数组排序的前置比较函数 static bool myCompare(const GamePlayer& player1, const GamePlayer& player2); void InFile(vector<GamePlayer>& v);//将排好序的玩家排名写入到"rand.txt"中 void Show_Menu() { cout << "*****************************************" << endl; cout << "**********欢迎使用猜单词小程序!*********" << endl; cout << "*************0.退出单词小程序************" << endl; cout << "*************1.开始游戏******************" << endl; cout << "*************2.查看玩家排名**************" << endl; cout << "*************3.清空玩家排名**************" << endl; cout << endl; } void OpenFile(char File[200][100]) { ifstream ifs; int iline = 0; ifs.open("word.txt", ios::in); if (ifs) { while (!ifs.eof()) { ifs >> File[iline]; iline++; if (iline >= 200) { break; } } } else cout << "对不起,读取的单词本为空" << endl; } int GuessWordNum(int& GWN) { cout << "请输入你想猜单词的数量" << endl; cin >> GWN; return GWN; } string InputName(string& name) { cout << "请输入您的名字: " << endl; cin >> name; return name; } void exitsystem() { cout << "欢迎下次使用" << endl; system("pause"); exit(0); } void InFile(vector<GamePlayer>& v) { ofstream ofs; ofs.open("rand.txt", ios::out); if (ofs) { for (auto e : v) { ofs << "姓名:" << e.name << " " << "答对:" << e.right << " " << "答错:" << e.wrong << "得分:" << " " << e.score << " " << endl; } } else { cout << "对不起,没有这个排名本" << endl; } } static bool myCompare(const GamePlayer& player1, const GamePlayer& player2) { return player1.score > player2.score; } void Sort(vector<GamePlayer>& v) { sort(v.begin(), v.end(), myCompare); InFile(v); } void PlayGame(char File[200][100], vector<GamePlayer>& v) { int flag = 0; OpenFile(File); string name; InputName(name); int GWN = 0; GuessWordNum(GWN); int right = 0; int wrong = 0; while (GWN) { int Rand = 0;//设置随机数,放入到FiIe数组中 srand(time(NULL));//设置一个随机种子 Rand = rand() % 199;//随机取出单词 cout << "————————您一共有10次猜的机会——————" << endl; cout << "————下面是所猜单词的长度->用#来代替——————" << endl; int length = strlen(File[Rand]); for (int i = 0; i < length; i++) { cout << "#"; } cout << endl; int chance = 10; while (chance) { string guessword; cin >> guessword; if (guessword == File[Rand]) { cout << "恭喜你,猜对了" << endl; right++; flag = 1; break; } else { chance--; if (chance >= 1) { cout << "对不起,您猜错了" << endl; cout << "您还有" << chance << "次机会,请好好把握" << endl; } else { cout << "对不起,本轮您已经没有机会了" << endl; cout << "很遗憾,没猜出..." << endl; cout << "正确单词为" << File[Rand] << endl; break; } } } GWN--; if (flag == -1) { wrong++; } if (GWN == 0) { cout << "您是否要进行下一轮游戏" << endl; cout << "如果确定请输入Y,如果退出请按任意键" << endl; char s; cin >> s; if (s == 'y' || s == 'Y') { cout << "请输入您要猜单词的个数" << endl; int i = 0; cin >> i; GWN = i; } else { int score = 20 * right - 10 * wrong; cout << "本轮游戏您一共猜对了" << right << "个单词" << "猜错了" << wrong << "个单词" << endl; cout << "本轮游戏您一共得分为" << score << endl; GamePlayer GP; GP.name = name; GP.right = right; GP.wrong = wrong; GP.score = score; v.push_back(GP); cout << endl; cout << endl; cout << endl; cout << endl; } } } } void Check() { ifstream ifs("rand.txt"); vector<string> show; string line; while (getline(ifs, line)) { show.push_back(line); } int count = show.size(); if (show.size() >= 1) { int i = 0; for (; i < count; i++) { cout << show[i] << endl; } cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; return; } else { cout << "对不起,暂时没有排名" << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; } } void Clear() { cout << "您确定要删除所有玩家的记录吗?" << endl; cout << "如果确定请输入Y,如果退出请按任意键" << endl; char s; cin >> s; if (s == 'y' || s == 'Y') { ofstream file("rand.txt", ios::trunc); if (!file) { cout << "清空文本失败" << endl; exit(0); } file.close(); return; } else { return; } } int main() { int choice=0; char File[200][100]; vector<GamePlayer> v; while (true) { Show_Menu(); cout << "请输入您的选择: " << endl; cout << "请不要输入除数字以外的字母或符号: " << endl; cin >> choice; switch (choice) { case EXIT://退出系统 exitsystem(); break; case START://开始游戏 { PlayGame(File, v); Sort(v); break; } case CHECK://查看玩家排名 Check(); break; case CLEAR://查看玩家排名 Clear(); break; default: system("cls");//清屏操作 break; } } return 0; }
加载全部内容