Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize number guessing Mini Game in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to realize number guessing Mini Game with c language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

After learning about loops, branches, and functions, you can write some simple Mini Game to add fun to your programming path. It not only improves the coding ability, but also can learn and play at the same time.

Second, game logic

1. Print selection menu (1.play, 0.exit)

two。 Call the rand () function to generate random numbers

3. Set the range of random numbers

4. Guess the number

5. Judge the size of the guess

III. Mind mapping

IV. Game process

Fifth, code analysis 1. The method of generating random numbers

It is not possible to use the rand () function to generate a random number, because it generates the same random number every time it plays, that is, the random number is not random, so it is necessary to use the srand () function to plant a random seed to generate a random number. According to the fact that time varies from time to time, the time () time function can be used to generate seeds.

Here, the type of time_t is defined as a long integer in C language, and the parameter type of srand () function is unsigned int (unsigned shaping), so you need to force type conversion when you use it, and the * * time () * * function does not need to set parameters, so use NULL instead.

two。 The method of setting the range of random numbers

We have asked the system to randomly generate a random number, because the range of random numbers generated by the system is too large, which is time-consuming to guess. Let's first determine the range of a random number, between 0mur100. If you rand () 0 to 1, you can get a random number between 0 and 100. What rand () 0 gets is a random number between 1 and 99, plus 1 is a random number between 1 and 100.

6. Complete code # include#include#includevoid menu () {printf ("* *\ n"); printf ("* 1.play *\ n"); printf ("* 2.exit *\ n") Printf ("* *\ n");} void game () {/ / the implementation of the number guessing game / / 1. The generated random number / / rand function returns a random value / / time stamp int ret = rand ()% 32767 + 1; the remainder of / / 0 is 0-99, and then + 1, the range is 1-100 / / printf ("% d\ n", ret); / / 2. Guess the number int guess = 0; while (1) {printf ("guess the number:"); scanf ("% d", & guess); if (guess)

< ret) { printf("猜小了\n"); } else if (guess >

Ret) {printf ("guess big\ n");} else {printf ("Congratulations, you guessed it\ n"); break;}} int main () {int input = 0; srand ((unsigned int) time (NULL)); do {menu () / / print menu printf ("Please choose:"); scanf ("% d", & input); switch (input) {case 1: game (); break; case 0: printf ("quit the game\ n"); break Default: printf ("choose wrong, reselect\ n"); break;}} while (input); return 0;} "how to achieve Mini Game guessing numbers in C language" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report