catalogue
1, It is expected to realize the function of guessing numbers game
2, Explanation of specific ideas
2. Realization of menu function
3. Implementation of main function
preface
For c language beginners, using c language to realize the number guessing game is a basic problem. This article will explain in detail how to use code to realize the number guessing game. I hope readers can learn to play branches and loops and the most basic c language grammar before reading this blog.
Tip: the following is the main content of this article. The following cases can be used for reference
1, It is expected to realize the function of guessing numbers game
1: What is a number guessing game
As the name suggests, the number guessing game is a computer randomly generates a number for players to guess. If you don't guess right, the computer will prompt the player to guess big or small. The player can try many times until the player guesses right, and the game will end.
2: Expected effect
On the basis of guessing numbers, players can choose to do it again if they are not satisfied. If they are tired, they can also choose to quit the game
2, Explanation of specific ideas
1. General idea
In order to realize the basic functions of the game, we should have a game menu. Through the menu, players can choose whether to enter the game or exit the game. In order to make the game play repeatedly, this process should be a cycle. When you choose to exit the game, you will break this cycle. In order to make the code organized, we define a unique main function, and then write sub functions according to the function, so that each sub function has its meaning. To sum up, we need to print a sub function of the menu module and the sub function of the game implementation (including the function of computer generating random numbers and players can guess numbers repeatedly). We might as well put whether to choose to enter the game in the main function.
2. Realization of menu function
Because we put the selection process in the main function, the menu function is very simple. The code example is as follows
void menu() { printf("*****Welcome to the number guessing game*****\n"); printf("********1: Enter the game*********\n"); printf("********0: Exit the game*********\n"); printf("****************************\n"); }
3. Implementation of main function
Because the program runs from the main function to the end of the main function, the main function should play the function of building the basic framework of the whole game. The main function should not only realize whether to choose to enter the game, but also reasonably reference menu () and game () to form an organic whole of the whole program.
int main() { srand((unsigned)time(NULL));//Define a randomly generated number. Be careful not to put it in the loop statement, otherwise the generated number will not be random enough int input = 0; do { menu(); printf("Please enter your choice:"); scanf("%d",&input); if (input == 1) { game();//If you choose to play a game, call the game() function to realize the game function } else if (input == 0) { printf("The game has exited"); break;//This sentence is not necessary because while determines the condition } else { printf("The instruction you entered is invalid. Please re-enter it\n");//Because only 1 and 0 are meaningful, but players may enter other numbers. You should be prompted here } } while (input != 0); return 0; }
4. Code implementation of game() function
In game (), we want to realize two functions: 1. The computer randomly generates a random number between 1 and 100. 2: The principle of generating random numbers in c language is complex. You need to define a starting point for generating random numbers, and then generate random numbers with rand() function.
The code is as follows:
void game() { //1: Realize the generation of random numbers between 1-100 int random_num = rand()%100+1;//rand() is a random number generating function, which should be used in conjunction with srand((unsigned)time(NULL)) //2: Enter the player guessing numbers link while (1) { int guess = 0;//guess stores the player's guesses printf("Please enter the guessed number:"); scanf("%d", &guess); if (guess < random_num) printf("Guess it's small\n"); else if (guess > random_num) printf("Guess big\n"); else { printf("Congratulations, you guessed right\n"); break;//In order to prevent a dead cycle, if you guess right, break the cycle } } }
The header file to be referenced by game is #include < stdlib h>
srand((unsigned)time(NULL)) the header file to be referenced is #include < time h>
5. Total program code
As follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> //Rules of the game //1: The computer randomly generates a number between 1 and 100 //2: The player guesses, and the digital computer will prompt the player to guess big or small until the player guesses right and the game is over //3: The game can be played repeatedly, that is, after playing one, the player can choose to play another or exit directly void menu() { int input = 0; printf("*****Welcome to the number guessing game*****\n"); printf("********1: Enter the game*********\n"); printf("********0: Exit the game*********\n"); printf("****************************\n"); } void game() { //1: Realize the generation of random numbers between 1-100 int random_num = rand()%100+1;//rand() is a random number generating function, which should be used in conjunction with srand((unsigned)time(NULL)) //2: Enter the player guessing numbers link while (1) { int guess = 0;//guess stores the player's guesses printf("Please enter the guessed number:"); scanf("%d", &guess); if (guess < random_num) printf("Guess it's small\n"); else if (guess > random_num) printf("Guess big\n"); else { printf("Congratulations, you guessed right\n"); break;//In order to prevent a dead cycle, if you guess right, break the cycle } } } int main() { srand((unsigned)time(NULL)); int input = 0; do { menu(); printf("Please enter your choice:"); scanf("%d",&input); if (input == 1) { game(); } else if (input == 0) { printf("The game has exited"); break; } else { printf("The instruction you entered is invalid. Please re-enter it\n"); } } while (input != 0); return 0; }
III Common errors
1. In game (), put the random number statement segment in the while loop, resulting in a change in the random number every time you guess during the game. If you guess wrong for the first time, you will always guess wrong.
2. After guessing a number in game (), the if judgment condition is written incorrectly, resulting in that the original guess is big and the computer shows that the guess is small. The original guess is small and the guess is big. In this way, you will never guess correctly unless you guess correctly for the first time.
I am a beginner of c language. There may be some mistakes. I hope you can correct me immediately. Thank you.