catalogue
1. Guess the operation logic of the number game
1. Print out the menu for users to choose
2. Build the logic of the game
2.1 create and call a GAME function
2.2 the system outputs a random number
2.3 users input data and judge whether the data is right or wrong
Understanding this will make the code fly
1.1 print out the menu for users to choose
First, create a MENU function yourself
The menu must realize the function of the menu, such as whether to start the game or exit the game, and ask once at the end of each game
So here we use the do while () loop to realize each query and selection
#include <stdio.h> void MENU() { printf("*******************\n"); printf("**1.play***2.exit**\n"); //Print out the menu for the user to choose 1 to play the game and 0 to exit printf("*******************\n"); } int main() { int input=0; do { MENU(); //Call this function to print out the menu scanf("%d",&input); //The user chooses to start the game or exit the game switch(input) //Judge the content entered by the user { case 1: { printf("The game begins. Please enter the number you want to guess\n"); break; } case 0: { printf("game over\n") break; } default: { printf("Input error, please re-enter") } }while(input) //Here is the judgment. If it is 0, the loop will end. If it is non-0, the loop will continue } }
1.2 build game logic
1.2.1 Create and call a game function
#include <stdio.h> void GAME()//Create a function { } void MENU() { printf("*******************\n"); printf("**1.play***2.exit**\n"); printf("*******************\n"); } int main() { int input=0; do { MENU(); scanf("%d",&input); switch(input) { case 1: { printf("The game begins. Please enter the number you want to guess\n"); GAME();//Call a GAME function break; } case 0: { printf("game over\n") break; } default: { printf("Input error, please re-enter") } }while(input) } }
1.2.2 Let the system generate a random number (the rand () function generates a random number)
Note: the random number called by rand is the same (pseudo-random number) every time the program is executed. Therefore, a random number should be initialized by calling the srand() function, and the parameter required by srand is an unsigned random number. Where do unsigned random numbers come from? This leads to the concept of timestamp Header file #include < stdlib. H >
Timestamp: it is a number converted from the difference between the current time and the time when the computer was born. With the change of time second by second, the timestamp is constantly changing. Therefore, the parameters conforming to srand() function (because srand() function needs an unsigned integer, the timestamp should be forcibly converted) are called with time, and the header file #include < time. H >
#include <stdio.h> void GAME() { int r= rand();//Call a random function and save it with r to facilitate future judgment } void MENU() { printf("*******************\n"); printf("**1.play***2.exit**\n"); printf("*******************\n"); } int main() { srand((unsigned int)time(null));//Initialize random function int input=0; do { MENU(); scanf("%d",&input); switch(input) { case 1: { printf("The game begins. Please enter the number you want to guess\n"); GAME(); break; } case 0: { printf("game over\n") break; } default: { printf("Input error, please re-enter") } }while(input) } }
1.2.3 the user inputs the guessed number and judges
#include <stdio.h> #include <time.h> #include <stdlib.h> void GAME() { int r = rand()%100;//%100 is to limit the random number to 100 int i = 0; while (1)//Because it is impossible to guess right at one time, if you guess wrong, you need to enter the loop and continue to guess. If you guess right, you will jump out of the loop { scanf("%d", &i); //Enter the number guessed by the user if (i > r)//Judge the number guessed by the user and give the result { printf("Guess big\n"); } else if (i == r) { printf("Congratulations, you guessed right\n"); break; } else { printf("Guess it's small\n"); } } } void MENU() { printf("*******************\n"); printf("**1.play***2.exit**\n"); printf("*******************\n"); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { MENU(); scanf("%d", &input); switch (input) { case 1: { printf("The game begins. Please enter the number you want to guess\n"); GAME(); break; } case 0: { printf("game over\n"); break; } default: { printf("Input error, please re-enter"); } } } while (input); }