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

What are the two methods to realize the game of guessing numbers in C language?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What are the two methods to realize the number guessing game in C language? I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Preface

Guessing number Mini Game is an interesting C language Mini Game that most of us will learn when we learn the C language. I will introduce the implementation and programming logic of the game in detail below. And use dichotomy to speed up the Mini Game. While playing Mini Game, you can also have a better understanding of C language control sentences.

I. description of the game

Game content

As the name implies, the program allows the system to randomly generate an integer of 0-100 and let the player guess. After the player guesses the number, the system gives the player feedback and counts the number of guesses until the player guesses correctly.

Game method

1. Generate game menu 1. Play (enter the game) 0. Exit (quit the game)

2. Use circular statements to build a game framework, so that players can play many games until they guess correctly.

3. Design the specific idea of the game, make the system generate a random number, let the player input a number, then compare the two numbers, and gradually lock the target according to the feedback information of the system.

Second, the code implementation 1. Generate game menu

(1) void menu ()

(2) effect display

two。 Construct the main function

Int main () function

Using the do--while () loop, the player makes multiple guesses. Use the switch statement to control whether to play the game or quit the game.

Sometimes the program requires a random number within a specified range. To limit the range of random numbers to an integer between 1 and a maximum max, you can use the following formula:

Number = rand ()% max + 1

For example, to generate a random number from 1 to 10 to represent the number of points of the dice, you can use the following statement:

Dice = rand () 610 + 1

3. Build game functions

Game effect demonstration:

Third, realize the guess by dichotomy.

If you use dichotomy to guess any random number less than 100, you can guess the random number correctly up to seven times.

Dichotomy can also be called binary search, which describes the process of searching for a specific value in an ordered set. The generalized binary search is to reduce the size of the problem to half of the original size as much as possible.

Through dichotomy, the program continues to narrow the scope of guessing until it guesses n.

For example, n = 93

The program guessed 50 for the first time and judged that the value was too small.

The second guess is 75, and the judgment value is too small.

The third guess is 87, and the judgment value is too small.

The fourth guess is 93, ringht

If 93 is wrong, we can continue to narrow it down according to the feedback, and we can guess the right answer up to seven times.

The code is attached:

# include # include # include / / time system time header file void menu () / / create the game menu {printf ("*\ n"); printf ("* 1.playback games *\ n"); printf ("* 0.exitually loaded games *\ n"); printf ("*\ n") } void playgame () {int magic=rand () 0x1pm int guess; int counter=0;// counter / a random integer int guess; int counter=0;// counter starting with 0 while (1) {do {printf ("guess a number:") / / prompt the user to play a number guessing game and enter an integer scanf ("% d", & guess); counter++; if (guess > magic) / / depending on the comparison, prompt the user to be high, low, or right. Printf ("too big!\ n"); else if (guess

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