“This is day 9 of my participation in the First Wen Challenge 2022.First challenge in 2022″I believe we have played guess numbers this amateur small game, we will use the simplest C language code today to achieve it, the blogger package you learn, remember to like collection.

@TOC

Achieve the goal

  • The menu
  • First of all, the number guessing game must have a random number
  • You can play many games over and over again
  • Guess the number to indicate (big, small, right)

The menu

Most games have a menu interface that asks the player to play or quit! We packaged it as a function menu to prompt the player.

void menu(a)
{
	printf("**************************\n");
	printf("****** 1.paly *******\n");
	printf("****** 0.exit *******\n");
	printf("**************************\n");
	printf("Please select (1/0) >");
}
Copy the code

Tectonic framework

How to play the game repeatedly, the first thing we think of is to use the loop while for do while to achieve the purpose of repeating the game

The blogger chose do while

Do while can be used to enter the game once before judging whether it is a game, because we need to put the menu of game selection in the loop, and ask the player to play or exit every time we play the game

Do while implements repeated play, but how to determine play or exit? We can use select statements, if else,switch, because there are only 3 cases 1. Enter the game 2. Exit the game 3. Wrong choice here to choose switch more intuitive we set 1 to enter the game, 0 to exit the game just fit do while 0 to exit the loop code is as follows


int main(a)
{
	int input = 0; 
	do
	{   
		menu();   / / the menu
	   scanf("%d",&input);  // For the player to choose
	   switch (input)      // Enter the branch
	   {
	   case 1:game();  / / 1, the game
		   break;
	   case 0:printf("Quit the game \n");  / / 0, exit
		   break;
	   default:printf("Wrong input, please retype \n"); }}while (input);
	
	return 0;
}
Copy the code

The game to achieve

Now that we’re in the game, we can guess the numbers

We always set up a random number for players to guess before each game! How do YOU set random numbers in C? C has a function that generates random numbers

rand

int rand (void); <stdlib.h>

Generate random number Returns a pseudo-random integral number in the range between 0 and RAND_MAX (32767)

Rand function details link

The return valueintNo parameter, contained instdlib.hYou can see that in the libraryrandCan generate a0~RAND_MAXRandom number of.rand100 random numbers were generated. There seems to be no problem.But when I run it again, it generates the same random number, so let’s read it a little bit more carefullyrandfunction

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.

You can see that with rand, you also use the srand function. srand void srand (unsigned int seed);

Initialize the random number generator

Initialize the pseudorandom number generator with the argument passed as seed. For each different seed value used in the call to RAND, the pseudorandom number generator can be expected to produce a different result sequence in subsequent calls to RAND.

Srand details

You can seesrandNo return value, argumentunsigned intCan be used as arandInitialize the random number generator to generate different result sequences. Again, the problem is that we input different parameters tosrandIn order to achieve each random number generation! (O O ױ omega)! Our goal is to generate random numbers.Where are we gonna find random numbers?!

order to generate random-like numbers, srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime>). This is distinctive enough for most trivial randomization needs.

A closer look at the srand function reveals another time function

time time_t time (time_t* timer); time.hThe return valuetime_t, the parametertime_t* Under VS we can see that,time_tThe type oflong long

Get the current time

Gets the current calendar time with a value of type time_t. The function returns this value, and if the argument is not a null pointer, it sets this value to the object that the timer points to.

The value returned usually represents the number of seconds since 00:00 hours of UTC, January 1, 1970 (the current Unix timestamp)! The time stamp

The timestamp is the difference between the current time and 00:00, January 1, 1970. So we can know that different times have unique time stamps, so our random number can be replaced by time stamps.

Comparing the above code, we can see that the starting point of the random number generator,srandJust use build once, so we can put,mainIn the function, it is guaranteed to be executed only once.

How do we set the range of random numbers? If we want to generate random numbers from 1 to 100, we use rand%100+1.

int x=rand()%100+1;

Copy the code

Then we will have to guess the number, may guess big, guess small, guess right, to prompt the player. You can do this with an if else structure.

Part of the game code

void game(a)
{
  int x=rand()%100+1;  
  // Each game generates a random number from 1 to 100,
 // Do not put in loop parts to cause bugs
while(1)
{  
   int gass=0;
  scanf("%d",&gass);  / / guess Numbers
  if(x>gass)   // The number is greater than the number you guess
  {
    printf("Guess too small \n");
  }
  else if(x<gass)
  {
    printf("Big guess \n");
  }
  else 
  {
    printf("Congratulations, you guessed right \n");
    break;  // If you guess correctly, the game ends and exits the loop}}}Copy the code

All the code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

void menu(a)
{
	printf("**************************\n");
	printf("****** 1.paly *******\n");
	printf("****** 0.exit *******\n");
	printf("**************************\n");
	printf("Please select (1/0) >");
}
void game(a)
{
	int x = rand()%100+1; 
	 // Each game generates a random number from 1 to 100,
     // Do not put in loop parts to cause bugs
	while (1)
	{
		int gass = 0;
		printf("Please enter the number you want to guess (1-100)>");
		scanf("%d", &gass);  / / guess Numbers
		if (x > gass)   // The number is greater than the number you guess
		{
			printf("Guess too small \n");
		}
		else if (x < gass)
		{
			printf("Big guess \n");
		}
		else
		{
			printf("Congratulations, you guessed right \n");
			break;  // If you guess correctly, the game ends and exits the loop}}}int main(a)
{
	// Guess the number game

	srand((unsigned int)time(NULL));  // Sets the random generator starting point
	int input = 0;
	do
	{
		menu();
	   scanf("%d",&input);
	   switch (input)
	   {
	   case 1:game();
		   break;
	   case 0:printf("Quit the game \n");
		   break;
	   default:printf("Wrong input, please retype \n"); }}while (input);
	
	return 0;
}
Copy the code

Does it smell like that? It’s a la carte, but it’s a little game.

The blogger has orders, so it can only be written like this. The blogger has been learning and has something to comment on, looking forward to your attention and praise.