Take you by hand to learn how to guess numbers and write the code of the game

Posted by kiwis on Thu, 27 Jan 2022 08:22:52 +0100

Statement: because I am a non professional transcoder and my personal level is limited, there will inevitably be inaccuracies in my blog. If any readers find it, I urge you to actively point out that I also want to know where my mistakes are. Welcome to correct and make progress together.

  • contact information: 3146367553@qq.com

If you want to see the source code directly, you can jump to 3 Source code.

catalogue

1. General description of program functions

2. Analysis of coding ideas

2.1 overall framework analysis

2.2 key and difficult points analysis (how to generate random numbers of 1-100)

2.3 relevant details and two modes

3. Source code and screenshot

1. General description of program functions

The system generates a random number (integer) of 1-100, and the player guesses on the keyboard. The player will tell you whether he guesses big or small. If he guesses right, he will congratulate you. At the same time, players can quit the game in the middle of playing the game. Individuals can choose between simple and difficult modes.

2. Analysis of coding ideas

2.1 overall framework analysis

① First of all, we can contact the small games we have played before (not small games can also be used). Will we have an option to start and exit the game when we enter the game? Is there no response when clicking on other parts of the screen? Therefore, we can set up a main menu function to print the game interface (because the interface may be seen more than once). At the same time, we think from the perspective of players. Do we have a high probability of guessing the desire to guess correctly until we quit the game if we don't want to play? Therefore, the choice at the beginning of the game is actually in line with the cycle. In addition, users must choose when they enter the game for the first time, little cute, don't you think?

Therefore, choosing whether to start the game is a do while loop, and the loop condition can be 1 (you can break out of the game).

② It's not difficult for us to analyze. It only takes one or two sentences to quit the game. Starting the game is a more complex code. At the same time, the user can start the game many times (as long as he still wants to play). In order to avoid repeated wheel building, we should set the game as a function.

2.2 key and difficult points analysis (how to generate random numbers of 1-100)

In fact, the key point is that how can we let the computer generate a random number of 1-100? First, if there is a random number x, then x% 101 is 1-100 (the remainder range is 1-100). Next, the problem of random numbers.

After consulting, rand function can generate random numbers.

We found that the RAND header file is stdlib h. It is worth noting that we need to generate the srand random number generator before calling the rand function.

The usage of srand function is srand(unsigned int), that is, unsigned integer is in parentheses. However, once determined in parentheses, the random number is also determined. In this way, the number of small games is fixed. Therefore, the parentheses must be random numbers? However, how can random numbers generate random numbers? At this time, we can change our thinking, as long as the number in brackets is a changed number. So, when we play the game, what changes? Yes, it's time!

After consulting, there is exactly a time function that returns a difference between the current time point and the computer start time (the difference changes with time). Therefore, the random number generator can be written as srand ((unsigned int) time(NULL)). At the same time, through experiments, I found that the random number generator must be separated from the rand function, that is, the srand generator is in the main function, and the value generated by the rand function is assigned in the game function.

2.3 relevant details and two modes

① First of all, we certainly don't like to see the main interface of the game all the time when we are playing the game, so the screen clearing function is of great use.

That is, system("cls"), and the header file is windows h.

② There are two modes I designed. One is a simple mode, which can see my previous guessing digital records; The other is the difficult mode. You can't see your own guess number record.

③ Every five guesses, the computer will ask whether to terminate the game and exit. (there will be game customers who suddenly don't want to play or can't play for special reasons).

3. Source code and screenshot

After talking so much, the code is the most important. You need to copy it yourself.

Source code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

void menu()
{
    printf("****************\n");
    printf("****************\n");
    printf("Welcome to the figure guessing game interface\n");
    printf("1:Start the game\n");
    printf("0:Exit the game\n");
    printf("****************\n");
    printf("****************\n");
}
void game1() //Simple mode (you can see your own guess record)
{
    int guess = 0;
    int x = rand() % 101;//Store random numbers, take the remainder 101, and the remainder is 1-100
    int count = 0;
    while (1)
    {
        printf("Please enter the number you guessed:");
        scanf("%d", &guess);
        count++;
        if (guess == x)
        {
            system("cls");
            printf("Congratulations, you guessed right\n");
            printf("Will return to the game interface\n");
            break;
        }
        else if (guess > x)
        {           
            if (0 == count % 5)
            {
                printf("Do you want to terminate the game?\n");
                char input = 'A';
                getchar();//Clear buffer enter key
                scanf("%c", &input);
                if ('Y' == input)
                {
                   break;
                }
            }
            printf("Sorry, your guess is too big\n");
        }
        else
        {
           if (0 == count % 5)
            {
                printf("Do you want to terminate the game?\n");
                char input = 'A';
                getchar();//Clear buffer enter key
                scanf("%c", &input);
                if ('Y' == input)
                {
                   break;
                }
            }
            printf("Sorry, your guess is too small\n");
        }
    }
}

void game2() //Difficult mode (you can't see your own guess record)
{
    int guess = 0;
    int x = rand() % 101;//Store random numbers, take the remainder 101, and the remainder is 1-100
    int count = 0;
    while (1)
    {
        printf("Please enter the number you guessed:");
        scanf("%d", &guess);
        count++;
        if (guess == x)
        {
            system("cls");
            printf("Congratulations, you guessed right\n");
            printf("Will return to the game interface\n");
            break;
        }
        else if (guess > x)
        {           
            system("cls");
            if (0 == count % 5)
            {
                printf("Terminate the game?\n");
                char input = 'A';
                getchar();//Clear buffer enter key
                scanf("%c", &input);
                if ('Y' == input)
                {
                   break;
                }
            }
            printf("Sorry, your guess is too big\n");
        }
        else
        {
           system("cls");
           if (0 == count % 5)
            {
                printf("Do you want to terminate the game?\n");
                char input = 'A';
                getchar();//Clear buffer enter key
                scanf("%c", &input);
                if ('Y' == input)
                {
                   break;
                }
            }
            printf("Sorry, your guess is too small\n");
        }
    }
}

int main()
{
    int choose = 0;
    srand((unsigned int) time(NULL));
    do
    {
        
        menu();
        scanf("%d", &choose);
        if (1 == choose)
        {
            system("cls");
            printf("Start guessing numbers\n");
            int choose2 = 0;
            printf("Please select mode: 1.Simple mode 2.Difficult mode\n");
            getchar();
            scanf("%d", &choose2);
            while(1)
            {
                if (1 == choose2)
                {
                    game1();
                    break;
                }
                else if (2 == choose2)
                {
                    game2();
                    break;
                }
                else
                {
                    printf("Your choice is incorrect, please re-enter\n");
                }
            }
        }
        else if (0 == choose)
        {
            system("cls");
            printf("Exit the game\n");
            break;
        }
        else
        {
            system("cls");
            printf("Your input is incorrect, please re-enter\n");
        }
    } while (1);

    return 0;
}

Operation screenshot:

 

It's not easy to create, little ones. Move your little hands and point a praise before you go.

 

 

 

Topics: C Mini Program Game Development