亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言

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

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

效果展示

一、函數(shù)接口

enum
{
	EXIT=0,
	START,
	CHECK,
	CLEAR
};
 
//玩家結(jié)構(gòu)體聲明
typedef struct 
{
	string name;
	int right;//猜對單詞個(gè)數(shù)
	int wrong;//猜錯(cuò)個(gè)數(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]);//打開單詞文檔,導(dǎo)入到char數(shù)組中
void Clear();//清空玩家名單
int GuessWordNum(int& GWN);//設(shè)置猜單詞的數(shù)量
string InputName(string& name);//輸入玩家的姓名
void Sort(vector<GamePlayer>& v);//將vector數(shù)組中的玩家按分?jǐn)?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è)置隨機(jī)數(shù),放入到FiIe數(shù)組中
		srand(time(NULL));//設(shè)置一個(gè)隨機(jī)種子
		Rand = rand() % 199;//隨機(jī)取出單詞
		cout << "————————您一共有10次猜的機(jī)會——————" << 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 << "對不起,您猜錯(cuò)了" << endl;
					cout << "您還有" << chance << "次機(jī)會,請好好把握" << endl;
				}
				else
				{
					cout << "對不起,本輪您已經(jīng)沒有機(jī)會了" << endl;
					cout << "很遺憾,沒猜出..." << endl;
					cout << "正確單詞為" << File[Rand] << endl;
					break;
				}
			}
		}
		GWN--;
		if (flag == -1)
		{
			wrong++;
		}
		if (GWN == 0)
		{
			cout << "您是否要進(jìn)行下一輪游戲" << endl;
			cout << "如果確定請輸入Y,如果退出請按任意鍵" << endl;
			char s;
			cin >> s;
			if (s == 'y' || s == 'Y')
			{
				cout << "請輸入您要猜單詞的個(gè)數(shù)" << endl;
				int i = 0;
				cin >> i;
				GWN = i;
			}
			else
			{
				int score = 20 * right - 10 * wrong;
				cout << "本輪游戲您一共猜對了" << right << "個(gè)單詞" << "猜錯(cuò)了" << wrong << "個(gè)單詞" << 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 << "對不起,暫時(shí)沒有排名" << endl;
		cout << endl;
		cout << endl;
		cout << endl;
		cout << endl;
		cout << endl;
	}

6.清空玩家排名

void Clear()
{
	cout << "您確定要?jiǎng)h除所有玩家的記錄嗎?" << 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.玩家排名

這里對玩家的分?jǐn)?shù)進(jìn)行排序,利用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;//猜對單詞個(gè)數(shù)
	int wrong;//猜錯(cuò)個(gè)數(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]);//打開單詞文檔,導(dǎo)入到char數(shù)組中
void Clear();//清空玩家名單
int GuessWordNum(int& GWN);//設(shè)置猜單詞的數(shù)量
string InputName(string& name);//輸入玩家的姓名
void Sort(vector<GamePlayer>& v);//將vector數(shù)組中的玩家按分?jǐn)?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 << "  " << "答錯(cuò):" << e.wrong << "得分:" << "  "
			<< e.score << " " << endl;
		}
 
	}
	else
	{
		cout << "對不起,沒有這個(gè)排名本" << 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è)置隨機(jī)數(shù),放入到FiIe數(shù)組中
		srand(time(NULL));//設(shè)置一個(gè)隨機(jī)種子
		Rand = rand() % 199;//隨機(jī)取出單詞
		cout << "————————您一共有10次猜的機(jī)會——————" << 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 << "對不起,您猜錯(cuò)了" << endl;
					cout << "您還有" << chance << "次機(jī)會,請好好把握" << endl;
				}
				else
				{
					cout << "對不起,本輪您已經(jīng)沒有機(jī)會了" << endl;
					cout << "很遺憾,沒猜出..." << endl;
					cout << "正確單詞為" << File[Rand] << endl;
					break;
				}
			}
		}
		GWN--;
		if (flag == -1)
		{
			wrong++;
		}
		if (GWN == 0)
		{
			cout << "您是否要進(jìn)行下一輪游戲" << endl;
			cout << "如果確定請輸入Y,如果退出請按任意鍵" << endl;
			char s;
			cin >> s;
			if (s == 'y' || s == 'Y')
			{
				cout << "請輸入您要猜單詞的個(gè)數(shù)" << endl;
				int i = 0;
				cin >> i;
				GWN = i;
			}
			else
			{
				int score = 20 * right - 10 * wrong;
				cout << "本輪游戲您一共猜對了" << right << "個(gè)單詞" << "猜錯(cuò)了" << wrong << "個(gè)單詞" << 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 << "對不起,暫時(shí)沒有排名" << endl;
		cout << endl;
		cout << endl;
		cout << endl;
		cout << endl;
		cout << endl;
	}
 
 
 
}
 
 
void Clear()
{
	cout << "您確定要?jiǎng)h除所有玩家的記錄嗎?" << 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++ 實(shí)戰(zhàn)開發(fā)一個(gè)猜單詞的小游戲的文章就介紹到這了,更多相關(guān)C++ 猜單詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言sqrt函數(shù)的實(shí)例用法講解

    C語言sqrt函數(shù)的實(shí)例用法講解

    在本篇文章里小編給大家整理的是關(guān)于C語言sqrt函數(shù)的實(shí)例內(nèi)容以及用法詳解,需要的朋友們可以參考下。
    2020-02-02
  • C語言編寫簡單拼圖游戲

    C語言編寫簡單拼圖游戲

    這篇文章主要為大家詳細(xì)介紹了C語言編寫簡單拼圖游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C語言三子棋小游戲的實(shí)現(xiàn)

    C語言三子棋小游戲的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了C語言三子棋小游戲的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C與C++中結(jié)構(gòu)體的區(qū)別

    C與C++中結(jié)構(gòu)體的區(qū)別

    C中的結(jié)構(gòu)體只涉及到數(shù)據(jù)結(jié)構(gòu),而不涉及到算法,也就是說在C中數(shù)據(jù)結(jié)構(gòu)和算法是分離的,而到C++中一類或者一個(gè)結(jié)構(gòu)體可以包含函數(shù)(這個(gè)函數(shù)在C++我們通常中稱為成員函數(shù)),C++中的結(jié)構(gòu)體和類體現(xiàn)了數(shù)據(jù)結(jié)構(gòu)和算法的結(jié)合
    2013-10-10
  • C語言各類操作符全面講解

    C語言各類操作符全面講解

    C?語言提供了豐富的操作符,有:算術(shù)操作符,移位操作符,位操作符,賦值操作符,單目操作符,關(guān)系操作符,邏輯操作符,條件操作符等。接下了讓我們詳細(xì)了解掌握它
    2022-05-05
  • C++11 強(qiáng)類型枚舉相關(guān)總結(jié)

    C++11 強(qiáng)類型枚舉相關(guān)總結(jié)

    這篇文章主要介紹了C++11 強(qiáng)類型枚舉的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c++11,感興趣的朋友可以了解下
    2021-02-02
  • OpenCV鼠標(biāo)繪制矩形和截取矩形區(qū)域圖像

    OpenCV鼠標(biāo)繪制矩形和截取矩形區(qū)域圖像

    這篇文章主要為大家詳細(xì)介紹了OpenCV鼠標(biāo)繪制矩形和截取矩形區(qū)域圖像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 利用C語言繪制一個(gè)正方體

    利用C語言繪制一個(gè)正方體

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言繪制一個(gè)正方體,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)和借鑒價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-01-01
  • C++的對象特性和友元你真的了解嗎

    C++的對象特性和友元你真的了解嗎

    這篇文章主要為大家詳細(xì)介紹了C++的對象特性和友元,使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C++14中binary literals的使用詳解

    C++14中binary literals的使用詳解

    這篇文章主要介紹了C++14中binary literals的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06

最新評論