[C language game--Guess the number]

Preface

The guessing number game is an interesting C language game that most of us will learn when learning C language. Below I will introduce the implementation and programming logic of the game in detail. While playing mini games, you can also have a better understanding of C language control statements.

Insert image description here

1. Game description

The computer randomly generates a number from 1 to 100 and the player guesses it. Players can set the number of guesses each time according to their needs. If you guess correctly within the set number of times, you will be prompted that you guessed correctly, otherwise you will be prompted that you guessed incorrectly.

2. Code implementation

2.1 Print menu

Before we play the game, we always print the menu and make selections. So choose do-while loop here

#include<stdio.h>
void menu()
{
    
    
	printf("***********************\n");
	printf("***  1.play  0.exit ***\n");
	printf("***********************\n");
}
int main()
{
    
    
	do
	{
    
    
		menu();
	} while (1);
	return 0;
}

Show results:
Insert image description here

2.2 Build the basic framework

Choose 1: Play the game;
Choose 0: Exit;
Choose other: Choose again

int main()
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			printf("玩游戏\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择:\n");
		}
	} while (input);
	return 0;
}

Show results:
Insert image description here

2.3 Play games

Since we want to play a guessing number game, after we select 1, we can't just print one to play the game, so let's actually implement the guessing number game.

2.3.1 Generate random numbers

How can we generate random numbers?
C language provides a function to generate random numbers

2.3.1.1rand()

Insert image description here

Looking at the documentation we find:

  • The rand function will return apseudorandom number, the range of this random number is between 0 and RAND_MAX. The size of RAND_MAX depends on the implementation of the compiler, but on most compilers it is 32767.
  • Before calling rand, use the srand function to seed the pseudo-random number generator.
  • The use of rand function requires a header file: stdlib.h

What does it mean? We might as well write code to test:
Run it and find that random numbers are indeed generated. But when I ran it for the second time, I found that the random number generated was the same as the first time. How could it work? Isn't this a BUG?

Insert image description here
Insert image description here

If we learn more about it, it is not difficult to find that in fact, the random numbers generated by the rand function are pseudo-random. The pseudo-random numbers are not real random numbers, but random numbers generated by a certain algorithm. number. A true random number cannot predict what the next value will be. The rand function is a random number generated by operating on a reference value called "seed".
The reason why the sequence of random numbers generated by each previous run of the program is the same is becauseThe default seed used by the rand function to generate random numbers is 1. If you want to generate different random numbers, the seed must be changed.

If the seed needs to change, why don’t we just make the seed a random value?
NO, big leak, special leak
If this is the case, it will be an endless cycle.

2.3.1.2srand()

What is the srand mentioned in the document just now?
Insert image description here

In the program, the srand function is called before calling the rand function. The parameter seed of the srand function is used to set the seed when the rand function generates random numbers. As long as the seed is changing, the random number generated each time will The number sequence also changes.
That meansIf the seed given to srand is random, rand can generate random numbers.; When generating random numbers, a random number is needed, which is another contradiction.

So what on the computer is constantly changing? --time

2.3.1.3time()

Insert image description here

  • The time function will return the current calendar time. In fact, it returns the difference between January 1, 1970, 0:00:00, and the current program running time, in seconds. if
  • If the parameter timer of time() is NULL, only the difference in time will be returned. The time difference returned by the time function is also called: timestamp

If this is the case, we can write the code like this:
Insert image description here
Insert image description here
In this way, we find that the numbers are random, and then our code will be easier to write.
The random number we need is between 1-100, so we can write the code like this:
Insert image description here

2.3.2game()

Generate random numbers

void game()
{
    
    
	srand((unsigned int)time(NULL));
	int random_number = rand() % 100 + 1;
	printf("%d\n", random_number);
}

The effect is as follows:

Insert image description here
But if you are an experienced driver and have faster hand speed, the random numbers generated will be the same, as follows:
Insert image description here
Why is this?
Because when weRunning game() once will call srand() once, so calling it in a shorter time will produce the same number..
Therefore, the srand function does not need to be called frequently. It is enough to call it once in a running program.
Therefore, we can put it in main()

void game()
{
    
    
	int random_number = rand() % 100 + 1;
	int number = 0;
	while (1)
	{
    
    
		printf("请猜数字:");
		scanf("%d", &number);
		if (number > random_number)
		{
    
    
			printf("您猜大了\n");
		}
		else if (number < random_number)
		{
    
    
			printf("您猜小了\n");
		}
		else
		{
    
    
			printf("恭喜你,猜对了!\n");
			break;
		}
	}
}

2.4 Set the number of guesses yourself

The user enters the number of times himself. If the guess is not correct within the set number of times, a failure message will be displayed
The code changes are as follows:

switch (input)
		{
    
    
		case 1:
			printf("你感觉你几次就能猜对呢?\n");
			scanf("%d", &count);
			game(count);
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择:\n");
		}
void game(int count)
{
    
    
	int random_number = rand() % 100 + 1;
	int number = 0;
	while (count)
	{
    
    
		printf("请猜数字:");
		scanf("%d", &number);
		if (number > random_number)
		{
    
    
			printf("您猜大了\n");
		}
		else if (number < random_number)
		{
    
    
			printf("您猜小了\n");
		}
		else
		{
    
    
			printf("恭喜你,猜对了!\n");
			break;
		}
		count--;
	}
	if (count == 0)
	{
    
    
		printf("你未在规定的次数内猜对,有点高估自己了,还得练哦\n");
	}
}

The effect is as follows:
Insert image description here
Insert image description here

3. Complete code

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
    
    
	printf("***********************\n");
	printf("***  1.play  0.exit ***\n");
	printf("***********************\n");
}
void game(int count)
{
    
    
	int random_number = rand() % 100 + 1;
	int number = 0;
	while (count)
	{
    
    
		printf("请猜数字:");
		scanf("%d", &number);
		if (number > random_number)
		{
    
    
			printf("您猜大了\n");
		}
		else if (number < random_number)
		{
    
    
			printf("您猜小了\n");
		}
		else
		{
    
    
			printf("恭喜你,猜对了!\n");
			break;
		}
		count--;
	}
	if (count == 0)
	{
    
    
		printf("你未在规定的次数内猜对,有点高估自己了,还得练哦\n");
	}
}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	int input = 0;
	int count = 0;
	do
	{
    
    
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			printf("你感觉你几次就能猜对呢?\n");
			scanf("%d", &count);
			game(count);
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择:\n");
		}
	} while (input);
	return 0;
}

That’s it for this sharing, thank you for watching
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_69380220/article/details/133975986