[C] character generator for a game help

Discussion in 'C' started by iroattoyou, May 28, 2014.

  1. iroattoyou

    iroattoyou New Member

    Joined:
    May 28, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Question:



    Code I have thusfar:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define MAX_STAT
    
    
    enum {
        STR, DEX, INT, CON, WIS, CHA, LEVEL, MAX_STAT
    };
    struct Character {
        int level;
        int class;
        int HP;
        int bonus[8];
        int stats[MAX_STAT];
    };
    struct Character myCharacter;
    void roll_stat(int *stat_to_be_rolled);
    void get_bonus(int *bonus_to_be_calculated, int base_stat);
    int roll_hp(int char_level);
    int main(void)
    {
        /* Declare stat variables */
        char stats_label[7][6];
        /* = "Level", "Str", "Dex", "Con", "Int", "Wis", "Cha"; */
        /* Declare control variables */
        int stat_loop, accept = 0;
        /* Seed the random number generator */
        srand((unsigned) time(NULL));
        /* Store labels in a string. This helps us later on to easily print them */
        strcpy(stats_label[0], "Level");
        strcpy(stats_label[1], "Str");
        strcpy(stats_label[2], "Dex");
        strcpy(stats_label[3], "Con");
        strcpy(stats_label[4], "Int");
        strcpy(stats_label[5], "Wis");
        strcpy(stats_label[6], "Cha");
        while (!accept) {
    	/* prompt and read in level */
    	printf("Please enter Level: ");
    	scanf("%d", &myCharacter.stats[0]);
    	/* roll stats */
    	for (stat_loop = 1; stat_loop < 7; stat_loop++)
    	    roll_stat(&myCharacter.stats[stat_loop]);
    	/* calculate bonuses */
    	myCharacter.bonus[0] = roll_hp(myCharacter.stats[0]);
    	for (stat_loop = 1; stat_loop < 7; stat_loop++)
    	    get_bonus(&myCharacter.bonus[stat_loop],
    		      myCharacter.stats[stat_loop]);
    	/* print the stats out for acceptance */
    	printf("%s: %d ", stats_label[0], myCharacter.stats[0]);
    	for (stat_loop = 1; stat_loop < 7; stat_loop++)
    	    printf("%s: %d %d ", stats_label[stat_loop],
    		   myCharacter.stats[stat_loop],
    		   myCharacter.bonus[stat_loop]);
    	printf("Hitpoints: %d\n", myCharacter.bonus[0]);
    	/* check for loop exit condition */
    	printf
    	    ("Do you want to accept these? (enter 1 to accept, 0 to reroll): ");
    	scanf("%d", &accept);
        }
        /* print stats in required formatted block */
        printf("[%s] [%d]\n", stats_label[0], myCharacter.stats[0]);
        for (stat_loop = 1; stat_loop < 7; stat_loop++)
    	printf("[%s]: [%d] [%d] \n", stats_label[stat_loop],
    	       myCharacter.stats[stat_loop], myCharacter.bonus[stat_loop]);
        printf("[Hitpoints]: [%d]\n", myCharacter.bonus[0]);
        return (0);
    }
    
    /* Roll 4d6 and drop lowest */
    void roll_stat(int *stat_to_be_rolled)
    {
        int dice1, dice2, dice3, dice4, total;
        /* Roll 4d6 */
        dice1 = (rand() % 6 + 1);
        dice2 = (rand() % 6 + 1);
        dice3 = (rand() % 6 + 1);
        dice4 = (rand() % 6 + 1);
        /* Find lowest dice and add result of the other 3 */
        if ((dice1 < dice2) && (dice1 < dice3) && (dice1 < dice4))
    	total = dice2 + dice3 + dice4;
        else if ((dice2 < dice1) && (dice2 < dice3) && (dice2 < dice4))
    	total = dice1 + dice3 + dice4;
        else if ((dice3 < dice1) && (dice3 < dice2) && (dice3 < dice4))
    	total = dice1 + dice2 + dice4;
        else
    	total = dice1 + dice2 + dice3;
        /* assign total value to the memory location we were given */
        *stat_to_be_rolled = total;
    }
    
    /* This was extracted as a function to make the bonus loop more compact */
    void get_bonus(int *bonus_to_be_calculated, int base_stat)
    {
        if (base_stat > 9)
    	*bonus_to_be_calculated = (base_stat - 10) / 2;
        else
    	*bonus_to_be_calculated = (base_stat - 11) / 2;
    }
    
    /* This was extracted as a function to make the HP loop more compact */
    int roll_hp(int char_level)
    {
        int hploop = 0, hp = 0;
        /* printf("Leve1: %d", char_level); For testing */
        for (hploop = 0; hploop < char_level; hploop++)
    	hp += (rand() % 6 + 1);
        return (hp);
    }
    d y : a dice of Y sides (ie d6)

    x d y: X number of Y sided dice (ie: 3d6 = 3 x 6 sided dice)

    Hitdice: The type of dice rolled for hit-points or a Class/Profession. (ie: d4, d6, d8)

    Skill Structure: struct Skill { char name[20], char optional[20], char short_desc[250], int stat_affinity, int ranks, struct Skill *next_Skill;};

    Class Structure: struct Class { char name[20]; int Hitdice, Str_Dice, Dex_Dice, Con_Dice, Int_Dice, Wis_Dice, Cha_Dice, Skill_Points; double BAB_Type; struct Class *next_Class;)};

    Is my hitpoint formula correct? Does my code seem to satisfy the question requirements? Are my Hitdice and Class structures correct? If not, how would one do this?
     
  2. iroattoyou

    iroattoyou New Member

    Joined:
    May 28, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Question: i.gyazo.com/5a854aa3d6f797ce242b4374db03e85e.png
    i.gyazo.com/75ddda9e22ba67c54b8963faf5170a4e.png
     
  3. iroattoyou

    iroattoyou New Member

    Joined:
    May 28, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Context: gyazo.com/3b5e9878a6fd235e45eb2e3e6d56ebf4 gyazo.com/2b439244c28e64d4320c273fb3617579
    i.imgur.com/FguROec.png
    i.imgur.com/JWpphi7.png
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The best way to answer this is to run a few trials on paper based on your understanding of the questions. Use a predetermined sequence of numbers (generated by throwing dice if you like) and try to pick numbers that will test the limits of your software.

    Then replace the calls to rand() with calls to a function that will return that same sequence of numbers and check if you get the same results. If you don't, then start looking through your code to try to find out why.

    It can help to simplify your code to work on one thing at a time, instead of trying to do it all in one go. Write, simulate, test and debug one thing before moving onto the next; that's what the professionals do.
     
    1 person likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice