Browse Source

prod/test: added new stats to increase stat function

created levelUp function and 2 Unittests test_levelUp_*
remotes/origin/characterStats
Kai Kehres 2 years ago
parent
commit
3f03db161e
  1. 12
      src/c/character.c
  2. 4
      src/c/character.h
  3. 24
      test/c/test_character.c

12
src/c/character.c

@ -66,7 +66,6 @@ int calculateStatIncrease (Character *character, int amount) {
return character->level*amount; return character->level*amount;
} }
void increaseStat (Character *character, int stat, int amount) { void increaseStat (Character *character, int stat, int amount) {
switch (stat) switch (stat)
{ {
@ -80,7 +79,18 @@ void increaseStat (Character *character, int stat, int amount) {
character->healthPoints += amount;break; character->healthPoints += amount;break;
case 5: case 5:
character->manaPoints += amount;break; character->manaPoints += amount;break;
case 6:
character->level += amount;break;
case 7:
character->exp += amount;break;
case 8:
character->maxExp += amount;break;
default: default:
break; break;
} }
} }
void levelUp (Character *character) {
if (getCharacterExp(character) > getCharacterMaxExp(character)) {
increaseStat(character,LEVEL,1);
}
}

4
src/c/character.h

@ -7,7 +7,7 @@ typedef struct {
} Character; } Character;
enum { enum {
STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6
STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6, EXP = 7, MAXEXP = 8
}; };
void setCharacterHealthPoints (Character *character, int newHealthPoints); void setCharacterHealthPoints (Character *character, int newHealthPoints);
@ -41,4 +41,6 @@ int getCharacterMaxExp (Character *character);
void increaseStat (Character *character, int stat, int amount); void increaseStat (Character *character, int stat, int amount);
int calculateStatIncrease (Character *character, int amount); int calculateStatIncrease (Character *character, int amount);
void levelUp (Character *character);
#endif #endif

24
test/c/test_character.c

@ -16,6 +16,10 @@ void setUp(void)
testCharacter2.intelligence = 7; testCharacter2.intelligence = 7;
testCharacter.level = 0; testCharacter.level = 0;
testCharacter2.level = 5; testCharacter2.level = 5;
testCharacter.exp = 50;
testCharacter2.exp = 110;
testCharacter.maxExp = 100;
testCharacter2.maxExp = 100;
} }
void tearDown(void) void tearDown(void)
@ -78,10 +82,30 @@ void test_increaseStat(void) {
TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength);
increaseStat(&testCharacter2,INTELLIGENCE,15); increaseStat(&testCharacter2,INTELLIGENCE,15);
TEST_ASSERT_EQUAL_INT(22,testCharacter2.intelligence); TEST_ASSERT_EQUAL_INT(22,testCharacter2.intelligence);
increaseStat(&testCharacter2,LEVEL,15);
TEST_ASSERT_EQUAL_INT(20,testCharacter2.level);
} }
void test_calculateStatIncrease(void) { void test_calculateStatIncrease(void) {
TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); TEST_ASSERT_EQUAL_INT(5,testCharacter2.level);
TEST_ASSERT_EQUAL_INT(25,calculateStatIncrease(&testCharacter2,5)); TEST_ASSERT_EQUAL_INT(25,calculateStatIncrease(&testCharacter2,5));
} }
void test_levelUp_enoughExp (void) {
TEST_ASSERT_EQUAL_INT(110,testCharacter2.exp);
TEST_ASSERT_EQUAL_INT(100,testCharacter2.maxExp);
TEST_ASSERT_EQUAL_INT(5,testCharacter2.level);
levelUp(&testCharacter2);
TEST_ASSERT_EQUAL_INT(6,testCharacter2.level);
}
void test_levelUp_notEnoughExp (void) {
TEST_ASSERT_EQUAL_INT(50,testCharacter.exp);
TEST_ASSERT_EQUAL_INT(100,testCharacter.maxExp);
TEST_ASSERT_EQUAL_INT(0,testCharacter.level);
levelUp(&testCharacter);
TEST_ASSERT_EQUAL_INT(0,testCharacter.level);
}
#endif // TEST #endif // TEST
Loading…
Cancel
Save