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;
}
 
 

到此這篇關於C++ 實戰開發一個猜單詞的小遊戲的文章就介紹到這瞭,更多相關C++ 猜單詞內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: