C++ implements a simple guessing number game


 Guess the number


Introduction to the mini game: The number guessing game isLet the game machine randomly generate a positive integer within 100, the user inputs a number to guess it, you need to write a program to automatically compare it with the randomly generated guessed number , and prompt that it is too large , is still small, equal means guessed. If guessed, end the program.


Functions implemented by mini games:

1.Set random numbers:Generate random numbers from 1 to 100

2.Exquisite menu:Let users understand how to operate the game

3.Five chances to guess the number:Only five chances to guess the number

4.Game login page:Enter the password to log in to the mini-game

5.Choose the operation by yourself:The user chooses to play the game or exit the game


 First come to our header file:

#include<iostream>//标准输入输出流
#include<malloc.h>//动态开辟内存
#include<assert.h>//断言
#include <Windows.h>//改变字体颜色
#include<ctime>//使用time函数
#include<conio.h>//使用getch函数

Enum constants:

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

Mini-game login system:

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<<"请输入登入密码:"<<endl;
	while ((count = _getch()) != '\r')
	{
		if (count == '\b')
		{
			i--;
			cout << "\b \b" << endl;
		}
		else
		{
			shuru[i++] = count;
			cout<<"*"<<flush;  //flush不换行的输出流
		}
	}
	shuru[i++] = '\0';
	if (strcmp(mima, shuru) == 0)//比较字符串的大小
	{
		cout << "\n密码正确,您已进入系统!" << endl;
	}
	else
	{
		cout << "密码错误,请重新输入!" << endl;
		exit(0);//关闭当前文件
	}
	system("pause");
	system("cls");
}

Our game session:

void game()
{
	int r = rand() % 100 + 1;//生成1到100之间的随机数
	int* ptr = (int*)malloc(sizeof(int));
	assert(ptr);//判断ptr的动态内存有没有开辟成功
	int count = 5;
	while (count--)
	{
		cout << "请输入你喜欢的数字>:" <<endl;
		cin >> *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;
}

Our action menu:

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

Our main function:

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

The following is the entire code for our guessing number game:


#include<iostream>
#include<malloc.h>
#include<assert.h>
#include <Windows.h>
#include<ctime>
#include<conio.h>
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<<"请输入登入密码:"<<endl;
	while ((count = _getch()) != '\r')
	{
		if (count == '\b')
		{
			i--;
			cout << "\b \b" << endl;
		}
		else
		{
			shuru[i++] = count;
			cout<<"*"<<flush;
		}
	}
	shuru[i++] = '\0';
	if (strcmp(mima, shuru) == 0)
	{
		cout << "\n密码正确,您已进入系统!" << endl;
	}
	else
	{
		cout << "密码错误,请重新输入!" << endl;
		exit(0);
	}
	system("pause");
	system("cls");
}
void game()
{
	int r = rand() % 100 + 1;
	int* ptr = (int*)malloc(sizeof(int));
	assert(ptr);
	int count = 5;
	while (count--)
	{
		cout << "请输入你喜欢的数字>:" <<endl;
		cin >> *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;
}

Guess you like

Origin blog.csdn.net/2301_79201049/article/details/135037422