From 0e26ef798c145ae4f8e9dbebe089e0d70f3a2d61 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 16:28:26 +0100 Subject: [PATCH] prod/test: created calculateDamage function and Unittests --- src/c/character.c | 22 ++++++++++++++++++---- src/c/character.h | 7 ++++++- test/c/test_character.c | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 86627f8..3ccf7ba 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -122,12 +122,12 @@ int setWeaponDurability (Weapon *weapon, int 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) { @@ -158,4 +158,18 @@ void levelUp (Character *character) { increaseStat(character,LEVEL,1); 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; + } } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index 0c083a1..dff4d81 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -3,7 +3,7 @@ typedef struct { 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]; } Character; typedef struct { @@ -72,6 +72,10 @@ void setCharacterName (Character *character, char newName[]); char* getCharacterName (Character *character); +void setCharacterWeaponClass (Character *character, int newWeaponClass); + +int getCharacterWeaponClass (Character *character); + int getWeaponAttack (Weapon *weapon); void setWeaponAttack (Weapon *weapon, int newAttack); @@ -80,4 +84,5 @@ int getWeaponDurability (Weapon *weapon); int setWeaponDurability (Weapon *weapon, int newDurability); +int calculateDamage (Character *character,Weapon *weapon); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 6d9fd46..5733e4d 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -3,6 +3,7 @@ #include "character.h" Character testCharacter; Character testCharacter2; +Weapon testWeapon; void setUp(void) { @@ -20,6 +21,11 @@ void setUp(void) testCharacter2.exp = 110; testCharacter.maxExp = 100; testCharacter2.maxExp = 100; + testCharacter.attack = 5; + testCharacter2.weaponClass = SWORD; + + testWeapon.attack = 5; + testWeapon.durability = 100; } void tearDown(void) @@ -117,4 +123,19 @@ void test_levelUp_notEnoughExp_loseExp (void) { 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 \ No newline at end of file