Struct + Enum Monsters!

PHOTO EMBED

Mon Jun 10 2024 12:06:37 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #enum #struct

#include <stdio.h>

enum Monster_Type
{
    ZOMBIE, WEREWOLF, VAMPIRE, MUMMY, CHANGELING
};

struct Monster
{
    char type [20];
    int age; 
    int power; 
    float speed;
    int stamina;
};

struct Monster create_monster(enum Monster_Type type);

void print_monster(struct Monster monster);

int main(void)
{
	struct Monster monster1 = create_monster(CHANGELING);
	print_monster(monster1);

	struct Monster monster2 = create_monster(MUMMY);
	print_monster(monster2);

	struct Monster monster3 = create_monster(WEREWOLF);
	print_monster(monster3);

	struct Monster monster4 = create_monster(VAMPIRE);
	print_monster(monster4);

	struct Monster monster5 = create_monster(ZOMBIE);
	print_monster(monster5);

	return 0;
}

// TODO: Define functions here:
struct Monster create_monster(enum Monster_Type type)
{   
    struct Monster monster;
    
    if(type == CHANGELING)
    {
        sprintf(monster.type,"Changeling");
        monster.age = 21;
        monster.power = 86;
        monster.speed = 6.77;
        monster.stamina = 4057;
    }
    if(type == MUMMY)
    {
        sprintf(monster.type,"Mummy");
        monster.age = 3793;
        monster.power = 97;
        monster.speed = 0.37;
        monster.stamina = 492;
    }
    if(type == WEREWOLF)
    {
        sprintf(monster.type,"Werewolf");
        monster.age = 70;
        monster.power = 144;
        monster.speed = 20.53;
        monster.stamina = 4628;
    }
    if(type==VAMPIRE)
    {
        sprintf(monster.type,"Vampire");
        monster.age = 89;
        monster.power = 260;
        monster.speed = 10.08;
        monster.stamina = 3926;
    }

    if(type==ZOMBIE)
    {
        sprintf(monster.type,"Zombie");
        monster.age = -1;
        monster.power = 1;
        monster.speed = 0.30;
        monster.stamina = 173;
    }
    return monster;
}

void print_monster(struct Monster monster)
{
    printf("Monster Type: %s\n",monster.type);
	printf("         Age: %d years\n",monster.age);
	printf("       Power: %d \n",monster.power);
	printf("       Speed: %.2f \n",monster.speed);
	printf("     Stamina: %d \n",monster.stamina);
}
content_copyCOPY

Result: Monster Type: Changeling Age: 21 years Power: 86 Speed: 6.77 Stamina: 4057 Monster Type: Mummy Age: 3793 years Power: 97 Speed: 0.37 Stamina: 492 Monster Type: Werewolf Age: 70 years Power: 144 Speed: 20.53 Stamina: 4628 Monster Type: Vampire Age: 89 years Power: 260 Speed: 10.08 Stamina: 3926 Monster Type: Zombie Age: -1 years Power: 1 Speed: 0.30 Stamina: 173 Objective Your task is to complete the given C source code to create and print details of various monster types. You need to generate random statistics for each monster within specified ranges. Program Components Enums and Structures: enum Monster_Type includes types like ZOMBIE, WEREWOLF, VAMPIRE, MUMMY, CHANGELING. struct Monster contains members like type (from the Monster_Type enum), age, power, speed, and stamina. Declare and Define Functions: create_monster(enum Monster_Type type): Takes an enum Monster_Type as a parameter. Returns a struct Monster with random statistics within the specified range for each type. print_monster(struct Monster monster): Takes a struct Monster as a parameter. Prints the monster details in the specified format. Main Function: Calls create_monster to create one monster of each type specified by the Monster_Type enumeration. Passes each created monster to the print_monster function for display.