簡體   English   中英

在 C 中添加具有 n 面和常數的 X 骰子的結果

[英]Adding the result of X dices with n sides and a constant in C

我必須編寫一個程序,在其中我必須將 x 骰子的結果與 n 個面加或減一個常數(C)相加。 輸入應該是這樣的字符串:“xDn+-C”(x、n 和 C 必須是十進制數)。 例如:“4D5+6”或“6D9-5”。 D只是意味着“骰子”。

我使用 function 來隨機化卷,但我不知道如何繼續......

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

我不知道我應該如何將該輸入作為字符串進行加減運算,也不知道下一步該怎么做。

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

假設輸入中沒有錯誤,解決您的任務的 function 是:

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()是一個相當不錯的 function。 對於更復雜的任務,最好使用正則表達式庫。

像往常一樣,除了最簡單的骰子游戲,不要在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;
}

在這里測試一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM