如何在C程序中调用函数?

时间:2015-11-05 16:44:59

标签: c

我尝试使用以下规则制作骰子游戏:

  1. 你掷三个骰子
  2. 如果你滚动并且每个模具是6,那么你赢得100kr
  3. 如果你滚动并且每个模具是相同的(但不是6个),那么你赢得50kr
  4. 如果您滚动并且两个模具相同,则赢得10 kr
  5. 玩家必须每回合下注至少10kr才能玩
  6. 如果玩家获胜,则会询问他们是否想再玩一次
  7. 当玩家的钱少于10kr时,玩家被告知他们不能再玩了。
  8. 我能够做到这一点,但现在我必须通过调用函数来运行它。我已经陷入困境 - 我的代码在下面,但我不确定如何继续。

    #pragma warning(disable:4996)
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include<stdbool.h>
    
    int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling ); // prototype
    keep_rolling = 'y';
    
    int main()
    {
        int d1, d2, d3, money, r;
        char keep_rolling = 'n';
        money = 100;
        r = rolling(d1, d2, d3, money,keep_rolling);
        srand((int)time(NULL));
        printf("welcome to the game! \n \n");
    
        //gameroll
        printf("1.it cost you 10kr to play \n");
        printf("2.if all the dicies are sixes you win 100kr \n");
        printf("3. if all the dicies are alike except number (6) you win 50kr \n");
        printf("4. if you get at least two alike you 1 \n");
        printf("5. otherwise you win nothing\n");
    
        printf("you have %d kr, if you wnat to play press (n) \n\n", money);
        fflush(stdin);
        keep_rolling = ((getchar()));
        d1 = (rand() % 6 + 1);
        d2 = (rand() % 6 + 1);
        d3 = (rand() % 6 + 1);
    
    
    }
    
    int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling) // def my function 
    {
        keep_rolling = 'y';
        do {
            dice1 = (rand() % 6 + 1);
            dice2 = (rand() % 6 + 1);// from 1 to 6
            dice3 = (rand() % 6 + 1);
    
            if (moneyinput < 10)
            {
                printf("you do not have enough money \n");
                break; // exit the program
            }
            moneyinput -= 10;
            if (dice1 == 6 && dice2 == 6 & dice3 == 6)
            {
                printf("you have won 100\n ");
                moneyinput += 90;
            }
            else if (dice1 == dice2 == dice3)
            {
                printf("you have won 50kr \n");
                moneyinput += 40;
            }
            else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3)
            {
                printf("you have won 10kr \n");
                moneyinput += 10;
            }
            else
            {
                printf("Sorry! you have lost. god luck next time \n");
            }
        } while (keep_rolling == 'y');
        system("pause");
    }
    

2 个答案:

答案 0 :(得分:1)

您需要将问题移至用户的滚动功能。您的主要功能不能独立于滚动功能运行。

你的if语句中也有一些逻辑错误会使你的结果与你想要的结果不同。

if (dice1 == 6 && dice2 == 6 & dice3 == 6)

应该是

if (dice1 == 6 && dice2 == 6 && dice3 == 6)

else if (dice1 == dice2 == dice3)

应该是

else if (dice1 == dice2 && dice2 == dice3)

除此之外,您还需要从滚动功能返回。

答案 1 :(得分:0)

请尝试以下方法:
它被编写为在Linux终端上运行,因此它使用fgets()进行用户输入 我们的想法是在main()中有一个循环继续运行,直到用户想要停止或用完钱。在这个循环中调用你的骰子滚动功能。

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


int diceRoll(int moneyinput);

int diceRoll(int moneyinput)
{
    int dice1 = (rand() % 6 + 1);
    int dice2 = (rand() % 6 + 1);// from 1 to 6
    int dice3 = (rand() % 6 + 1);

    moneyinput -= 10;
    if (dice1 == 6 && dice2 == 6 && dice3 == 6)
    {
        printf("you have won 100\n ");
        moneyinput += 90;
    }
    else if (dice1 == dice2 && dice2 == dice3)
    {
        printf("you have won 50kr \n");
        moneyinput += 40;
    }
    else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3)
    {
        printf("you have won 10kr \n");
        moneyinput += 10;
    }
    else
    {
        printf("Sorry! you have lost. good luck next time \n");
    }
    return( moneyinput);
}


int main()
{
    int money = 100;
    char choice[2];

    srand((int)time(NULL));

    printf("welcome to the game! \n \n");

    printf("1.it cost you 10kr to play \n");
    printf("2.if all the dicies are sixes you win 100kr \n");
    printf("3. if all the dicies are alike except number (6) you win 50kr \n");
    printf("4. if you get at least two alike you 1 \n");
    printf("5. otherwise you win nothing\n");

    do
    {
        printf("you have %d kr, if you want to play press (y): ", money);
        fgets( choice, sizeof(choice)+1, stdin );   // read a char then LF

        if( 'y' == choice[0] )
        {
            money = diceRoll( money );
        }
    }while( (money >= 10) && choice[0] == 'y' );

    printf("Quitting game with %dkr!\n", money);

}