Guessing games based on C language (with source code)_* Blog with the wind of the past*

Posted by jd307 on Mon, 07 Mar 2022 21:59:01 +0100

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

preface

A simple guessing game based on C language aims to get familiar with the random number functions, method calls and the application of modular code in C language.

1, Rules of the game

Prompt the user to enter an integer (take 1-100 here as an example). If the number entered by the user is larger than the number randomly generated by the system, prompt the user to enter the value too large and re-enter. On the contrary, prompt the user to enter the value too small and re-enter until the number entered by the user is the same as the value generated by the system. The game ends.

2, Basic ideas

Firstly, an integer is randomly generated through the random number function (here, the integer between 1-100 is taken as an example), and then the generated random number is compared with the number entered by the user to judge whether the number entered by the user conforms to the rules of the game.

3, Code analysis

1. Print game menu

This step is simple printf. There is no difficulty. Just pay attention to the typesetting.

The example code is as follows:

/*Game menu*/
void menu()
{
    printf("\t\t\t\t[Interesting guessing game]\n");
    printf("\t\t\t1.Start the game\t\t2.Exit the game\n");
    printf("------------------------------------------------------------------------------\n");
}

2. Realize functional interaction

This part is mainly about the application of switch.

The example code is as follows:

/*Functional interaction*/
void interactive()
{
    printf("Please enter your choice:");
    scanf("%d",&choice);
        switch(choice){
          case 1:
              game();break;
          case 2:
              out();break;
          default:printf("Please at 1-2 Medium selection\n");break;
        }
}

3. Generate game data

The game data here corresponds to the random number randomly generated by the system, which is used to compare with the number entered by the user to ensure the normal progress of the game. The random number function is mainly applied here.

The example code is as follows:

/*Generate game data*/
void date()
{
    srand((time(NULL)));//Random number seed
    num_s = rand()%101;//Randomly generate random numbers within 100
}

4. Run the game

When the preliminary work is ready, we can start running the game,
This part is mainly the application of logical understanding of game rules. It is relatively simple. Just pay attention to the logical relationship.

The example code is as follows:

/*Start the game*/
void game()
{
    date();
    printf("Please enter a number:");
    scanf("%d",&num);
    num= juge(num);//Reassign the judged num to num
    while(1){
        if(num==num_s){
            printf("Congratulations, you guessed it!\n");
            system("pause");
            system("cls");break;
        }else if(num-num_s>0){
            printf("The number entered is too large!\n");
            printf("Try again:");
            scanf("%d",&num);
        }else if(num-num_s<0){
            printf("The number entered is too small!\n");
            printf("Try again:");
            scanf("%d",&num);
        }
    }
}

4, Operation results

5, Overall code

#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
/*Define global variables*/
int choice = 0;//Storage function selection data
int num = 0;//Store user input data
int num_s = 0;//Store randomly generated data of the game

/*Function declaration*/
void menu();//Game menu
void interactive();//Functional interaction
void date();//Generate game data
void game();//Start the game
void out();//Exit the game
int juge(int num);//Judge whether the user input is legal

/*Test function*/
int main()
{
    system("title Fun guessing game");
    system("color f5");
    while(1){
      menu();
      interactive();
    }
    return 0;
}
/*Game menu*/
void menu()
{
    printf("\t\t\t\t[Interesting guessing game]\n");
    printf("\t\t\t1.Start the game\t\t2.Exit the game\n");
    printf("------------------------------------------------------------------------------\n");
}
/*Functional interaction*/
void interactive()
{
    printf("Please enter your choice:");
    scanf("%d",&choice);
        switch(choice){
          case 1:
              game();break;
          case 2:
              out();break;
          default:printf("Please at 1-2 Medium selection\n");break;
        }
}
/*Generate game data*/
void date()
{
    srand((time(NULL)));//Random number seed
    num_s = rand()%101;//Randomly generate random numbers within 100
}
/*Judge whether the data entered by the user is legal*/
int juge(int num)
{
    while(1){
        if(num>0&&num<=100){
           return num;break;
        }else if(num>100||num<=0){
            printf("(Please at 1-100 Select a number within)\n");
            printf("Please re-enter:");
            scanf("%d",&num);
        }else{
            printf("Your input is illegal, please re-enter:");
            scanf("%d",&num);
        }
   }
}
/*Start the game*/
void game()
{
    date();
    printf("Please enter a number:");
    scanf("%d",&num);
    num= juge(num);//Reassign the judged num to num
    while(1){
        if(num==num_s){
            printf("Congratulations, you guessed it!\n");
            system("pause");
            system("cls");break;
        }else if(num-num_s>0){
            printf("The number entered is too large!\n");
            printf("Try again:");
            scanf("%d",&num);
        }else if(num-num_s<0){
            printf("The number entered is too small!\n");
            printf("Try again:");
            scanf("%d",&num);
        }
    }
}
/*Exit the game*/
void out()
{
	int i;
    printf("Exit the game in three seconds:");
    for(i=0;i<3;i++){
        printf("ยท");
        Sleep(1000);
    }
    printf("\n");
    printf("Welcome to play again next time!\n");
    exit(0);
}

6, Summary

This article is mainly an exercise of the basic knowledge of C language. It mainly reviews the random number functions and function calls in C language. For novices, mastering the logic in the code is very important.

Topics: C Game Development