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 guess the number Mini Game in C language

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to guess the number Mini Game in C language". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn "how to guess numbers in C language Mini Game"!

Clearly achieve the goal

Before doing any small project, we first need to be clear about what we want to achieve.

So what is the goal of guessing numbers?

The computer randomly gives a target number, and the player guesses.

The computer will guess whether it is big or small according to the number guessed by the player.

Until you guess right, the round of the game is over.

The end of the game will ask if you want to continue. 1 means to continue, 0 means not to continue.

There are 3 game modes (simple, ordinary and difficult), and the corresponding contents are 25, 15 and 5 times respectively.

The game will record your record and press 2 in the menu to query your record.

Clear logical structure

Having a clear idea of what you want to achieve, the first step is to build a logical structure.

So what is the logical structure?

There is a menu prompt that presses 1 for starting / continuing the game, 2 for querying the results, 0 for quitting the game, and other keys for typing errors.

After entering 2, it will jump out of the menu again. After you are prompted for an error, you can re-enter the command

After entering 1, you will ask for the game mode and start.

So the logical structure is as follows:

# include int computer = 0 menu / computer's record int player = 0 role / player's record int main () {int input = 0 strike / type the command do {menu (); / / menu prompt module followed by implementation scanf ("% d", & input) Switch (input) {case 0: printf ("successfully quit the game"); break; case 1: playgame (); / / play the game module, followed by the implementation of store (); / / store the record module break Case 2: query (); / / query the performance module, followed by the implementation of break;}} while (input); return 0;} function details

Menu module

According to the target requirements, the menu block code is as follows:

Void menu () {printf ("* *"* 0Murray-quit the game * * "* 1Mui Muay-start the game *" * * 2Mutual Mutual-query the results * * "*") }

Store the record

The goal is to record the results you have played before whenever you open the game.

Void store () {FILE* fp = fopen ("grade.txt", "w +"); if (fp = = NULL) {exit (- 1);} else {fprintf (fp, "computer: player =% d:% d", computer,player);} fclose (fp);} Game Module

The functions of the game module are:

Players choose game mode

Then the computer will first randomly give a number that the player needs to guess.

Roughly indicate which range the number is in.

The player began to guess. And every time you guess, you will be prompted that there are still several opportunities left.

Void playgame () {srand (time (NULL)); / / Random number seed int target = rand ()% 1001bat / Random number representing the computer int input = 0, num = 0; / / input represents the number guessed by the game, and num represents the remaining number of guesses num = mode () / / Game mode selection module, followed by the implementation of while (1) {printf ("Please enter the number you think is the answer:"); scanf ("% d", & input); if (num = = 0) {break;} if (input > target) {printf ("guess big, try again") } else if (input > target) {printf ("guess small, try again");} else {printf ("Congratulations, you guessed right"); break;} num--; printf ("you still have% d chances left", num) } if (! num) {printf ("I'm sorry you lost"); computer++; / / Mark the record} else {printf ("Congratulations, you won"); player++;} printf ("do you want to continue the game?"); / / this step is to remind players whether to continue.}

Game mode choice

Int mode () {char str [20] = {0}; int num = 0; printf ("Please enter game mode: simple and common difficulty:"); scanf ("% s", str); if (strcmp ("simple", str) = = 0) {num = 25;} else if (strcmp ("normal", str) = = 0) {num = 15 } else {num = 5;} return num;}

Performance inquiry

Void query () {FILE* fp = fopen ("grade.txt", "r"); int a = 0, b = 0; char p [60] = {0}; if (fp = = NULL) {exit (- 1);} else {fgets (pmae 60m FP); printf ("% s", p);} fclose (fp); printf ("does the game continue?") } finished product display # include # include int computer = 0; / / computer record int player = 0 / / the player's record void menu () {printf ("* *"* 0Murray-quit the game * * "* 1Mui Muay-start the game *" * * 2Mutual Mutual-query the results * * "*") } int mode () {char str [20] = {0}; int num = 0; printf ("Please enter game mode: simple and common difficulty:"); scanf ("% s", str); if (strcmp ("simple", str) = = 0) {num = 25;} else if (strcmp ("normal", str) = 0) {num = 15 } else {num = 5;} return num;} void playgame () {srand (time (NULL)); / / Random number seed int target = rand ()% 1001 int input = 0-1000 int input = 0, num = 0; / / input represents the number of guesses in the game, and num represents the remaining number of guesses num = mode () / / Game mode selection module while (1) {printf ("Please enter the number you think is the answer (0-1000):"); scanf ("% d", & input); if (num = = 0) {break;} if (input > target) {printf ("guess big, try again") } else if (input < target) {printf ("guess small, try again");} else {printf ("Congratulations, you guessed right"); break;} num--; printf ("you still have% d chances left", num) } if (! num) {printf ("Sorry you lost"); computer++; / / Mark the record} else {printf ("Congratulations, you won"); player++;} printf ("do you want to continue the game?") / / this step is to remind the player whether to continue.} void query () {FILE* fp = fopen ("grade.txt", "r"); int a = 0, b = 0; char p [60] = {0}; if (fp = = NULL) {exit (- 1);} else {fgets (pdome 60th FP); printf ("% s", p);} fclose (fp) Printf ("does the game continue?");} void store () {FILE* fp = fopen ("grade.txt", "w"); if (fp = = NULL) {exit (- 1);} else {fprintf (fp, "computer: player =% d:% d", computer,player);} fclose (fp);} int main () {int input = 0 / / Type the command do {menu (); / / menu prompt module, followed by the implementation of scanf ("% d", & input); switch (input) {case 0: printf ("successfully quit the game"); break; case 1: playgame () / / play the game module, then implement store (); / / store the record module break; case 2: query (); / / query the record module, and then implement break;}} while (input); return 0 } Thank you for your reading. the above is the content of "how to guess numbers in C language Mini Game". After the study of this article, I believe you have a deeper understanding of the problem of how to guess numbers in Mini Game in C language. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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