C language guessing game

Hello, friends. I'm Muxi Xi. You can call me Xiaomu 💕
Author home page: Muxi Xi's personal blog home page.🎉
Series column: c language practice.
🎉 Like friends, Xiaomu likes editing and typing code every day 🤭, Lose weight and become addicted to study. It is a great honor to share what I have learned with you, make progress with you, and become a qualified Book King. ✨ If there are errors in the article, welcome to the comment area ✏️ comment. Then start today's study! 😘
So that's all for today's study. Friends who think it's good can pay attention, praise or collect it! 😘 Thank you for your support. I hope you guys can test the following code by yourself. After all, the operation by yourself makes the memory more profound.


1. Preface

Today, Xiaomu takes you through the basic C language code to realize a simple word guessing game and become the king of volumes, starting with daily practice. ✨

2. Guessing game

First of all, when playing the game, we must explain the rules, so we need to write code to realize this step. ✏️

Code implementation of setting rules:

//Explain the rules, start playing the game by entering 1 and exit the game by entering 0.
#include<stdio.h>
void meau()//Because the code does not need to return a value, it is decorated with void
{
	printf("*******Welcome to the word guessing game******\n");
	printf("*****************************\n");
	printf("*********** 1.play **********\n");
	printf("*********** 0.exit **********\n");
	printf("*****************************\n");
}
int main()
{
	meau();
	return 0;
}

The real role of void lies in: (1) the limitation of function return; (2) Qualification of function parameters. ✏️ Void literally means "no type", void * is "no type pointer", and void * can point to any type of data.
Void is almost only used to "comment" and restrict programs, because no one has ever defined a void variable. Let's try to define it:
void a;
When compiling this line of statement, there will be an error and a prompt "allegal use of type 'void'". However, even if the compilation of void a does not go wrong, it has no practical significance.

After explaining the rules, you need to ask players for their choice 🙌, That is, the player decides whether to start the game or exit the game by outputting some numbers, which needs to be realized through code ✏️, You can consider using a loop to realize it. No matter whether the player starts the game or not, you need to cycle once first. Consider using the do... while loop and switch branch loop to judge the number entered by the player and make the corresponding code operation.

Choose whether to start the code implementation of the game:

void meau()
{
	printf("*******Welcome to the word guessing game******\n");
	printf("*****************************\n");
	printf("*********** 1.play **********\n");
	printf("*********** 0.exit **********\n");
	printf("*****************************\n");
}
void game()
{
	;
}
int main()
{
	int input = 0;
	do
	{
		meau();
		printf("Please enter a selection:>");
	    scanf("%d", &input);
		switch (input)
		{
		    case 1:
				game();
				break;
			case 0:
				break;
			default:
				printf("Input error, please re-enter\n");//When the player enters a non-1 or 0, remind the player to re-enter the number.
				break;
		}
	} 
	while (input);
	  return 0;
}

Default is to use this keyword in the program to provide a default method.
It is used with switch statement in c language.
Default indicates that the default value of this field is an empty string '' 🎉, That is, when the value of this field is not specified when inserting a record, it defaults to '' as the insertion value.

Want the computer to generate a random number every time you play 🎈, Then you need to use srand and rand().
1, rand()

The rand() function is used to generate random numbers. However, the internal implementation of rand() is realized by linear congruence method. It is a pseudo-random number. Due to its long period, it can be regarded as random in a certain range.

rand() will return a range from 0 to RAND_ Pseudo random number (integer) between Max (32767).

Before calling the rand() function, you can use the srand() function to set the random number seed. If the random number seed is not set, the rand() function automatically designs the random number seed as 1 when calling. If the random seed is the same, the random number generated each time will be the same.

The header file required for the rand() function is: < stdlib H> ✏️

rand() function prototype: int rand(void);

Use the rand() function to generate random integers within 1-100: int Number1 = rand()% 100;

2, srand()

The header file required by the srand() function is still: < stdlib H> ✏️

srand() function prototype: void srand (used int seed); ✏️

srand() is used to set the random number seed when rand() generates random numbers. The parameter seed is an integer. You can usually use the return value of time(0) or geypid(0) as seed.

Use rand() and srand() to generate random integers within 1-100: srand(time(0));

int number1 = rand() % 100;

3, A method of generating random integers within a specified range using rand() and srand()

Method of "modulo division + addition"

Because, for any number, 0 < = rand ()% (n-m + 1) < = N-M

Therefore, 0+m<=rand()% (n-m+1) +m<=n-m+m therefore, to generate the random number num within the range of [m,n], you can use:

int num=rand()%(n-m+1)+m;

rand()%(n-m+1)+m is a formula, which can be recorded for future reference.

For example, generate random integers of 10 ~ 30:

srand((int)time(0));

int a = rand() % (21)+10;

Code implementation of word guessing game:

#include<stdio.h>
#include<stdlib.h>
#include <time. h> / / time header file
void meau()
{
	printf("*******Welcome to the word guessing game******\n");
	printf("*****************************\n");
	printf("*********** 1.play **********\n");
	printf("*********** 0.exit **********\n");
	printf("*****************************\n");
}
void game()
{
	int guess = 0;
	int ret = rand() % 100;//Randomly generate numbers from 1 to 99
	while (1)//Keep the while loop true
	{
		printf("%Please enter a 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");
			break;
	}
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));//NULL is a NULL pointer / / let the computer follow the time and set a different starting point for the random number
	do
	{
		meau();
		printf("Please enter a selection:>");
	    scanf("%d", &input);
		switch (input)
		{
		    case 1:
				game();//Guess the whole logic of numbers
				break;
			case 0:
				break;
			default:
				printf("Input error, please re-enter\n");
				break;
		}
	} 
	while (input);
	  return 0;
}


In this way, we can realize a guessing game! 😘

3. Write at the end

Yours ❤️ Praise is the source of my creative power
Yours ✨ Collection is the direction of my struggle
Yours 🙌 Attention is my greatest support
Yours ✏️ Comments are a bright light for me to move forward
It's not easy to create. I hope you can support Xiaomu 😘

See you next time!

Tags: C

Posted by loci on Tue, 19 Apr 2022 03:21:05 +0300