简体   繁体   English

需要帮助进行基于投注的 C 程序骰子游戏

[英]Need help for a C Program Dice game based on betting

so I have to write a complete program to ask user to input amount and store it in the variable name balance, then ask user for the bet amount.所以我必须编写一个完整的程序来要求用户输入金额并将其存储在变量名称 balance 中,然后向用户询问下注金额。 If the amount is less than or equals balance amount, then roll dice.如果金额小于或等于余额金额,则掷骰子。 If the sum roll is 7 or 11, the player wins three times the bet.如果总点数为 7 或 11,则玩家赢得赌注的三倍。 Otherwise, the player loses the bet.否则,玩家输掉赌注。 Player should be able to bet as long as he/she has money in balance.只要他/她有余额,玩家就应该能够下注。

I don't really get what else to do from here (from the code posted below) so please help me fix it.我真的不知道从这里(从下面发布的代码中)还有什么可做的,所以请帮我修复它。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int balance;
int sum;

int Ask_User_for_Bet(int b);
int Dice_Roll(void);
int Win_or_Lose();

int main()
{
    while(balance>0){
        int Ask_User_for_Bet(int b);
        int rollDice();
        int Win_or_Lose();
    }

    return 0;
}


int Ask_User_for_Bet(int b)
{
    printf("Enter bet amount");
    scanf("%d", b);
}

int rollDice(void) {
    sum=((rand() % 12) + 1);
    return ((rand() % 12) + 1);
}

int Win_or_Lose(){
    if(sum==7 || sum==11)
            printf("You won! Play again?");
    else
        printf("You lost! Play again?");



}

Lots wrong here.这里有很多错误。

balance and sum are not initialised to zero balancesum未初始化为零

int Ask_User_for_Bet(int *b)
{
    printf("Enter bet amount");
    scanf("%d", b);
// Or don't pass b, declare locally, pass &b then return
    }

Where do you reduce the balance?你在哪里减少余额?

roll_dice does not return both dice. roll_dice不返回两个骰子。 Also, rand() will give you the same sequence each time.此外, rand() 每次都会给你相同的序列。

You don't show the outcome!你不显示结果!

Code-wise there's a few things I'd change代码方面我会改变一些事情

Use braces even for single statements.即使对于单个语句也使用大括号。

Get rid of global vars balance & sum摆脱全局变量balancesum

Here:这里:

while(balance>0){
    int Ask_User_for_Bet(int b);
    int rollDice();
    int Win_or_Lose();
}

the loop body is actually empty, because you haven't placed any statements, but prototype declarations.循环体实际上是空的,因为您没有放置任何语句,而是原型声明。 This, for example:这,例如:

    int Ask_User_for_Bet(int b);

just tells the compiler that there is a function Ask_User_for_Bet that takes an int and that returns an int .只是告诉编译器有一个函数Ask_User_for_Bet接受一个int并返回一个int This is equivalent (and redundant with) the same declarations made further above.这与上面进一步做出的相同声明等效(并且是多余的)。

You want to call the functons instead, so:您想改为调用函数,因此:

while(balance>0){
    Ask_User_for_Bet(balance);
    rollDice();
    Win_or_Lose();
}

The call doesn't include any type information and just passed the desired arguments.该调用不包含任何类型信息,只是传递了所需的参数。

(Note that you will set sum to something else than the return value in rollDice() – you roll the die twice, when you want to roll it only once. And rolling a twelve-sided die once isn't the ame as rolling two six-sided dice. You will also have to check whether the balance can hold the bet and you have to do to balance, so that the while condition will become false at some time.) (请注意,您将把sum设置为rollDice()的返回值以外的其他值——当您只想掷一次时,您将骰子掷两次。并且掷一次十二面骰与掷两次不同六面骰子。你还要检查余额是否可以保持赌注,你必须做平衡,这样while条件会在某个时候变成假。)

As posted by others before there are many mistakes in your code.正如其他人之前发布的那样,您的代码中有很多错误。 Instead of pointing them, I wrote some example code you can use (i tested it, it works) for your problem.我没有指出它们,而是编写了一些示例代码,您可以使用(我测试过,它有效)解决您的问题。 You can study it and see where you have been wrong.你可以研究它,看看你哪里错了。 For example your scanf's are no written correctly, they need a & operator, the rand will give you the same sequence each time, it needs a seed at start.例如,您的 scanf 没有正确编写,它们需要一个 & 运算符,rand 每次都会给您相同的序列,它在开始时需要一个种子。 Here is how you should use it in your main before running rand:在运行 rand 之前,您应该如何在主程序中使用它:

 int seed = time(NULL);
 srand(seed);

below is the code下面是代码

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


int Ask_User_for_Balance(void);
int Ask_User_for_Bet(int balance_copy);
int rollDice(void);


int main(void){

    int balance=5; //something more than 0 (not necessary as you input the value later)
    int bet=0;
    int sum=0;  //the roll dice result

    //seed for random function
    int seed = time(NULL);
    srand(seed);

// enter balance    
    balance= Ask_User_for_Balance();

// main game loop

    while(balance>0){       
        bet= Ask_User_for_Bet(balance);
        sum= rollDice();
        printf ("dice roll is: %d\n", sum);

        //win or lose implementation
        if (sum==7 || sum==11){
            balance= balance + (bet*3);
             printf("You won! Current balance: %d \n ",balance);
        }
        else{
            balance=balance - bet;
            printf("You lost! Current balance: %d \n ",balance);

        }
    } // close while

    printf("Game Over...\n");

    return 0;
}


int Ask_User_for_Balance(void){

    int balance_temp;
    printf("Enter Balance\n");
    scanf("%d", &balance_temp);

    return balance_temp;

}

int Ask_User_for_Bet(int balance_copy){
    int bet_temp=0;

    //do not let the player bet more than his balance
    do{
        printf("Enter bet amount\n");
        scanf("%d", &bet_temp);
        if(bet_temp>balance_copy){
            printf("Please bet less than your balance\n");
        }
    }while (bet_temp>balance_copy);

    return bet_temp;
}


int rollDice(void) {
    int sum_temp;
    sum_temp=((rand() % 12) + 1);
    return sum_temp;
}

You have a large number of both logic and syntax errors in your code.您的代码中存在大量逻辑和语法错误。 Always compile with compiler warnings enabled (by adding -Wall -Wextra to your gcc compile string, or by adding /W3 to your cl.exe (VS) compile string).始终在启用编译器警告的情况下进行编译(通过将-Wall -Wextra添加到您的gcc编译字符串,或通过将/W3添加到您的cl.exe (VS) 编译字符串)。

Read and fix all warnings.阅读并修复所有警告。 Never accept code until it compiles cleanly, without any warning.永远不要接受代码,直到它干净地编译,没有任何警告。

Validate ALL user input by checking the return of scanf (or any input function for that matter).通过检查scanf (或任何输入函数)的返回来验证所有用户输入 You can further validate that any input received falls within the range of expected values.您可以进一步验证收到的任何输入是否在预期值范围内。

Order your code so that your functions logically work together, and make use of the function returns.对您的代码进行排序,以便您的函数在逻辑上协同工作,并利用函数返回。 Do not declare global variables when they can be passed as parameters.当全局变量可以作为参数传递时,不要声明它们。

Putting that altogether, you could tweak your code similar to the following:总而言之,您可以调整类似于以下内容的代码:

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

int get_user_bet (void);
int dice_roll (void);
int win_or_lose (int roll, int bet);

int main (void)
{
    int balance = 0, previous = 0, bet = 0, roll = 0, ch = 0;
    char c = 0;

    printf ("enter opening balance: $");
    if (scanf ("%d", &balance) != 1) {
        fprintf (stderr, "error: invalid input - balance.\n");
        return 1;
    }

    if (balance <= 0) {
        fprintf (stderr, "error: balance 0 or negative.\n");
        return 1;
    }

    while (c != 'n' && c != 'N' && balance > 0) {
        previous = balance;     /* save starting balance as previous */
        bet = get_user_bet();   /* get user bet */

        balance -= bet;         /* subtract from balance (at risk) */
        roll = dice_roll();     /* roll the dice */
        balance += win_or_lose (roll, bet); /* update bal. with win */

        printf ("bal. before: %d  bal. after: %d\n", previous, balance);
        if (balance > previous)
            printf ("\nYou won! Play again? "); /* display win */
        else
            printf ("\nYou lost Play again? "); /* display loss */

        if (scanf (" %c", &c) != 1) {   /* get answer to play again */
            fprintf (stderr, "error: user canceled.\n");
            break;
        }
        /* note you should clear any remaining chars from stdin here */
        for (ch = getchar(); ch != '\n' && ch != EOF; ch = getchar()) {}
    }

    return 0;
}

int get_user_bet()
{
    int b = 0;

    printf ("Enter bet amount: ");
    if (scanf ("%d", &b) != 1)
        return 0;

    return b;
}

int dice_roll (void)
{
    return rand() % 12 + 1;
}

int win_or_lose (int roll, int bet)
{
    printf ("roll was: %d\n", roll);

    if (roll == 7 || roll == 11)
        return bet + 3 * bet;

    return 0;
}

Example Use/Output示例使用/输出

$ >bin\diceroll.exe
enter opening balance: $30
Enter bet amount: 10
roll was: 6
bal. before: 30  bal. after: 20

You lost Play again? y
Enter bet amount: 10
roll was: 12
bal. before: 20  bal. after: 10

You lost Play again? y
Enter bet amount: 10
roll was: 11
bal. before: 10  bal. after: 40

You won! Play again? y
Enter bet amount: 10
roll was: 5
bal. before: 40  bal. after: 30

You lost Play again? n

Look things over and let me know if you have any additional questions I can help you with.仔细查看,如果您有任何其他问题,请告诉我,我可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM