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

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

《C++游戏编程入门》案例

源码

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

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

	srand(static_cast<unsigned int>(time(0)));
	//static_cast<unsigned int>将这个值转为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 << "\n\nYou 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 << "\n\nEnter 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); 需包含头文件<cctype>
  • 字符串的包含比较 while (used.find(guess) != string::npos)参照:C++中string.find()函数与string::npos
  • cin的实现方式:在读取数个字符时自动循环了,差点没看明白…
  • srand(static_cast<unsigned int>(time(0)))生成随机数种子:发现注释掉这一行直接不可以乱序了,下面的random_shuffle直接没有作用了,须在调用前声明随机数种子

版权声明:本文为Courtship233原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。