当前位置:网站首页>C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)
C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)
2022-07-27 07:35:00 【Programming enthusiast - Axin】
List of articles
- C Language implementation guessing numbers game project practice ( be based on srand function 、rand function ,Switch sentence 、while loop 、if Condition criteria, etc )
C Language implementation guessing numbers game project practice ( be based on srand function 、rand function ,Switch sentence 、while loop 、if Condition criteria, etc )
One 、 Guess the requirements of the number game
Guessing numbers is a classic game we liked when we were young , In this paper , The main functions of the guessing numbers game are as follows
1. Log in to guess the number game system , Display the hour welcome interface .
2. The numbers guessed by users are systematically and randomly 1-20 Between generation .
3. Users can have 5 Guess this randomly generated number for one time .
4. If the user guesses big , The system will display guess big , And prompt how many chances there are to guess the number .
5. if 5 I didn't guess the number randomly generated by the system twice , Then the game is over .
Two 、 Guess the process of realizing the number game
2.1 Project creation
Use VS2019 Create a file called GuessNumber Of C Language project , And in GuessNumber Create... In the project Main.c Source files and Main.h The header file .
2.2 Content of header file
stay Main.h Header file , Include relevant header files , As shown below
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
In declaring two functions , As shown below
// Show menu
void menu();
// Guess the number game function
void gameGuess();
among ,menu The function of the function is to display the initialization interface of the guessing number game , And provide relevant instructions for guessing numbers games .gameGuess The function of the function is the core of the whole number guessing game , stay gameGuess It mainly realizes the function of users guessing numbers , And provide users with friendly prompt function .
2.3 Source file content
First, include the header file
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
among #define _CRT_SECURE_NO_WARNINGS To make the compiler have no warnings .
main Add the following code to the function
system("color 3E");
int select = 0;
// Prevent random numbers from appearing
srand((unsigned int)time(NULL));
// Using the loop implementation, you can play many times
do
{
menu();
printf(" Please enter the option :");
scanf("%d", &select);
switch (select)
{
case 1:
gameGuess();
break;
case 0:
break;
default:
printf(" Wrong choice \n");
system("pause");
system("cls");
break;
}
} while (select);
printf(" Welcome to use next time , bye !!!\n");
system("pause");
return 0;
First define select Variable , According to the value entered by the user , To the variable select assignment , So as to enter the corresponding function , And then realize the relevant functions .
srand To prevent random numbers from appearing .
According to the input select value , Get into switch Branch statements to judge .
menu Function as follows
// Show menu
void menu()
{
printf("**********************************************************\n");
printf("* Welcome to the number guessing game \n");
printf("* Rule description \n");
printf("* Guess the number is 1-20\n");
printf("* share 5 Second chance \n");
printf("* Welcome to guess the number !!!\n\n");
printf("* 1. Start the game \n");
printf("* 0. Quit the game \n");
printf("**********************************************************\n\n");
}
Its main function is to display the menu function of the system , Prompt the user to guess how the number game should be played .
gameGuess Function as follows
// Guess the number game function
void gameGuess()
{
int num = 0, input = 0, count = 0;
num = rand() % 20 + 1;// obtain 1-100 A random number of
while (count != 5)// utilize if Circular judgement
{
printf(" Please enter a number :");
scanf("%d", &input);
if (input == num)
{
printf(" Congratulations! , Your guess is right !!!\n");
break;
}
else if (input < num)
{
printf(" You guess it's small \n");
}
else
{
printf(" You guessed big \n");
}
count++;
if (count == 5)
{
printf(" unfortunately ,5 The first chance is used up , You're not right !!!\n\n");
printf(" The right answer is :%d\n", num);
break;
}
printf(" Do you have :%d Second chance \n\n", 5 - count);
}
system("pause");
system("cls");
}
Use system library functions rand Randomly generate one 1-20 The number of , Assign a value to a variable num , Then according to the input variables input Whether the values are equal is used == Judge .
count It is mainly used to judge how many chances the user has to guess the number .
3、 ... and 、 Guess the debugging results of the number game are as follows
1. Entry system , Input number 1
2. Guess the correct debugging result 
3. There is no debugging result guessed correctly 
Four 、 Based on the summary of guessing numbers games
The guessing numbers game in this article makes us understand C The introduction of language can be effectively improved , about srand function 、rand function ,Switch sentence 、while loop 、if Condition criteria, etc C Language knowledge points can be further mastered .
5、 ... and 、 Complete code
Main.h The header file
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Show menu
void menu();
// Guess the number game function
void gameGuess();
Main.c Source file
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
int main()
{
system("color 3E");
int select = 0;
// Prevent random numbers from appearing
srand((unsigned int)time(NULL));
// Using the loop implementation, you can play many times
do
{
menu();
printf(" Please enter the option :");
scanf("%d", &select);
switch (select)
{
case 1:
gameGuess();
break;
case 0:
break;
default:
printf(" Wrong choice \n");
system("pause");
system("cls");
break;
}
} while (select);
printf(" Welcome to use next time , bye !!!\n");
system("pause");
return 0;
}
// Show menu
void menu()
{
printf("**********************************************************\n");
printf("* Welcome to the number guessing game \n");
printf("* Rule description \n");
printf("* Guess the number is 1-20\n");
printf("* share 5 Second chance \n");
printf("* Welcome to guess the number !!!\n\n");
printf("* 1. Start the game \n");
printf("* 0. Quit the game \n");
printf("**********************************************************\n\n");
}
// Guess the number game function
void gameGuess()
{
int num = 0, input = 0, count = 0;
num = rand() % 20 + 1;// obtain 1-100 A random number of
while (count != 5)// utilize if Circular judgement
{
printf(" Please enter a number :");
scanf("%d", &input);
if (input == num)
{
printf(" Congratulations! , Your guess is right !!!\n");
break;
}
else if (input < num)
{
printf(" You guess it's small \n");
}
else
{
printf(" You guessed big \n");
}
count++;
if (count == 5)
{
printf(" unfortunately ,5 The first chance is used up , You're not right !!!\n\n");
printf(" The right answer is :%d\n", num);
break;
}
printf(" Do you have :%d Second chance \n\n", 5 - count);
}
system("pause");
system("cls");
}
边栏推荐
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- UUID and secrets module
- Functools module
- 冰冰学习笔记:类与对象(中)
- js做一个红绿灯
- Gossip: talk with your daughter about why you should learn culture lessons well
- Examples of Oracle triggers
- 用shell来计算文本中的数字之和
- (2022牛客多校三)A-Ancestor(LCA)
- TCP/IP协议分析(TCP/IP三次握手&四次挥手+OSI&TCP/IP模型)
猜你喜欢
随机推荐
C# Winfrom 常用功能整合-2
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
Oracle composite query
Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query
Functools module
UI gesture actions of uiautomator common classes
在Perl程序中暴露Prometheus指标
Oracle database problems
(2022牛客多校三)J-Journey(dijkstra)
Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
flink去重(一)flink、flink-sql中常见的去重方案总结
冰冰学习笔记:类与对象(中)
用shell来计算文本中的数字之和
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
Grayog log server single node deployment
when的多条件查询
Using docker in MAC to build Oracle database server
Understanding and learning of node flow and processing flow in io
杂谈:高考
Mysql: increase the maximum number of connections




![[wsl2] configure the USB camera connecting the USB device and using the host](/img/03/7ebc7eebeda1598c8f4fdd1e9188c7.png)




