C++实现简单的猜数字小游戏


 猜数字

C++实现简单的猜数字小游戏_第1张图片


小游戏介绍:猜数字游戏是令游戏机随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了,还是小了,相等表示猜到了。如果猜到,则结束程序。


小游戏实现的功能:

1.设置随机数生成1到100的随机数

2.精美的菜单让用户明白怎么操作游戏

3.五次猜机会只有五次猜数字的机会

4.游戏登入页面输入密码登入小游戏

5.自行选择操作用户自行选择玩游戏或者退出游戏


 首先来到我们的头文件:

#include//标准输入输出流
#include//动态开辟内存
#include//断言
#include //改变字体颜色
#include//使用time函数
#include//使用getch函数

枚举常量:

enum Menu
{
	Quit = 0,
	Play = 1
};

小游戏的登入系统 :

void menu2()
{
	int input = 0, count = 0, i = 0;
	char mima[20] = "123";
	char shuru[20] = { 0 };
	system("color F4");
	cout << "\t\t\t     **************************************" << endl;
	cout << "\t\t\t     |       *欢迎来到猜数字小游戏*       |" << endl;
	cout << "\t\t\t     |           *            *           |" << endl;
	cout << "\t\t\t      ------------------------------------ " << endl;
	cout<<"请输入登入密码:"<

我们的游戏环节:

void game()
{
	int r = rand() % 100 + 1;//生成1到100之间的随机数
	int* ptr = (int*)malloc(sizeof(int));
	assert(ptr);//判断ptr的动态内存有没有开辟成功
	int count = 5;
	while (count--)
	{
		cout << "请输入你喜欢的数字>:" <> *ptr;
		if (*ptr > r)
		{
			cout << "你还有" << count << "次机会" << endl;
			cout << "猜大啦!" << endl;
		}
		else
		{
			cout << "你还有" << count << "次机会" << endl;
			cout << "猜小啦!" << endl;
		}
		if (*ptr == r)
		{
			cout << "恭喜你,猜对了!" << endl;
			break;
		}
		if (count == 0)
		{
			cout << "很遗憾,失败了,正确的值为" << r << endl;
		}
	}
	free(ptr);//释放空间
	ptr = NULL;
}

我们的操作菜单:

void menu1()
{
	system("color F4");
	cout << "|----------------------------------|" << endl;
	cout << "|----------*猜数字游戏*------------|" << endl;
	cout << "|------------1.Play----------------|" << endl;
	cout << "|------------0.Quit----------------|" << endl;
	cout << "|----------------------------------|" << endl;
}

我们的main函数:

int main()
{
	menu2();
	srand((unsigned int)time(NULL));//添加随机数种子,利用当前时间作为随机数,防止每次随机数都一样
	int* input = (int*)malloc(sizeof(int));
	assert(input);//判断input的动态内存有没有开辟成功
	do {
		system("cls");//清空控制台
		menu1();
		cout << "请输入当前操作:" << endl;
		cin >> *input;
		switch (*input)
		{
		case Quit://枚举类型美化选项
			cout << "--------*您已退出游戏*--------" << endl;
			system("pause");
			break;
		case Play:
			game();
			system("pause");
			break;
		default:
			cout << "-----------*输入错误,重新输入*-----------" << endl;
			cin >> *input;
			system("pause");
		}
	} while (*input);
	free(input);//释放空间
	input = NULL;
	return 0;
}

以下就是我们猜数字小游戏的整个代码啦:

C++实现简单的猜数字小游戏_第2张图片


#include
#include
#include
#include 
#include
#include
using namespace std;
enum Menu
{
	Quit = 0,
	Play = 1
};
void menu2()
{
	int input = 0, count = 0, i = 0;
	char mima[20] = "123";
	char shuru[20] = { 0 };
	system("color F4");
	cout << "\t\t\t     **************************************" << endl;
	cout << "\t\t\t     |       *欢迎来到猜数字小游戏*       |" << endl;
	cout << "\t\t\t     |           *            *           |" << endl;
	cout << "\t\t\t      ------------------------------------ " << endl;
	cout<<"请输入登入密码:"<:" <> *ptr;
		if (*ptr > r)
		{
			cout << "你还有" << count << "次机会" << endl;
			cout << "猜大啦!" << endl;
		}
		else
		{
			cout << "你还有" << count << "次机会" << endl;
			cout << "猜小啦!" << endl;
		}
		if (*ptr == r)
		{
			cout << "恭喜你,猜对了!" << endl;
			break;
		}
		if (count == 0)
		{
			cout << "很遗憾,失败了,正确的值为" << r << endl;
		}
	}
	free(ptr);
	ptr = NULL;
}
void menu1()
{
	system("color F4");
	cout << "|----------------------------------|" << endl;
	cout << "|----------*猜数字游戏*------------|" << endl;
	cout << "|------------1.Play----------------|" << endl;
	cout << "|------------0.Quit----------------|" << endl;
	cout << "|----------------------------------|" << endl;
}
int main()
{
	menu2();
	srand((unsigned int)time(NULL));
	int* input = (int*)malloc(sizeof(int));
	assert(input);
	do {
		system("cls");
		menu1();
		cout << "请输入当前操作:" << endl;
		cin >> *input;
		switch (*input)
		{
		case Quit:
			cout << "--------*您已退出游戏*--------" << endl;
			system("pause");
			break;
		case Play:
			game();
			system("pause");
			break;
		default:
			cout << "-----------*输入错误,重新输入*-----------" << endl;
			cin >> *input;
			system("pause");
		}
	} while (*input);
	free(input);
	input = NULL;
	return 0;
}

C++实现简单的猜数字小游戏_第3张图片

C++实现简单的猜数字小游戏_第4张图片

你可能感兴趣的:(c++,开发语言)