繁体   English   中英

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

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

所以我必须编写一个完整的程序来要求用户输入金额并将其存储在变量名称 balance 中,然后向用户询问下注金额。 如果金额小于或等于余额金额,则掷骰子。 如果总点数为 7 或 11,则玩家赢得赌注的三倍。 否则,玩家输掉赌注。 只要他/她有余额,玩家就应该能够下注。

我真的不知道从这里(从下面发布的代码中)还有什么可做的,所以请帮我修复它。

#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?");



}

这里有很多错误。

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
    }

你在哪里减少余额?

roll_dice不返回两个骰子。 此外, rand() 每次都会给你相同的序列。

你不显示结果!

代码方面我会改变一些事情

即使对于单个语句也使用大括号。

摆脱全局变量balancesum

这里:

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

循环体实际上是空的,因为您没有放置任何语句,而是原型声明。 这,例如:

    int Ask_User_for_Bet(int b);

只是告诉编译器有一个函数Ask_User_for_Bet接受一个int并返回一个int 这与上面进一步做出的相同声明等效(并且是多余的)。

您想改为调用函数,因此:

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

该调用不包含任何类型信息,只是传递了所需的参数。

(请注意,您将把sum设置为rollDice()的返回值以外的其他值——当您只想掷一次时,您将骰子掷两次。并且掷一次十二面骰与掷两次不同六面骰子。你还要检查余额是否可以保持赌注,你必须做平衡,这样while条件会在某个时候变成假。)

正如其他人之前发布的那样,您的代码中有很多错误。 我没有指出它们,而是编写了一些示例代码,您可以使用(我测试过,它有效)解决您的问题。 你可以研究它,看看你哪里错了。 例如,您的 scanf 没有正确编写,它们需要一个 & 运算符,rand 每次都会给您相同的序列,它在开始时需要一个种子。 在运行 rand 之前,您应该如何在主程序中使用它:

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

下面是代码

    #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;
}

您的代码中存在大量逻辑和语法错误。 始终在启用编译器警告的情况下进行编译(通过将-Wall -Wextra添加到您的gcc编译字符串,或通过将/W3添加到您的cl.exe (VS) 编译字符串)。

阅读并修复所有警告。 永远不要接受代码,直到它干净地编译,没有任何警告。

通过检查scanf (或任何输入函数)的返回来验证所有用户输入 您可以进一步验证收到的任何输入是否在预期值范围内。

对您的代码进行排序,以便您的函数在逻辑上协同工作,并利用函数返回。 当全局变量可以作为参数传递时,不要声明它们。

总而言之,您可以调整类似于以下内容的代码:

#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;
}

示例使用/输出

$ >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

仔细查看,如果您有任何其他问题,请告诉我,我可以为您提供帮助。

暂无
暂无

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

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