栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++】猜单词终端小游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++】猜单词终端小游戏

【C++】猜单词终端小游戏 《C++游戏编程入门》案例

源码

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
	//set up
	const int MAX_WRONG = 8;//最大错误允许次数
	vector words;//可能要猜的单词个数
	words.push_back("GUESS");
	words.push_back("HANGMAN");
	words.push_back("DIFFICULT");

	srand(static_cast(time(0)));
	//static_cast将这个值转为unsigned int型
	// time(0)返回一个基于当前日期和时间的数字

	random_shuffle(words.begin(), words.end());//乱序容器内的元素

	const string THE_WORD = words[0];//将第一个单词赋给THE_WORD

	int wrong = 0;//玩家错误的次数

	string soFar(THE_WORD.size(), '-');//目前为止的单词,要猜单词变为同长的‘-’

	string used = "";

	cout << "Welcome wo HangMan,Good Luck!n";

	//main loop
	while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
	{
		cout << "nnYou have " << (MAX_WRONG - wrong)<<"incorrect guesses left.n";
		cout << "nYou've used the following letters:n" << used << endl;
		cout << "nSo far,the word is:n" << soFar << endl;

		//get player's guess
		char guess;
		cout << "nnEnter your guess: ";
		cin >> guess;//输入猜测
		guess = toupper(guess);//把小写字母转换成大写字母

		//猜测和累加猜测是否包含(重复猜测相同字符或字符串的提醒 "You've already guessed"
		while (used.find(guess) != string::npos)//查找字符串a是否包含子串b, 不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos
		{
			cout << "nYou've already guessed " << guess << endl;
			cout << "Enter your guess:";
			cin >> guess;
			guess = toupper(guess);//转换成大写是因为方便比较
		}

		used += guess;//每次将猜测加到used上

		//比较要猜测字符串和猜测字符包含与否
		if (THE_WORD.find(guess) != string::npos)
		{
			//包含的情况
			cout << "Thatis right!" << guess << " is in the word.n";
			//遍历要猜测的单词,将猜对的字符在实际位置中替换soFar中的位置
			for (int i = 0; i < THE_WORD.length(); ++i)
			{
				if (THE_WORD[i] == guess)
				{
					soFar[i] = guess;
				}
			}
		}
		else
		{
			//不包含的情况
			cout << "Sorry, " << guess << " isn't in the word.n";
			++wrong;
		}
	}

	//跳出循环后结束游戏的两种情况
	if (wrong == MAX_WRONG)
	{
		cout << "nYou've been hanged!";
	}
	else
	{
		cout << "nYou guessed it!";
	}
	cout << "nThe word was " << THE_WORD << endl;

	system("pause");
	return 0;

}

中文伪代码:
  • 声明一些要用的变量:单词向量,允许错误最大次数,猜对后的进度单词,要猜的单词,玩家已经错误的次数,已经使用的字符串
  • main loop主要循环体,重复读入猜的字符串,直到两种情况是满足跳出
  • 循环体内读入单词并转换为大写方便比较
  • 是否包含子串的重复提醒
  • 猜测的字符和正确字符的对比,并将已经猜对的字符在soFar上显示出来
  • 两个情况满足后跳出循环结束游戏对应的方式
小结

是STL应用的一个实例,但对照理解起来还是花了一段时间,确实基础功上弱了一点…
以下是除了STL以外的注意点:

  • string的一个构造函数:string soFar(THE_WORD.size(),'-'),方便快速生成重复字符的字符串
  • 小写转大写的方便比较 guess = toupper(guess); 需包含头文件
  • 字符串的包含比较 while (used.find(guess) != string::npos)参照:C++中string.find()函数与string::npos
  • cin的实现方式:在读取数个字符时自动循环了,差点没看明白…
  • srand(static_cast(time(0)))生成随机数种子:发现注释掉这一行直接不可以乱序了,下面的random_shuffle直接没有作用了,须在调用前声明随机数种子
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429713.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号