C Language-Number Explosion Game

 question:

        Hello, welcome to Number Explosion. The system randomly generates a number. If you guess a higher number, you will be prompted to guess a higher number. If you guess a lower number, you will be prompted to guess a lower number.

Idea:

  1. Write down the general idea of ​​the game first
  2. First, there will be a menu, so first write a menu function to display the contents of the menu.
  3. Then select the options in the menu. Different options correspond to different functions, so this is best achieved with switch.
  4. If you want to keep playing and choose to exit before ending, then use a do while loop. Because the menu appears first and then the selection is made, so choose do while;
  5. If you choose to enter the game, enter the game function,
  6. The game function starts by randomly generating a number, and the rand() function generates numbers randomly. But because the range is too large, we want to generate numbers between 0 and 100, and take the modulus (i.e., take the remainder) of the randomly generated number %100. What we are looking for must be within 0-99, because if it is greater than 100, I will give it. Removed. Because we want 100 to appear, we add +1. Therefore, the overall statement is: int ret = rand() % 100 + 1;
  7. But the rand function requires a starting address of a random number. At this time, you need to use the srand() function, which will provide a starting address of a random number to ensure that it will not jump randomly. Inside the srand() function, a constantly changing quantity needs to be provided, and computer time is always changing. Therefore, it is most appropriate to use the time(NULL) function, and because time returns the time_t type, which is the long long type, but srand needs to be unsigned. Int type, so time is forced to be converted to srand((unsigned int)time(NULL)); it should be noted here that the starting position of the random number provided by srand can be called once, so just call it at the beginning of the main function.
  8. Then just play the game.

code show as below:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
	printf("********************\n");
	printf("1-开始游戏\n");
	printf("2-退出\n");
	printf("********************\n");
}
void game()
{
	int ret = rand()%100+1;
	printf("%d\n", ret);
	Sleep(50);
	system("cls");
	int n = 0;
	while (1)
	{
		printf("请输入你猜的数字\n");
		scanf("%d", &n);
		if (n == ret)
		{
			printf("我靠,牛皮,猜对了\n");
			printf("\n还想再来一局不,搏一搏,单车变摩托\n");
			printf("再来一局(1/0):");
			int choose = 0;
			scanf("%d", &choose);
			if (choose == 1)
			{
				game();
			}

			break;
		}
		else if (n < ret)
			printf("猜小了啊,老铁\n");
		else if (n > ret)
			printf("猜大了,铁子\n");
	}
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择选项\n");
		scanf("%d", &input);
		
		switch (input)
		{
			case 1:
				game();
				break;
			case 2:
				break;
			default:
				printf("选项不存在,请重新输入\n");
				break;
		}
	} while (input != 2);
}

The result is as follows:

Guess you like

Origin blog.csdn.net/m0_59844149/article/details/131434521