Browse Source

prod/test: created calculateDamage function and Unittests

remotes/origin/thirdcycle
Kai Kehres 2 years ago
parent
commit
0e26ef798c
  1. 22
      src/c/character.c
  2. 7
      src/c/character.h
  3. 21
      test/c/test_character.c

22
src/c/character.c

@ -122,12 +122,12 @@ int setWeaponDurability (Weapon *weapon, int newDurability) {
weapon->durability = newDurability; weapon->durability = newDurability;
} }
void setWeaponClass (Character* character, int weaponClass) {
character->weapon = weaponClass;
void setCharacterWeaponClass (Character* character, int weaponClass) {
character->weaponClass = weaponClass;
} }
int getWeaponClass (Character* character) {
return character->weapon;
int getCharacterWeaponClass (Character* character) {
return character->weaponClass;
} }
void increaseStat (Character *character, int stat, int amount) { void increaseStat (Character *character, int stat, int amount) {
@ -158,4 +158,18 @@ void levelUp (Character *character) {
increaseStat(character,LEVEL,1); increaseStat(character,LEVEL,1);
setCharacterExp(character,getCharacterExp(character)-getCharacterMaxExp(character)); setCharacterExp(character,getCharacterExp(character)-getCharacterMaxExp(character));
} }
}
int calculateDamage (Character *character,Weapon *weapon) {
switch (character->weaponClass)
{
case 1:
return character->strength*weapon->attack;
case 2:
return character->dexterity*weapon->attack;
case 3:
return character->intelligence*weapon->attack;
default:
return 1;
}
} }

7
src/c/character.h

@ -3,7 +3,7 @@
typedef struct { typedef struct {
int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp;
int attack,armor,maxHealthPoints,gold,items[10],weapon;
int attack,armor,maxHealthPoints,gold,items[10],weaponClass;
char name [50]; char name [50];
} Character; } Character;
typedef struct { typedef struct {
@ -72,6 +72,10 @@ void setCharacterName (Character *character, char newName[]);
char* getCharacterName (Character *character); char* getCharacterName (Character *character);
void setCharacterWeaponClass (Character *character, int newWeaponClass);
int getCharacterWeaponClass (Character *character);
int getWeaponAttack (Weapon *weapon); int getWeaponAttack (Weapon *weapon);
void setWeaponAttack (Weapon *weapon, int newAttack); void setWeaponAttack (Weapon *weapon, int newAttack);
@ -80,4 +84,5 @@ int getWeaponDurability (Weapon *weapon);
int setWeaponDurability (Weapon *weapon, int newDurability); int setWeaponDurability (Weapon *weapon, int newDurability);
int calculateDamage (Character *character,Weapon *weapon);
#endif #endif

21
test/c/test_character.c

@ -3,6 +3,7 @@
#include "character.h" #include "character.h"
Character testCharacter; Character testCharacter;
Character testCharacter2; Character testCharacter2;
Weapon testWeapon;
void setUp(void) void setUp(void)
{ {
@ -20,6 +21,11 @@ void setUp(void)
testCharacter2.exp = 110; testCharacter2.exp = 110;
testCharacter.maxExp = 100; testCharacter.maxExp = 100;
testCharacter2.maxExp = 100; testCharacter2.maxExp = 100;
testCharacter.attack = 5;
testCharacter2.weaponClass = SWORD;
testWeapon.attack = 5;
testWeapon.durability = 100;
} }
void tearDown(void) void tearDown(void)
@ -117,4 +123,19 @@ void test_levelUp_notEnoughExp_loseExp (void) {
TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); TEST_ASSERT_EQUAL_INT(50,testCharacter.exp);
} }
void test_calculateDamage_Sword (void) {
TEST_ASSERT_EQUAL_INT(25,calculateDamage(&testCharacter2,&testWeapon));
}
void test_calculateDamage_Bow (void) {
setCharacterWeaponClass(&testCharacter2,BOW);
TEST_ASSERT_EQUAL_INT(25,calculateDamage(&testCharacter2,&testWeapon));
}
void test_calculateDamage_Staff (void) {
setCharacterWeaponClass(&testCharacter2,STAFF);
TEST_ASSERT_EQUAL_INT(35,calculateDamage(&testCharacter2,&testWeapon));
}
#endif // TEST #endif // TEST
Loading…
Cancel
Save