Number guessing game C language

        Write some small games in C language, such as charades. We're going to write this game modularly, instead of cluttering the code with all the functionality in the main function (main). We guess numbers by computers that generate random numbers but do not print them.

        First, we need to print a game menu and select an operation. If you choose 1, you will continue, if you choose 0, you will exit. If you choose another number, it will prompt an error. This time to use the do...while statement. It is characterized by the implementation of a statement first, and then judge. When the value of the input is 0 (that is, false), the loop is exited. Then, we use the switch statement to call the corresponding function. We define the menu function to print the menu, as follows.


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

        The code in the switch section is as follows:

//do...while循环内的语句
        menu();
		printf("请选择:>");
		scanf("%d", &input);
		
		switch(input)
		{
			case 1:
				game();
				break;
			case 0:
				printf("退出\n");
				break;
			default:
				printf("error!\n");
				break;
		}

         Next is the main part of the game: the generation of random numbers. The random number generation requires the rand function. The usage of rand function is briefly introduced as follows:

        The header file is: #include<stdlib.h>;

        The function prototype is: int rand(void); Represents a random integer between 0 and RAND_MAX (RAND_MAX = 32767)

        For example, int r = rand() % 100 + 1; he means that a random number between 1 and 100 is randomly generated. To generate random numbers in other ranges, the method of "modulo division + addition" is required. The formula is as follows:

        Generate a random number num in the range of [m,n]: int num=rand()%(n-m+1)+m;

        It should be noted that rand does not generate random values, but pseudo-random values. When you run the program repeatedly, you may see that the rand function produces the same value. So before calling the rand() function, you can use the srand() function to set the random number seed. The usage of the srand function is briefly introduced as follows:

        The header file is: <stdlib.h>

        Function prototype: void srand (usigned int seed); used to set the random number seed when rand() generates random numbers. The parameter seed is an integer, and the return value of time(0) or getpid(0) can usually be used as seed.

        Interested can change the value in ( ) to see how the rand function produces random values.

        time() is the timestamp, and the header file is: #include<time.h>. This article does not introduce too much. This part of the code is as follows:

srand((unsigned int)time(NULL));
int r = rand() % 100 + 1;

        However, it should be noted that the srand function is placed in the main function. There is no need to re-generate randomly every time.

        The complete code is as follows.

//Guessing
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void game()
{
	//过程
	//生成随机数
	int guess = 0; 
	int r = rand() % 100 + 1;
	//printf("  %d\n", r);
	//猜数字
	while(1)
	{
		printf("猜数字:>");
		scanf("%d", &guess); 
		if(guess < r)
		{
			printf("小了!\n");
		}
		else if(guess > r)
		{
			printf("大了!\n");
		}
		else
		{
			printf("你猜对了!\n");
			break;
		}
	} 
}

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

int main(void)
{
	int input = 0;
	srand((unsigned int)time(NULL));//设置游戏的生成器
	do
	{
		//menu
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		
		switch(input)
		{
			case 1:
				game();
				break;
			case 0:
				printf("退出\n");
				break;
			default:
				printf("error!\n");
				break;
		}
	}while(input);
	
	return 0;
}

The result is as follows

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324103951&siteId=291194637