<> Figure guessing game

<> Rules of the game :

The number guessing game is to randomly generate a number , Then the player guesses the number , Judge whether it is correct
If you make a mistake, please prompt whether you guessed big or small

1. First generate a menu , Determine whether the player wants to play the game
for example :
void meun() { printf("**************************\n"); printf("****** 1. Start the game
******\n"); printf("****** 0. Exit the game ******\n"); printf(
"**************************\n"); } int main() { int input = 0; do { menu
Print the menu once per cycle meun(); scanf("%d", &input); Judge whether the player plays the game switch(input) { case 1: printf
(" Guess the number \n"); Implementation of test game break; case 0: printf(" sign out \n"); break; default: printf(
" Input error \n"); } } while (input); return 0; }
No problem above , Now start writing code for guessing numbers
2. The realization of guessing numbers
First, generate a random number , To use rand function , Generate a random number , The random number it generates is 0 reach 32767
Implementation of guessing numbers game void game() { int ret = rand(); printf("%d\n", ret); }
however rand The random numbers are the same every time

srand Function is to set a random starting point
however srand( ) It has to be a random value , Pass a message that is worth changing all the time , Then time changes all the time , As long as time changes, random values change
Then there is a concept of timestamp , You can check it

Here is a function called time The concept of timestamp can be implemented ,time Is an unsigned integer , To cast
time() The header file is <time.h>
srand() The header file for is <stdlib.h>
there NULL It means null pointer yes 0

void game() { srand((unsigned int)time(NULL)); starting point , But you don't have to reset the starting point every time you lose You can put it in the main function 2.
Generate random number int ret = rand()%100+1; here %100 The number is 0 reach 99,+1 namely 0 reach 100 Yes If not, yes 0 reach
32767 Number of printf("%d\n", ret); }
The next step is to judge the size of the value
void game() { int guess = 0; Enter number int ret = rand()%100+1; // Generate random number Judge size while (1
) { printf(" Please guess the number :"); scanf("%d", &guess); if (guess < ret) { printf(" Guess it's small \n"); }
else if (guess > ret) { printf(" Guess big \n"); } else { printf(" Congratulations, you guessed right \n"); break; } }
}

<> Guess the end of the number game
#include<stdlib.h> #include<time.h> void meun() { printf(
"**************************\n"); printf("****** 1. Start the game ******\n"); printf(
"****** 0. Exit the game ******\n"); printf("**************************\n"); } void game(
) { int guess = 0; //2. Generate random number int ret = rand()%100+1; // Generate random number //printf("%d\n",
ret); //3. Guess the number while (1) { printf(" Please guess the number :"); scanf("%d", &guess); if (guess < ret
) { printf(" Guess it's small \n"); } else if (guess > ret) { printf(" Guess big \n"); } else { printf(
" Congratulations, you guessed right \n"); break; } } } int main() { int input = 0; srand((unsigned int)time(
NULL)); do { // Print the menu once per cycle //1. menu meun(); printf(" Please select :"); scanf("%d", &input);
switch(input) { case 1: //printf(" Guess the number \n"); game(); break; case 0: printf(" sign out \n")
; break; default: printf(" Input error \n"); break; } } while (input); return 0; }
end !!!
Don't forget to like it !!

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq