【问题标题】:Adding the result of X dices with n sides and a constant in C在 C 中添加具有 n 面和常数的 X 骰子的结果
【发布时间】:2021-08-17 02:57:24
【问题描述】:

我必须编写一个程序,在其中我必须将 x 骰子的结果与 n 面加或减一个常数(C)相加。输入应该是这样的字符串:“xDn+-C”(x、n 和 C 必须是十进制数)。例如:“4D5+6”或“6D9-5”。 D 的意思是“骰子”。

我使用了一个函数来随机化卷,但我不知道如何继续...

void initD6(void) {
    
   srand((unsigned)time( NULL ) );
}

int D6(void) {

   return ((rand()%6)+1);
}

int main(){
   char Dice[4];

   for(i=0; i<5; i++){
     
   Dice[i] = D6();

   return 0;
}

我不知道我应该如何将该输入作为字符串以及加减法,也不知道下一步该怎么做。

【问题讨论】:

  • 如果你的任务是模拟任意骰子,为什么要为 D6 编写代码?
  • initD6 应该是initDice,并且应该从main 调用一次。 D6 应该是 rollDie 并且应该将 n 作为参数。您不需要 Dice 数组。您可以在掷骰子时计算总和。循环中的5 必须是x。对于初始开发、测试、调试,我会在main 函数中硬编码xcn。一旦你得到了掷骰子和总和编码并开始工作,那么你就可以继续阅读/解析用户输入了。

标签: arrays c dice


【解决方案1】:

假设输入中没有错误,解决您的任务的函数是:

int throw_dice(const char* s) 
{
    int num, sides, res; 
    sscanf(s,"%iD%i%i", &num, &sides, &res);
    for (int i = 0; i < num; ++i) {
        res += rand() % sides + 1;
    }
    return res; 
}

对于简单的字符串解析,sscanf() 是一个相当不错的函数。对于更复杂的任务,最好使用regular expression 库。

像往常一样,除了最简单的骰子游戏,不要在rand() 上转发任何东西,不涉及金钱。

您可以通过以下完整示例进行尝试:

#include <stdio.h>

int throw_dice(const char* s) 
{
    int num, sides, res; 
    sscanf(s,"%iD%i%i", &num, &sides, &res);
    for (int i = 0; i < num; ++i) {
        res += rand() % sides + 1;
    }
    return res; 
}

void throw_multiple_times(const char* s, int times)
{
    printf("%s: ", s);
    for (int i = 0; i < times; ++i) {
        printf("%i ", throw_dice(s));
    }
    printf("\n");
}

int main(void) 
{
    srand((unsigned)time(NULL));
    const char* s;
    
    throw_multiple_times("4D5+6", 100);
    throw_multiple_times("6D9-5", 100);

    return 0;
}

测试它here

【讨论】:

    【解决方案2】:
    1. 添加结构:
        struct rules
        {
            int dices;
            int facesPerDice;
            int offset;
        };
    
    1. 解决骰子问题:
        int throwDice(int faces)
        {
            return (rand() % faces) + 1;
        }
        
        int playGame(struct rules rules)
        {
            int result = 0;
            
            for (int i = 0; i < rules.dices; i++)
                result += throwDice(rules.facesPerDice);
            
            return result + rules.offset;
        }
    
    1. 解决解析问题:
        /**
            Converts a string to a unsigned int until an invalid character is found or a null character is found.
            
            You should replace this with the function you normally use to convert a string to a integer.
        */
        unsigned int stringToUInt(char *str)
        {
            unsigned int result = 0;
            
            int charindex = 0;
            char currentchar;
            while ((currentchar = str[charindex++]) != '\0')
            {
                if (currentchar < '0' || currentchar > '9')
                    break;
                result *= 10;
                result += currentchar - '0';
            }
            
            return result;
        }
        
        /** 
            Reads a string and generates a struct rules based on it.
            
            The string is expected to be given in the following format:
                [uint]'D'[uint]['+' or '-'][uint]
            where:
                the first uint is the number of dices to roll
                the second uint is the number of faces per dice
                the third uint is the offset
                
            Terminates the program if something goes wrong.
        */
        struct rules parse(char *str)
        {
            struct rules result;
            
            result.dices = stringToUInt(str);
            
            while (*(str++) != 'D')
                if (*str == '\0')
                    exit(1);
        
            result.facesPerDice = stringToUInt(str);
            
            while (*(str++) != '+' && *(str-1) != '-')
                if (*str == '\0')
                    exit(1);
            
            result.offset = stringToUInt(str);
            result.offset *= (*(str-1) == '+' ? 1 : -1);
            
            return result;
        }
    
    1. 把所有东西放在一起:
        int main(int argc, char *argv[])
        {
            srand(time(NULL));
    
            char input[] = "3D6+9"; //You could use console input if you want
            struct rules rules = parse(input);
            int gameResult = playGame(rules);
            printf("Game result: %d\n", gameResult);
    
            return 0;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2019-03-13
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多