diff --git a/src/c/character.c b/src/c/character.c new file mode 100644 index 0000000..9c436ff --- /dev/null +++ b/src/c/character.c @@ -0,0 +1,97 @@ +#include +#include +#include + +#include "character.h" + +Character character; + +void setCharacterHealthPoints (Character *character, int newHealthPoints){ + character->healthPoints = newHealthPoints; +} + +int getCharacterHealthPoints (Character *character) { + return character->healthPoints; +} + +void setCharacterStrength (Character *character, int newStrength) { + character->strength = newStrength; +} + +int getCharacterStrength (Character *character) { + return character->strength; +} + +void setCharacterDexterity (Character *character, int newDexterity) { + character->dexterity = newDexterity; +} + +int getCharacterDexterity (Character *character) { + return character->dexterity; +} + +void setCharacterIntelligence (Character *character, int newIntelligence) { + character->intelligence = newIntelligence; +} + +int getCharacterIntelligence (Character *character) { + return character->intelligence; +} + +void setCharacterLevel (Character *character, int newLevel) { + character->level = newLevel; +} + +int getCharacterLevel (Character *character) { + return character->level; +} + +void setCharacterExp (Character *character, int newExp) { + character->exp = newExp; +} + +int getCharacterExp (Character *character) { + return character->exp; +} + +void setCharacterMaxExp (Character *character, int newMaxExp) { + character->maxExp = newMaxExp; +} + +int getCharacterMaxExp (Character *character) { + return character->maxExp; +} + +int calculateStatIncrease (Character *character, int amount) { + return character->level*amount; +} + +void increaseStat (Character *character, int stat, int amount) { + switch (stat) + { + case 1: + character->strength += amount;break; + case 2: + character->dexterity += amount;break; + case 3: + character->intelligence += amount;break; + case 4: + character->healthPoints += amount;break; + case 5: + character->manaPoints += amount;break; + case 6: + character->level += amount;break; + case 7: + character->exp += amount;break; + case 8: + character->maxExp += amount;break; + default: + break; + } +} +void levelUp (Character *character) { + if (getCharacterExp(character) > getCharacterMaxExp(character)) { + increaseStat(character,LEVEL,1); + setCharacterExp(character,getCharacterExp(character)-getCharacterMaxExp(character)); + } +} \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h new file mode 100644 index 0000000..7ed09b4 --- /dev/null +++ b/src/c/character.h @@ -0,0 +1,46 @@ +#ifndef CHARACTER_H +#define CHARACTER_H + +typedef struct { + int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; + char name [50]; +} Character; + +enum { + STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6, EXP = 7, MAXEXP = 8 +}; + +void setCharacterHealthPoints (Character *character, int newHealthPoints); + +int getCharacterHealthPoints (Character *character); + +void setCharacterStrength (Character *character, int newStrength); + +int getCharacterStrength (Character *character); + +void setCharacterDexterity (Character *character, int newDexterity); + +int getCharacterDexterity (Character *character); + +void setCharacterIntelligence (Character *character, int newIntelligence); + +int getCharacterIntelligence (Character *character); + +void setCharacterLevel (Character *character, int newLevel); + +int getCharacterLevel (Character *character); + +void setCharacterExp (Character *character, int newExp); + +int getCharacterExp (Character *character); + +void setCharacterMaxExp (Character *character, int newMaxExp); + +int getCharacterMaxExp (Character *character); + +void increaseStat (Character *character, int stat, int amount); + +int calculateStatIncrease (Character *character, int amount); + +void levelUp (Character *character); +#endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c new file mode 100644 index 0000000..6d9fd46 --- /dev/null +++ b/test/c/test_character.c @@ -0,0 +1,120 @@ +#ifdef TEST +#include "unity.h" +#include "character.h" +Character testCharacter; +Character testCharacter2; + +void setUp(void) +{ + testCharacter.healthPoints = 0; + testCharacter2.healthPoints = 15; + testCharacter.strength = 0; + testCharacter2.strength = 5; + testCharacter.dexterity = 0; + testCharacter2.dexterity = 5; + testCharacter.intelligence = 0; + testCharacter2.intelligence = 7; + testCharacter.level = 0; + testCharacter2.level = 5; + testCharacter.exp = 50; + testCharacter2.exp = 110; + testCharacter.maxExp = 100; + testCharacter2.maxExp = 100; +} + +void tearDown(void) +{ +} + +void test_setCharacterHealthPoints(void) +{ + TEST_ASSERT_EQUAL_INT(0,testCharacter.healthPoints); + setCharacterHealthPoints(&testCharacter,50); + TEST_ASSERT_EQUAL_INT(50,testCharacter.healthPoints); +} + +void test_getCharacterHealthPoints (void) { + TEST_ASSERT_EQUAL_INT(15,getCharacterHealthPoints(&testCharacter2)); +} + +void test_setCharacterStrenght(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.strength); + setCharacterStrength(&testCharacter,50); + TEST_ASSERT_EQUAL_INT(50,testCharacter.strength); +} + +void test_getCharacterStrength(void) { + TEST_ASSERT_EQUAL_INT(5,getCharacterStrength(&testCharacter2)); +} + +void test_setCharacterDexterity(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.dexterity); + setCharacterDexterity(&testCharacter,50); + TEST_ASSERT_EQUAL_INT(50,testCharacter.dexterity); +} + +void test_getCharacterDexterity(void) { + TEST_ASSERT_EQUAL_INT(5,getCharacterDexterity(&testCharacter2)); +} + +void test_setCharacterIntelligence(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.intelligence); + setCharacterIntelligence(&testCharacter,45); + TEST_ASSERT_EQUAL_INT(45,testCharacter.intelligence); +} + +void test_getCharacterIntelligence(void) { + TEST_ASSERT_EQUAL_INT(7,getCharacterIntelligence(&testCharacter2)); +} + +void test_setCharacterLevel(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.level); + setCharacterLevel(&testCharacter,1); + TEST_ASSERT_EQUAL_INT(1,testCharacter.level); +} + +void test_getCharacterLevel(void) { + TEST_ASSERT_EQUAL_INT(5,getCharacterLevel(&testCharacter2)); +} + +void test_increaseStat(void) { + increaseStat(&testCharacter2,STRENGTH,15); + TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); + increaseStat(&testCharacter2,INTELLIGENCE,15); + TEST_ASSERT_EQUAL_INT(22,testCharacter2.intelligence); + increaseStat(&testCharacter2,LEVEL,15); + TEST_ASSERT_EQUAL_INT(20,testCharacter2.level); +} + +void test_calculateStatIncrease(void) { + TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); + TEST_ASSERT_EQUAL_INT(25,calculateStatIncrease(&testCharacter2,5)); +} + +void test_levelUp_enoughExp (void) { + TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); + levelUp(&testCharacter2); + TEST_ASSERT_EQUAL_INT(6,testCharacter2.level); +} + +void test_levelUp_enoughExp_loseExp (void) { + TEST_ASSERT_EQUAL_INT(110,testCharacter2.exp); + TEST_ASSERT_EQUAL_INT(100,testCharacter2.maxExp); + levelUp(&testCharacter2); + TEST_ASSERT_EQUAL_INT(10,testCharacter2.exp); +} + +void test_levelUp_notEnoughExp (void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.level); + levelUp(&testCharacter); + TEST_ASSERT_EQUAL_INT(0,testCharacter.level); +} + +void test_levelUp_notEnoughExp_loseExp (void) { + TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); + TEST_ASSERT_EQUAL_INT(100,testCharacter.maxExp); + levelUp(&testCharacter); + TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); +} + +#endif // TEST \ No newline at end of file