当前位置:网站首页>C language simple games
C language simple games
2022-07-22 13:57:00 【Australopithecus northerner】
List of articles
Preface
This blog will lead you to write some by yourself C Language games ; To increase for C Interest in language
One 、 Guess the number game
First of all, let's briefly introduce this little game :
Usually played by two people , One side gives numbers , Guess one side . The person who gives the number should think of a number without repetition , You can't let the guessing know . The guessing person can start to guess .
If the correct answer is 576, And the guesser guessed 56, Then we can give the guesser a hint , Tell him to guess small ; If the guesser guesses for 999, Then we can give the guesser a hint , Tell it to guess big ; Until you get it right ;.
analysis :
1、 Since you need to guess the number , We players can't know , The computer has to randomly generate a value for us to guess ;
2、 Since random values are needed , We can call rand() Function to generate random values ;
rand The range of random numbers that can be generated is 0~RAND_MAX;
RAND_MAX How much is? ?
So let's test , See if it can really generate random numbers ;
After many tests , Find out rand Always generate 41, It hasn't changed , No random number is generated ; This is very puzzling ,rand Is the generated random number really ?
actually ,rand The function is for a function called “ seeds ” To generate a random number . The reason why the same number is generated every time before , Because of rand The default seed constant of the function is 1. To generate different random numbers , You have to change the value of the seed ;
What changes this is the need srand Function to implement ;
Let's say we call srand(2), It'll take 2 Generate random numbers for the benchmark ; But once the seed is determined , The generated random number is also determined ,
Therefore, we must change the seed value itself from a constant to a random number , However, in order to generate random numbers, we need random numbers , This in itself is contradictory ;
Our general solution is : Take the time of running the program as the seed ;
It's useful here time function
A brief introduction :
Running results :
So far, we have solved the problem of how to generate random numbers ;
3、 How to control the scope ?
In order to reduce the difficulty of the game , We have to control the range of numbers generated by the residence :
The general method only needs :b+rand()%(a+1)// Generation is greater than or equal to b And less than or equal to b+a The random number ;
So far, the idea has been roughly straightened out ;
Next, we can implement it in code :
#include<time.h>
#include<stdio.h>
enum GAME// Use enumeration constants to express , Make code more readable ;
{
EXIT,// The default from the 0 Start , Then increase in turn
PLAY,
};
void menu()// Simply write a menu to give players a good interface ;
{
printf("+******* Guess the number game (1~100) *******+\n");
printf("l************ 1.PLAY *************l\n");
printf("l************ 0.EXIT *************l\n");
printf("+*********************************+\n");
}
void game()
{
int key = rand() % 100 + 1;// Generate 1~100 The random number
int val = 0;// The number we guessed ;
while (1)// Using the dead loop , Give players the chance to guess wirelessly ;
{
printf(" Please enter :");
scanf("%d",&val);
if (key > val)
printf(" Guess a little \n");
else if (key < val)
printf(" Guess the \n");
else
{
printf(" Congratulations on your guesses \n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d",&input);
switch (input)
{
case PLAY:
game(); break;
case EXIT:printf(" Quit the game \n"); break;
default:printf(" Input error , Please reselect \a\a\a\n"); break;
}
} while (input);
return 0;
}
Two 、 Rotate the cursor
Next, let's write a little game ;
Rotate the cursor ;
design sketch ;
It is this cursor that keeps rotating ;
Ideas : Mainly used \r This escape character , The Chinese name is return ;
\r and \n The difference between ?
\r: The Chinese name is return , The function is to move the cursor to the beginning of the line , Write from scratch ;
\n: Chinese name is newline , The function is to move the cursor to the beginning of the next line , Write from scratch ;
With this knowledge , Rotating the cursor is a good implementation :
#include<windows.h>
int main()
{
int i = 0;
char arr[] = "|/-|\\";// here \\ Represents a character \ while (1)
{
i %= 5;// Prevent arrays from crossing bounds ;
printf("[%c]\r",arr[i]);
i++;
Sleep(300);// Let the program rest 0.3s
}
return 0;
}
3、 ... and 、 Multiple characters move from both ends , Converging in the middle
The last little game , It's similar to rotating the cursor ;
design sketch :
* Will disappear one by one ;
Go straight to the code :
#include<windows.h>
int main()
{
char arr1[] = "I love China!";//1
char arr2[] = "*************";//2 The two character arrays must be the same length to be effective ;
int left = 0;
int right = strlen(arr1) - 1;
printf("%s\r", arr2);
Sleep(1000);
while (left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
right--;
left++;
printf("%s\r",arr2);
Sleep(1000);
}
printf("%s", arr2);// Prevent the last carriage return I Ate ;
return 0;
}
边栏推荐
- Usage scenarios of stale read function
- JUC-进程和线程
- Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
- JUC-内存
- Daily question C language 9
- There are two key skills for high availability of microservices, which you must use
- Database expansion can also be so smooth, MySQL 100 billion level data production environment expansion practice
- c语言进阶篇:数据的存储(浮点型)
- 【C】 General template of information management system / address book (introduce static, dynamic and file versions)
- el-tree树设置懒加载以及设置默认勾选
猜你喜欢
海量 Region 集群调优最佳实践
ASM学习系列(二)
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
Live review | wonderful playback of Apache pulsar meetup (including PPT download)
JUC-进程和线程
c语言进阶:字符串函数及模拟实现
Job hopping? Have you got the 7 skills you need to master in the interview software test?
c语言进阶篇:数据的存储(浮点型)
Mass region cluster tuning best practices
Cyberspace mapping
随机推荐
Best practices of haproxy in tidb
Interview project preparation | how to show the project to the interviewer?
ASM学习系列(二)
JUC thread pool
Leetcode interview question 17.15: longest word
PADS画板框
最终一致性性分布式事务 TCC
【微信小程序】Image图片加载(78/100)
Job hopping? Have you got the 7 skills you need to master in the interview software test?
Learn the use of mockmvc from the source code
JUC-并发包
How to add a map to a page
HAProxy 在 TiDB 中的最佳实践
JUC-无锁
Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
Leetcode-814: binary tree pruning
Error: your local changes to the following files would be overwritten by merge:xxxx
Win10 registry of the nth reinstallation of the system
JUC-进程和线程
Live review | wonderful playback of Apache pulsar meetup (including PPT download)