c - 如何订购相互依赖的功能?

标签 c

我在过去的两年里在学校学习了 C 和 C++ 的基础知识,这个星期一我刚开始新的一年,所以我想训练一下,因为我在假期里忘记了很多东西。

所以,我想在控制台中编写一个名为“more or less”的小游戏。我必须猜测一个数字,对于我建议的每个数字,控制台都会告诉我 secret 数字是否更高。没什么疯狂的。

我打算添加另一个迷你游戏和一些选项,所以我制作了一个菜单,只需点击一个数字,我就可以选择我想去的地方。

问题是我在游戏结束时做了一个重启功能,让我可以选择是要重启游戏还是返回菜单。

因此菜单函数在迷你游戏函数之后,但同时重启函数在迷你游戏函数中(这是一个 while 循环)并且在菜单函数之后。

我想还有另一种方法可以做到这一点,但我不知道怎么做。我对此一无所知。

我用英语描述了我的代码,因为它是用法语编写的(我是法国人)。如果你足够好,你可以不看翻译就试试:)

感谢您抽出宝贵时间帮助世界各地的人们。我猜你们都是 super 信息学家 :)

如果我在莎士比亚的语言中犯了一些错误,请不要犹豫告诉我。

//小游戏功能

int Plus_ou_moins() { //More or less - the game
Selection_nombre_mystere(); //function that defines the mistery number
printf("\nRentrez un nombre pour commencer\n"); //Enter a number to begin
printf("\n");
while (nombreEntre != nombreMystere) { //while proposed nb != mystery nb
    scanf("%d", &nombreEntre); //I read then stack the value in a variable
    if (nombreMystere > nombreEntre) //If the number isn't high enough
        printf("+\n"); //Write +
    else if (nombreMystere < nombreEntre) //the opposite
        printf("-\n");
    else if (nombreMystere == nombreEntre) { //I guessed the good number
        printf("Bravo, vous avez trouve le nombre mystere: %d\n", nombreMystere); //THE RESTART PART IN THE MINI-GAME FUNCTION ->                 
        int restart = 0;
        printf("Voulez vous jouer de nouveau ? "); //try again ?
        scanf("%d", &restart);//1=yes 0=no
        if (restart == 0) {
            Affichage_menu(); //I display the menu if it's no
        } else {
            Plus_ou_moins(); //I restart the game if it's yes
        }
    }
}
return 0;}

//菜单显示功能

void Affichage_menu(){
printf("=== MENU ===\n"); //Display things
printf("\n");
printf("1. Plus ou moins\n");
printf("2. Pour combien\n");
printf("3. Options\n");
printf("4. Statistiques\n");
Choix(); //I choose the number related to the game(1).
switch (choix) {
    case 1:
        Plus_ou_moins(); //I START THE GAME = IMPOSSIBLE because the game is declared before. Even if I reverse the order of the 2 functions, the menu is not declared at the restart part.
        break;
}}

最佳答案

答案是菜单函数应该调用游戏函数,但是游戏函数不应该调用菜单函数,游戏函数也不应该调用自己。

为避免游戏函数调用自身的情况,您可以使用无限循环:while(1)。这样,如果用户选择再次玩,循环将重新开始游戏。如果用户选择退出游戏,则简单的 return 语句会将用户送回菜单。

菜单也需要无限循环。并且菜单需要某种方式来退出循环。所以我会添加另一个菜单项,在下面的示例中编号为 5。当用户选择 5 时,函数返回,退出循环。

void Plus_ou_moins(void) { //More or less - the game
    while (1) {  // repeat the game until the user decides to stop
        Selection_nombre_mystere(); //function that defines the mistery number
        printf("\nRentrez un nombre pour commencer\n"); //Enter a number to begin
        printf("\n");
        while (nombreEntre != nombreMystere) { //while proposed nb != mystery nb
            scanf("%d", &nombreEntre); //I read then stack the value in a variable
            if (nombreMystere > nombreEntre) //If the number isn't high enough
                printf("+\n"); //Write +
            else if (nombreMystere < nombreEntre) //the opposite
                printf("-\n");
            else if (nombreMystere == nombreEntre) { //I guessed the good number
                printf("Bravo, vous avez trouve le nombre mystere: %d\n", nombreMystere); //THE RESTART PART IN THE MINI-GAME FUNCTION ->
                int restart = 0;
                printf("Voulez vous jouer de nouveau ? "); //try again ?
                scanf("%d", &restart);//1=yes 0=no
                if (restart == 0) {
                    return; // if no, return to the menu
                }
            }
        }
    }
}

void Affichage_menu(void){
    while (1) {  // repeat the menu until the user decides to exit
        printf("=== MENU ===\n"); //Display things
        printf("\n");
        printf("1. Plus ou moins\n");
        printf("2. Pour combien\n");
        printf("3. Options\n");
        printf("4. Statistiques\n");
        printf("5. Sortir\n");
        Choix(); //I choose the number related to the game(1).
        switch (choix) {
            case 1:
                Plus_ou_moins(); //I START THE GAME
                break;
            case 5:
                return; // exit the menu
        }
    }
}

关于c - 如何订购相互依赖的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150754/

相关文章:

c - 第34行获取被调用对象不是函数或函数指针问题:13

c - 使用C程序为程序提供参数

c - 声明隐藏了 c 中的局部变量

c - 如何在结构中分配固定大小的二维数组?

c - 如何在不释放它指向的数据指针的情况下释放 skb 缓冲区?

c - 引用字符串时的 Printf 函数

c - 结构的稀疏初始化,任何资源?

c - 超过 50 万个元素的快速排序崩溃

c - 如何为 %20s 的 scanf 删除额外输入

c - 使用 doxygen 的 ISR 文档