当前位置:网站首页>3. Number guessing game
3. Number guessing game
2022-07-06 13:25:00 【It's Wang Jiujiu】
Guess the number game : Random generation 1~100 Number of numbers , Let users guess . When the guessed number is larger than the random number , Tips “ Guess the ”; When only the number of hours than random , Tips “ Guess a little ”; The number guessed is the same as the random number , Tips “ Guessed it ”.
1. The overall framework
First , Start with a menu , Print out options in the menu bar :1.paly、0.exit.
then , Player input options , When the input 1 when , Start the game ; Input 0 when , End the game ; Go out for other hours , Re input . And set the game to be playable , After playing once , You can continue to play .
#include<stdio.h>
void menu()
{
printf("******************\n");
printf("***** 1.paly *****\n");
printf("***** 0.exit *****\n");
printf("******************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));// use srand modification rand
do
{
menu();
printf(" Please select (1/0):>");
scanf("%d", &input);
switch (input)
{
case 0:
printf(" Quit the game \n");
break;
case 1:
printf(" Start the game , Please guess the number (1~100)\n");
game();// game
break;
default:
printf(" Input error , Please re-enter \n");
}
} while (input);
return 0;
}
2. Game part
When the overall framework is written , Run the test . Test success , Start writing the game part game() Code .
The design of the game part is mainly : The system generates a number —— Players enter —— Judge —— Wrong guess. —— Tips —— Re input —— Judge —— Guessed it ( Or the number of times is exhausted )
Because the whole process needs constant input 、 Judge , So you need to use while loop .( Be careful : The generated random number part cannot be put into while In circulation , Otherwise, every cycle , Will generate new random numbers )
void game()
{
int num = rand() % 100 + 1;// Generate 1~100 The random number
int guess = 0;
int count = 6;
while (count--)//6 Second chance ; After ++, Use it first and then ++
{
printf(" Please enter :>\n");
scanf("%d", &guess);
if (guess > num)
{
printf(" Guess the \n");
}
else if (guess < num)
{
printf(" Guess a little \n");
}
else
{
printf(" Guessed it \n");
Sleep(500);// Stop running 500 millisecond
system("cls");// Clear the screen .......
break;
}
}
printf(" I'm sorry , The opportunity has run out \n");
Sleep(500);// Stop running 500 millisecond
system("cls");// Clear the screen
}
(1) When using rand() Function when creating random values , You will find that the random value is the same every time , utilize MSDN, The query shows that :
At this time, you need to use srand Decorate it rand Function of , Use time time, To generate a random number .
srand((unsigned int)time(NULL)), here (unsigned int) take time Casts the return value of , Convert to unsigned integer , And then to time Pass a null pointer (NULL) that will do . Use rand Need to include header file <stdlib.h>, Use time Need to include <time.h>.
(2)int num = rand() % 100 + 1, When random numbers %100 when , Its scope is 0~99, So we need to +1.
(3)while (count--), Specified here count Second chance , After -- It means to use , Again --, When count=0 End cycle at .
(4)sleep(a) To stop running a millisecond ,system("cls") To clear the screen , Need to include header file <windows.h>.
The overall code is as follows :
// Guess the number game
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
void menu()
{
printf("******************\n");
printf("***** 1.paly *****\n");
printf("***** 0.exit *****\n");
printf("******************\n");
}
void game()
{
int num = rand() % 100 + 1;// Generate 1~100 The random number
int guess = 0;
int count = 6;
while (count--)//6 Second chance ; After ++, Use it first and then ++
{
printf(" Please enter :>\n");
scanf("%d", &guess);
if (guess > num)
{
printf(" Guess the \n");
}
else if (guess < num)
{
printf(" Guess a little \n");
}
else
{
printf(" Guessed it \n");
Sleep(500);// Stop running 500 millisecond
system("cls");// Clear the screen .......
break;
}
}
printf(" I'm sorry , The opportunity has run out \n");
Sleep(500);// Stop running 500 millisecond
system("cls");// Clear the screen
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));// use srand modification rand
do
{
menu();
printf(" Please select (1/0):>");
scanf("%d", &input);
switch (input)
{
case 0:
printf(" Quit the game \n");
break;
case 1:
printf(" Start the game , Please guess the number (1~100)\n");
game();// game
break;
default:
printf(" Input error , Please re-enter \n");
}
} while (input);
return 0;
}
If you want to send the game we made to your classmates to try , take VS in debug Change to release, Test and save , Go to the place where the project is saved release file .
X86 by 32 position , It is recommended to save X64, take X64 Sending it to students can work normally .
Click on release—— Will be one of the exe Send the documents to the students .
边栏推荐
- Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
- Introduction pointer notes
- Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
- Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
- Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
- Redis介绍与使用
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- 系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
- Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
- Arduino+ water level sensor +led display + buzzer alarm
猜你喜欢
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
继承和多态(下)
TYUT太原理工大学2022数据库大题之概念模型设计
Inheritance and polymorphism (I)
TYUT太原理工大学2022数据库大题之E-R图转关系模式
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
Rich Shenzhen people and renting Shenzhen people
How do architects draw system architecture blueprints?
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
随机推荐
Interview Essentials: talk about the various implementations of distributed locks!
几道高频的JVM面试题
TYUT太原理工大学2022数据库大题之数据库操作
5.MSDN的下载和使用
What are the advantages of using SQL in Excel VBA
最新坦克大战2022-全程开发笔记-3
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
系统设计学习(一)Design Pastebin.com (or Bit.ly)
Smart classroom solution and mobile teaching concept description
[Topic terminator]
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
3.猜数字游戏
如何保障 MySQL 和 Redis 的数据一致性?
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
TYUT太原理工大学2022软工导论简答题
How do architects draw system architecture blueprints?
TYUT太原理工大学2022数据库大题之概念模型设计
分支语句和循环语句
3.C语言用代数余子式计算行列式