用C语言编写一个骰子游戏,但是我不能正确地打印出点数

p8h8hvxi  于 2023-06-28  发布在  其他
关注(0)|答案(2)|浏览(93)

问题是在函数中,当回合类型为“Bonus”时,它应该打印200点,但它没有,而是打印出回合类型为“Double”的其他值。

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

enum ROUNDTYPE{ Bonus, Double, Regular};

// Generates a psuedo random number with parameters of max and min
int getRandomNumber(int min, int max)
{   
    //  start random number generation
    srand(time(0));
    //  random number of determined max and min
    int rndmNum = (rand() % max) + min;
    return rndmNum;
}
// Will determine the type of Round that will take place
enum ROUNDTYPE getRoundType()
{   
    // declaring roundtype
    enum ROUNDTYPE roundtype; 
    // random number of 1-10
    int max = 10;
    int min = 1;

    int rT = (getRandomNumber(min, max));
    // conditionals for roundtype

    if (rT <= 2)
    {   
        // Bonus: replace “points” to be equal to 200 as calculated above using a random number generator.
        roundtype = Bonus; 
        printf("\nType    : Bonus");
    }
    else if (rT >= 3 && rT <= 5)
    {   
        // Double: update “points” to be equal to DOUBLE the number of points 
        roundtype = Double; 
        printf("\nType    : Double");
    }
    else if (rT >= 5 && rT <= 10)
    {   
        // Regular: keep the “points” equal to the number of points as calculated above using a random number generator.
        roundtype = Regular; 
        printf("\nType    : Regular");
    }
    else
    {
        printf("\nError");
    }
}

int getRoundPoints(enum ROUNDTYPE roundtype)
{   
    // points variable
    int points;
    // conditionals to calculate points by roundtype
    if (roundtype = Bonus)
    {
        // print out points (10 - 100) in multiples of 10
        points = 200;
    }
    else if (roundtype = Double)
    {
        points = (((rand() % 10) + 1) * 20);    
    }
    else if (roundtype = Regular)
    {
        points = (((rand() % 10) + 1) * 10);
    }
    printf("\nPoints  : %d", points);
}
/*
void printPlayerPoints( int p1, int p2)
{

}

void printRoundInfo( ROUNDTYPE t, int dice, int points)
{

}
*/
int main()
{   
    
    int min = 1;
    int max = 6;
    printf("%d", getRandomNumber(min, max));
    //getRoundType();
    getRoundPoints(getRoundType());

    return 0;
}`

问题是在函数中,当回合类型为“Bonus”时,它应该打印200点,但它没有,而是打印出回合类型为“Double”的其他值。
任何帮助或建议将不胜感激。
下面是输出,因为你可以看到if不遵循条件if语句:

我认为调用ROUNDTYPE函数将该信息提供给getRoundPoints函数是不正确的

68bkxrlz

68bkxrlz1#

minmax之间随机化的结果应该是:

int rndmNum = rand() % (max - min + 1);
return min + rndmNum;

但这并不意味着负数。
主要错误是使用=(赋值)来比较==。在C语言中,这是一个弱点,通常可以看到以下编码约定:常数第一这可以防止意外的分配/打字错误。

if (Double == roundtype)

编译时始终使用较大的警告级别,因为C在接受非预期构造方面是臭名昭著的。(或者从高级编程语言开始。

hyrbngr7

hyrbngr72#

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//#include "dicegame.h"

enum ROUNDTYPE{ Bonus, Double, Regular};

// Generates a psuedo random number with parameters of max and min
int getRandomNumber(int min, int max)
{   
    //  random number of determined max and min
    int rndmNum = (rand() % (max-min+1)) + min;
    return rndmNum;
}
// Will determine the type of Round that will take place
enum ROUNDTYPE getRoundType()
{   
    // declaring roundtype
    enum ROUNDTYPE roundtype; 
    // random number of 1-10
    int max = 10;
    int min = 1;

    int rT = (getRandomNumber(1, 10));
    // conditionals for roundtype

    if (rT <= 2)
    {   
        // Bonus: replace “points” to be equal to 200 as calculated above using a random number generator.
        roundtype = Bonus; 
        printf("\nType    : Bonus");
    }
    else if (rT >= 5)
    {   
        // Regular: keep the “points” equal to the number of points as calculated above using a random number generator.
        roundtype = Regular; 
        printf("\nType    : Regular");
    }
    else
    {   
        // Double: update “points” to be equal to DOUBLE the number of points 
        roundtype = Double; 
        printf("\nType    : Double");
    }
    return roundtype;
}

int getRoundPoints(enum ROUNDTYPE roundtype)
{   
    // points variable
    int points;
    // conditionals to calculate points by roundtype
    if (roundtype == Bonus)
    {
        // print out points (10 - 100) in multiples of 10
        points = 200;
    }
    else if (Double == roundtype)
    {   
        points = getRandomNumber(1, 10) * 20;    
    }
    else
    {
        points = getRandomNumber(1, 10) * 10;
    }
    printf("\nPoints  : %d", points);
    return points; // add to return points
}
/*
void printPlayerPoints( int p1, int p2)
{

}

void printRoundInfo( ROUNDTYPE t, int dice, int points)
{

}
*/
int main()
{
    srand(time(0));
    int min = 1;
    int max = 6;
    printf("%d", getRandomNumber(min, max));

    enum ROUNDTYPE roundtype = getRoundType(); // Store the return value
    getRoundPoints(roundtype); // Pass the roundtype variable

    return 0;
}

所以这是我的代码将建议我看到的地方,我已经更新了代码,发现我也错过了getRoundType函数的返回roundtype。以及main函数更改以存储返回值

相关问题