Rand random number-guessing number game implementation

Game Requirements:

  • The computer automatically generates random numbers from 1 to 100
  • The player guesses the number. During the process of guessing the number, feedback is given as to whether the number is too big or too small according to the size of the guessed number. Until the correct answer is guessed, the game ends.

1. Game implementation ideas

1.1 Ability to automatically generate random numbers from 1 to 100

The rand function generates pseudo-random numbers, the time function serves as the “seed” of the rand function, and the srand function initializes the random number generator.

1.1.1 rand function prototype

//To use, you need to include the header file: #include

int rand (void);

The rand function returns a pseudo-random number in the range of 0~RAND_MAX. The size of RAND_MAX depends on the compiler implementation. On most compilers, it is 32797.

The rand function operates on a base value called “seed” to generate a random number. The rand function generates a pseudo-random number, which is passed Random numbers generated by a certain algorithm (a true random number cannot predict what the next value will be). To ensure that rand generates truly unpredictable random numbers, it is necessary to ensure that the seed changes.

In program summary, the time when the program is run is generally used as the seed, because the time changes all the time.

1.1.2 time function prototype

//Use the header function that needs to be included #include

time_t time(time_t* timer);

The time function will return the difference between 0:00:00 on January 1, 1970 and the current program running time, in seconds. The time difference returned is also called a “timestamp”.

1.1.2 srand function prototype

void srand (unsigned int seed);

Before calling the rand function in the program, call the srand function first, and set the seed of random numbers generated by the rand function through the function parameter seed. As long as the seed is changing, the random numbers generated by the rand function will also change.

1.2 Menu interface

Give game prompts, enter 1 to start the game, enter 0 to end the game, and there will be a prompt if you enter an error.

switch statement

1.3 Determine the correctness of the guessed number and provide answer tips based on the guessed number

Determine whether the guess number submitted by the user is correct. If the guess number is incorrect, whether it is greater than the correct answer or less than the correct answer.

if…else if branch statement

2. Implement the steps in sequence

2.1 Random number generation

Verify random number generation

Use the rand function in the game function at the beginning of the subsequent game to generate random numbers.

2.2 Menu interface settings

Use functions

//Menu function
void menu()
{
printf("******************************\\
");
printf("******* 1 . play ********\\
");
printf("************ 0 . exit ********\\
");
printf("******************************\\
");
\t
}
int main()
{
int input = 0;
//Use the return value of the time function to set the seed
//Because the parameter of srand is of unsigned int type, we cast the return value of the time function to type conversion
srand((unsigned int )time(NULL));

//scanf("%d", & amp;input);
do
{
menu(); //Menu function call

} while (input);
return 0;
}
Menu interface

2.3 Game starts

When the player inputs 1, the game starts. The game start function – the game function is called in the main function. The game function generates a random number, compares the input number with the answer, and gives a prompt.

void game()
{
int r = rand() % 100 + 1; //Generate random numbers from 1 to 100
int guess = 0;
while (1)
{
printf("Please guess the number:");
scanf("%d", & amp;guess);
if (guess > r)
{
printf("Guess it's big\\
");
}
else if (guess < r)
{
printf("Guess it's too small\\
");
}
else
{
printf("Congratulations, the answer is correct! The answer is: %d\\
", guess);
break;
}
\t\t\t
}
\t
}

3. Final rendering effect

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<time.h>

void menu() //menu function
{
printf("******************************\\
");
printf("******* 1 . play ********\\
");
printf("************ 0 . exit ********\\
");
printf("******************************\\
");
\t
}


void game() //game function
{
int r = rand() % 100 + 1; //Generate random numbers from 1 to 100
int guess = 0;
while (1)
{
printf("Please guess the number:");
scanf("%d", & amp;guess);
if (guess > r)
{
printf("Guess it's big\\
");
}
else if (guess < r)
{
printf("Guess it's too small\\
");
}
else
{
printf("Congratulations, the answer is correct! The answer is: %d\\
", guess);
break;
}
\t\t\t
}
\t
}


int main()
{
int input = 0;
//Use the return value of the time function to set the seed
//Because the parameter of srand is of unsigned int type, we cast the return value of the time function to type conversion
srand((unsigned int )time(NULL));
\t
do
{
menu();
printf("Please select: >");
scanf("%d", & amp;input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("Game over\\
");
break;
default:
printf("Input error, please re-enter!\\
");
}

} while (input);
return 0;
}

3.1 Add limit on number of responses

Give five chances to answer questions

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include

void menu()
{
printf(“******************************\\
“);
printf(“******* 1 . play ********\\
“);
printf(“************ 0 . exit ********\\
“);
printf(“******************************\\
“);

}

void game()
{
int r = rand() % 100 + 1; //Generate random numbers from 1 to 100
int guess = 0;
int count = 5;
while(count)
{
printf(“\\
You still have %d chances\\
“,count);
printf(“Please guess the number:”);
scanf(“%d”, & amp;guess);
if (guess > r)
{
printf(“Guess it’s big\\
“);
}
else if (guess < r)
{
printf(“Guess it’s too small\\
“);
}
else
{
printf(“Congratulations, the answer is correct! The answer is: %d\\
“, guess);
break;
}
count–;
}
if (count == 0)
{
printf(“The opportunity has been exhausted, you failed, the correct answer is: %d\\
“,r);
}
}

int main()
{
int input = 0;

//Use the return value of the time function to set the seed
//Because the parameter of srand is of unsigned int type, we cast the return value of the time function to type conversion
srand((unsigned int )time(NULL));
do
{
menu();
printf(“Please select: >”);
scanf(“%d”, & amp;input);
switch(input)
{
case 1:
game();
break;
case 0:
printf(“Game over\\
“);
break;
default:
printf(“Input error, please re-enter!\\
“);
}

} while (input);

return 0;
}

Newbies are starting to write, everyone is welcome to communicate in the comment area!

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194711 people are learning the system