最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ 實戰(zhàn)開發(fā)一個猜單詞的小游戲

 更新時間:2021年11月17日 14:49:35   作者:、薛定諤的貓~  
眾所周知紙上得來終覺淺,我們要在實戰(zhàn)中才能真正的掌握技術(shù),小編為大家?guī)硪环萦肅++編寫的猜單詞小游戲,給大家練練手,快來看看吧

前言

程序內(nèi)的單詞全部保存于word.txt的文本文檔中,玩家排名保存在rand.txt文本文檔中。運行程序時,會自動讀取文本中的內(nèi)容。

游戲規(guī)則:①先請用戶輸入猜的單詞數(shù)量,可以有一個默認值。②隨機抽取單詞,對每個單詞,系統(tǒng)根據(jù)謎底單詞長度在屏幕上顯示相應個數(shù)'#',假設(shè)謎底單詞為"hello",則在屏幕上輸出"#####"。③玩家輸入一個字母進行猜測,如果這個字母不在單詞中,系統(tǒng)提示玩家不對;如果猜對字母,比如玩家輸入了一個'l',則在屏幕上輸出"--ll-"。④重復③,直到玩家在規(guī)定的次數(shù)內(nèi)猜出了單詞或者超過次數(shù)游戲失敗。⑤顯示玩家每個單詞猜對與猜錯次數(shù)等統(tǒng)計信息。如果玩家猜出單詞,計算成績,如進入前五名提示玩家并記錄存儲到記錄文件中。⑥詢問玩家是否開始新一輪猜詞,如果玩家選“否”,則系統(tǒng)退到外面的菜單。

效果展示

一、函數(shù)接口

enum
{
	EXIT=0,
	START,
	CHECK,
	CLEAR
};
 
//玩家結(jié)構(gòu)體聲明
typedef struct 
{
	string name;
	int right;//猜對單詞個數(shù)
	int wrong;//猜錯個數(shù)
	int score;//得分
}GamePlayer;
void Show_Menu();//展示菜單內(nèi)容
void exitsystem();//退出系統(tǒng)
void PlayGame(char File[200][100], vector<GamePlayer>& v);//開始游戲
void Check();//查看排名
void OpenFile(char File[200][100]);//打開單詞文檔,導入到char數(shù)組中
void Clear();//清空玩家名單
int GuessWordNum(int& GWN);//設(shè)置猜單詞的數(shù)量
string InputName(string& name);//輸入玩家的姓名
void Sort(vector<GamePlayer>& v);//將vector數(shù)組中的玩家按分數(shù)排名
 
//對自定義類型的數(shù)組排序的前置比較函數(shù)
static bool myCompare(const GamePlayer& player1, const GamePlayer& player2);
void InFile(vector<GamePlayer>& v);//將排好序的玩家排名寫入到"rand.txt"中

二、重要函數(shù)接口詳解

1.菜單內(nèi)容

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;//設(shè)置隨機數(shù),放入到FiIe數(shù)組中
		srand(time(NULL));//設(shè)置一個隨機種子
		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 << "對不起,本輪您已經(jīng)沒有機會了" << 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 << "請輸入您要猜單詞的個數(shù)" << 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.玩家排名

這里對玩家的分數(shù)進行排序,利用qsort庫函數(shù)

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
};
 
//玩家結(jié)構(gòu)體聲明
typedef struct 
{
	string name;
	int right;//猜對單詞個數(shù)
	int wrong;//猜錯個數(shù)
	int score;//得分
}GamePlayer;
void Show_Menu();//展示菜單內(nèi)容
void exitsystem();//退出系統(tǒng)
void PlayGame(char File[200][100], vector<GamePlayer>& v);//開始游戲
void Check();//查看排名
void OpenFile(char File[200][100]);//打開單詞文檔,導入到char數(shù)組中
void Clear();//清空玩家名單
int GuessWordNum(int& GWN);//設(shè)置猜單詞的數(shù)量
string InputName(string& name);//輸入玩家的姓名
void Sort(vector<GamePlayer>& v);//將vector數(shù)組中的玩家按分數(shù)排名
 
//對自定義類型的數(shù)組排序的前置比較函數(shù)
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 << "請輸入你想猜單詞的數(shù)量" << 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;//設(shè)置隨機數(shù),放入到FiIe數(shù)組中
		srand(time(NULL));//設(shè)置一個隨機種子
		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 << "對不起,本輪您已經(jīng)沒有機會了" << 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 << "請輸入您要猜單詞的個數(shù)" << 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 << "請不要輸入除數(shù)字以外的字母或符號: " << endl;
		cin >> choice;
			switch (choice)
		{
			case EXIT://退出系統(tǒng)
				exitsystem();
				break;
			case START://開始游戲
			{
				PlayGame(File, v);
				Sort(v);
				break;
			}
			case CHECK://查看玩家排名
				Check();
				break;
			case CLEAR://查看玩家排名
				Clear();
				break;
			default:
				system("cls");//清屏操作
				break;
		}
	}
	return 0;
}
 
 

到此這篇關(guān)于C++ 實戰(zhàn)開發(fā)一個猜單詞的小游戲的文章就介紹到這了,更多相關(guān)C++ 猜單詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言實現(xiàn)strlen的三種方法小結(jié)

    C語言實現(xiàn)strlen的三種方法小結(jié)

    本文主要介紹了C語言實現(xiàn)strlen的三種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • 詳解C語言之緩沖區(qū)溢出

    詳解C語言之緩沖區(qū)溢出

    緩沖區(qū)是一塊連續(xù)的計算機內(nèi)存區(qū)域,可保存相同數(shù)據(jù)類型的多個實例。緩沖區(qū)可以是堆棧、堆和靜態(tài)數(shù)據(jù)區(qū)。在C/C++語言中,通常使用字符數(shù)組和malloc/new實現(xiàn)緩沖區(qū)。溢出指數(shù)據(jù)被添加到分配給該緩沖區(qū)的內(nèi)存塊之外。緩沖區(qū)溢出是最常見的程序缺陷
    2021-06-06
  • 一文讓你徹底明白C++中的const

    一文讓你徹底明白C++中的const

    這篇文章主要給大家介紹了關(guān)于C++中const的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • C語言實現(xiàn)磁盤映射

    C語言實現(xiàn)磁盤映射

    磁盤映射技術(shù)通過將文件映射到內(nèi)存中,提高了文件操作的效率,本文就來介紹一下C語言實現(xiàn)磁盤映射,感興趣的可以了解一下
    2024-09-09
  • 深入c++中臨時對象的析構(gòu)時機的詳解

    深入c++中臨時對象的析構(gòu)時機的詳解

    本篇文章對c++中臨時對象的析構(gòu)時機進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 變量定義與聲明的區(qū)別詳細解析

    變量定義與聲明的區(qū)別詳細解析

    外部變量(全局變量)的"定義"與外部變量的"聲明"是不相同的,外部變量的定義只能有一次,它的位置是在所有函數(shù)之外,而同一個文件中的外部變量聲明可以是多次的,它可以在函數(shù)之內(nèi)(哪個函數(shù)要用就在那個函數(shù)中聲明)也可以在函數(shù)之外(在外部變量的定義點之前)
    2013-09-09
  • C語言實現(xiàn)的猜數(shù)字小游戲

    C語言實現(xiàn)的猜數(shù)字小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)的猜數(shù)字小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C語言 詳細解析時間復雜度與空間復雜度

    C語言 詳細解析時間復雜度與空間復雜度

    算法復雜度分為時間復雜度和空間復雜度。其作用: 時間復雜度是度量算法執(zhí)行的時間長短;而空間復雜度是度量算法所需存儲空間的大小
    2022-04-04
  • C++ 將文件數(shù)據(jù)一次性加載進內(nèi)存實例代碼

    C++ 將文件數(shù)據(jù)一次性加載進內(nèi)存實例代碼

    這篇文章主要介紹了C++ 將文件數(shù)據(jù)一次性加載進內(nèi)存實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C語言利用EasyX繪制小企鵝表情包

    C語言利用EasyX繪制小企鵝表情包

    這篇文章主要為大家詳細介紹了C語言如何利用EasyX繪圖庫實現(xiàn)繪制可愛的小企鵝表情包,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-12-12

最新評論

沁水县| 赤峰市| 焦作市| 章丘市| 泰宁县| 容城县| 敖汉旗| 锦屏县| 邵东县| 南昌县| 化隆| 金川县| 邵武市| 图片| 梁河县| 仁布县| 开化县| 水富县| 甘南县| 花垣县| 乐东| 大英县| 宁武县| 枝江市| 天峻县| 香格里拉县| 乌拉特后旗| 开远市| 阳城县| 乐平市| 遂宁市| 鲜城| 巴林右旗| 荔浦县| 磐安县| 郴州市| 宁城县| 赤峰市| 海林市| 通化市| 绍兴县|