From eb7b7c07f67f726c975af0ec5b401259dda4dc72 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 12:18:55 +0100 Subject: [PATCH 01/12] prod/test: created function rollInitiative and Unittest --- src/c/encounter.c | 4 +++- src/c/encounter.h | 1 + test/c/test_encounter.c | 7 +++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 80559ba..13859b5 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -130,7 +130,9 @@ int fight(Character *character, enemy* enemy) } } - +int rollInitiative (Character *character) { + return randomIntRange(1,20) + character->dexterity; +} bool enemyChoosesHeal(enemy* enemy) { diff --git a/src/c/encounter.h b/src/c/encounter.h index 4a2c1ff..d181d37 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -17,6 +17,7 @@ typedef struct enemy{ bool playerAlive(int health); int playerHeal(int health, int damage, Character* character); int playerDamaged(int health, int damage, int armor, Character* character); +int rollInitiative (Character *character); void enemyHeal(enemy *enemy, int healAmount); void enemyDamaged(enemy* enemy, int damage); bool enemyChoosesHeal(enemy* enemy); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 023d470..f92bf32 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -9,9 +9,10 @@ #include "mock_helper.h" #include "utils.h" - +Character testcharacter; void setUp(void) { + testcharacter.dexterity = 5; } void teardown(void) @@ -395,5 +396,7 @@ void test_enemyHealsNoPotion(void) TEST_ASSERT_FALSE(result); } - +int test_rollInitiative (void) { + TEST_ASSERT(testcharacter.dexterity + 1 <= rollInitiative(&testcharacter) && testcharacter.dexterity + 20 >= rollInitiative(&testcharacter)); +} #endif // TEST From 197e696ef63e568bf5162bfcbacc8fe72302c86d Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 12:22:02 +0100 Subject: [PATCH 02/12] prod/test: created getter and setter for charactername --- src/c/character.c | 7 +++++++ src/c/character.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index 2d1d317..c994b8d 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -98,6 +98,13 @@ int getCharacterGold (Character *character){ return character->gold; } +void setCharacterName (Character *character, char *newName) { + strcpy(character->name,newName); +} + +char getCharacterName (Character *character) { + return character->name; +} void increaseStat (Character *character, int stat, int amount) { switch (stat) diff --git a/src/c/character.h b/src/c/character.h index 808c8d0..ee7973c 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -59,4 +59,9 @@ void setCharacterGold (Character *character, int newGold); int getCharacterGold (Character *character); void levelUp (Character *character); + +void setCharacterName (Character *character, char newName[]); + +char getCharacterName (Character *character); + #endif \ No newline at end of file From b493f971460c62dba5a3e0cbb085b366e09327d8 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 12:34:14 +0100 Subject: [PATCH 03/12] prod: changed returntype of getcharactername --- src/c/character.c | 2 +- src/c/character.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index c994b8d..02254ec 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -102,7 +102,7 @@ void setCharacterName (Character *character, char *newName) { strcpy(character->name,newName); } -char getCharacterName (Character *character) { +char* getCharacterName (Character *character) { return character->name; } diff --git a/src/c/character.h b/src/c/character.h index ee7973c..2afa7d4 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -62,6 +62,6 @@ void levelUp (Character *character); void setCharacterName (Character *character, char newName[]); -char getCharacterName (Character *character); +char* getCharacterName (Character *character); #endif \ No newline at end of file From 6b0a6f6f64101e9b039b527007faee3619ef7959 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 12:34:59 +0100 Subject: [PATCH 04/12] refactoring: changed player Alive function parameters to struct character --- src/c/encounter.c | 8 ++++---- src/c/encounter.h | 2 +- test/c/test_encounter.c | 7 ++++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 13859b5..b3440a0 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -17,9 +17,9 @@ humanoide gegner haben heiltränke mit denen sie sich ggf heilen. heilung erfolgt dann, wenn bestimmte hp (50%) unterschritten wird. wird allerdings gewürfelt, je niedriger die hp%, desto höher die chance. */ -bool playerAlive(int health) +bool playerAlive(Character *character) { - if (health > 0) + if (getCharacterHealthPoints(character) > 0) { return true; } @@ -86,7 +86,7 @@ int fight(Character *character, enemy* enemy) int playerH = 0; int currentTurn = 2; char decision; - while (playerAlive(getCharacterHealthPoints(character)) && getEnemyHealth(enemy) > 0) + while (playerAlive(character) && getEnemyHealth(enemy) > 0) { if (currentTurn != 1) { @@ -118,7 +118,7 @@ int fight(Character *character, enemy* enemy) } currentTurn = switchTurns(currentTurn); } - if (playerAlive(getCharacterHealthPoints(character))) + if (playerAlive(character)) { setCharacterExp(character, getCharacterExp(character) + getEnemyExp(enemy)); setCharacterGold(character, getCharacterGold(character) + getEnemyGold(enemy)); diff --git a/src/c/encounter.h b/src/c/encounter.h index d181d37..68d0d71 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -14,7 +14,7 @@ typedef struct enemy{ } enemy; -bool playerAlive(int health); +bool playerAlive(Character *character); int playerHeal(int health, int damage, Character* character); int playerDamaged(int health, int damage, int armor, Character* character); int rollInitiative (Character *character); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index f92bf32..49f25e8 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -13,6 +13,7 @@ Character testcharacter; void setUp(void) { testcharacter.dexterity = 5; + testcharacter.healthPoints = 100; } void teardown(void) @@ -25,7 +26,7 @@ void test_isPlayerAlive_healthGreaterZero(void) int health = 100; bool result; // act - result = playerAlive(health); + result = playerAlive(&testcharacter); // assert TEST_ASSERT_TRUE(result); } @@ -33,10 +34,10 @@ void test_isPlayerAlive_healthGreaterZero(void) void test_isPlayerAlive_healthLowerZero(void) { // arrange - int health = -1; + setCharacterHealthPoints(&testcharacter, -1); bool result; // act - result = playerAlive(health); + result = playerAlive(&testcharacter); // assert TEST_ASSERT_FALSE(result); From 1e1d7697a33bd4b2d9bd2742df95d3a1dd179a19 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 12:49:31 +0100 Subject: [PATCH 05/12] refactoring: changed playerHealfunction paramter and Unittest setup to resemble the changes --- src/c/character.h | 3 ++- src/c/encounter.c | 13 ++++++------- src/c/encounter.h | 2 +- test/c/test_encounter.c | 9 +++------ 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/c/character.h b/src/c/character.h index 2afa7d4..d50b076 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -2,7 +2,8 @@ #define CHARACTER_H typedef struct { - int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp,attack,armor,maxHealthPoints,gold; + int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; + int attack,armor,maxHealthPoints,gold; char name [50]; } Character; diff --git a/src/c/encounter.c b/src/c/encounter.c index b3440a0..8c820f7 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -29,15 +29,14 @@ bool playerAlive(Character *character) } } -int playerHeal(int health, int damage, Character* character) +int playerHeal(Character* character, int amount) { - int maxhealth = getCharacterMaxHealthPoints(character); - health = health + damage; - if (health > maxhealth) + setCharacterHealthPoints(character, getCharacterHealthPoints(character)+ amount); + if (getCharacterHealthPoints(character) > getCharacterMaxHealthPoints(character)) { - health = maxhealth; + setCharacterHealthPoints(character, getCharacterMaxHealthPoints(character)); } - return health; + return getCharacterHealthPoints(character); } int playerDamaged(int health, int damage, int armor, Character* character) @@ -96,7 +95,7 @@ int fight(Character *character, enemy* enemy) enemyDamaged(enemy, getCharacterAttack(character)); break; case 'h': - playerH = playerHeal(getCharacterHealthPoints(character), 10, character); + playerH = playerHeal(character,10); setCharacterHealthPoints(character, playerH); break; case 'f': diff --git a/src/c/encounter.h b/src/c/encounter.h index 68d0d71..4ae48ae 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -15,7 +15,7 @@ typedef struct enemy{ bool playerAlive(Character *character); -int playerHeal(int health, int damage, Character* character); +int playerHeal(Character* character,int amount); int playerDamaged(int health, int damage, int armor, Character* character); int rollInitiative (Character *character); void enemyHeal(enemy *enemy, int healAmount); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 49f25e8..3c33f55 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -14,6 +14,7 @@ void setUp(void) { testcharacter.dexterity = 5; testcharacter.healthPoints = 100; + testcharacter.maxHealthPoints = 100; } void teardown(void) @@ -60,15 +61,11 @@ void test_playerIsDamaged(void) void test_playerIsNotOverhealed(void) { // arrange - int health = 95; - int armor = 0; + setCharacterHealthPoints(&testcharacter, 95); int heal = 10; - int expectedHealth = 100; // act - Character testChar = {10,10,10,health,100,1,0,100,10,armor,100}; - health = playerHeal(health, heal, &testChar); // assert - TEST_ASSERT_EQUAL(expectedHealth, health); + TEST_ASSERT_EQUAL(getCharacterMaxHealthPoints(&testcharacter), playerHeal(&testcharacter,heal)); } void test_setEnemyHealth(void) From 7780e81c619548f611e39267a83a4c5fa9725282 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 12:58:26 +0100 Subject: [PATCH 06/12] refactoring: changed playerDamaged Parameters and Unittest setup to resemble the changes --- src/c/encounter.c | 8 ++++---- src/c/encounter.h | 2 +- test/c/test_encounter.c | 10 ++++------ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 8c820f7..a1d13b1 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -39,14 +39,14 @@ int playerHeal(Character* character, int amount) return getCharacterHealthPoints(character); } -int playerDamaged(int health, int damage, int armor, Character* character) +int playerDamaged(enemy* enemy, Character* character) { - int damagedealt = damage - armor; + int damagedealt = getEnemyDamage(enemy) - getCharacterArmor(character); if (damagedealt < 1) { damagedealt = 1; } - return health - damagedealt; + return getCharacterHealthPoints(character) - damagedealt; } void enemyHeal(enemy *enemy, int healAmount) @@ -111,7 +111,7 @@ int fight(Character *character, enemy* enemy) } else { - playerH = playerDamaged(getCharacterHealthPoints(character), getEnemyDamage(enemy), getCharacterArmor(character), character); + playerH = playerDamaged(enemy, character); setCharacterHealthPoints(character, playerH); } } diff --git a/src/c/encounter.h b/src/c/encounter.h index 4ae48ae..413ed6e 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -16,7 +16,7 @@ typedef struct enemy{ bool playerAlive(Character *character); int playerHeal(Character* character,int amount); -int playerDamaged(int health, int damage, int armor, Character* character); +int playerDamaged(enemy* enemy, Character* character); int rollInitiative (Character *character); void enemyHeal(enemy *enemy, int healAmount); void enemyDamaged(enemy* enemy, int damage); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 3c33f55..9e171a0 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -10,11 +10,14 @@ #include "utils.h" Character testcharacter; +enemy testenemy; void setUp(void) { testcharacter.dexterity = 5; testcharacter.healthPoints = 100; testcharacter.maxHealthPoints = 100; + + testenemy.damage = 10; } void teardown(void) @@ -47,15 +50,10 @@ void test_isPlayerAlive_healthLowerZero(void) void test_playerIsDamaged(void) { // arrange - int health = 100; - int damage = 10; - int armor = 0; int expectedHealth = 90; // act - Character testChar = {10,10,10,health,100,1,0,100,damage,armor,100}; - health = playerDamaged(health, damage, armor, &testChar); // assert - TEST_ASSERT_EQUAL(expectedHealth, health); + TEST_ASSERT_EQUAL(expectedHealth, playerDamaged(&testenemy, &testcharacter)); } void test_playerIsNotOverhealed(void) From c00b79a1f12956beb8288b672f37a24d78473902 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 13:03:42 +0100 Subject: [PATCH 07/12] refactoring: changed enemyHeal function --- src/c/encounter.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index a1d13b1..620b698 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -51,13 +51,11 @@ int playerDamaged(enemy* enemy, Character* character) void enemyHeal(enemy *enemy, int healAmount) { - int currentHealth = getEnemyHealth(enemy); - int maxHealth = getEnemyMaxHealth(enemy); - if (currentHealth + healAmount > maxHealth) + if (getEnemyHealth(enemy) + healAmount > getEnemyMaxHealth(enemy)) { - healAmount = maxHealth - currentHealth; + healAmount = getEnemyMaxHealth(enemy) - getEnemyHealth(enemy); } - setEnemyHealth(enemy, currentHealth + healAmount); + setEnemyHealth(enemy, getEnemyHealth(enemy) + healAmount); } void enemyDamaged(enemy *enemy, int damage) From 557375b9b690542272a68dd340b2644fc3c9a648 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 13:26:37 +0100 Subject: [PATCH 08/12] refactoring: changed enemyDamaged function parameter and variables in the function --- src/c/encounter.c | 10 ++++------ src/c/encounter.h | 2 +- test/c/test_encounter.c | 20 ++++++++------------ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 620b698..aa8287d 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -58,16 +58,14 @@ void enemyHeal(enemy *enemy, int healAmount) setEnemyHealth(enemy, getEnemyHealth(enemy) + healAmount); } -void enemyDamaged(enemy *enemy, int damage) +void enemyDamaged(enemy *enemy,Character* character) { - int armor = getEnemyArmor(enemy); - int currentHealth = getEnemyHealth(enemy); - int damagedealt = damage - armor; + int damagedealt = getCharacterAttack(character) - getEnemyArmor(enemy); if (damagedealt < 1) { damagedealt = 1; } - setEnemyHealth(enemy, currentHealth - damagedealt); + setEnemyHealth(enemy, getEnemyHealth(enemy) - damagedealt); } @@ -90,7 +88,7 @@ int fight(Character *character, enemy* enemy) decision = playerInputChar(); switch(decision){ case 'a': - enemyDamaged(enemy, getCharacterAttack(character)); + enemyDamaged(enemy, character); break; case 'h': playerH = playerHeal(character,10); diff --git a/src/c/encounter.h b/src/c/encounter.h index 413ed6e..071861f 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -19,7 +19,7 @@ int playerHeal(Character* character,int amount); int playerDamaged(enemy* enemy, Character* character); int rollInitiative (Character *character); void enemyHeal(enemy *enemy, int healAmount); -void enemyDamaged(enemy* enemy, int damage); +void enemyDamaged(enemy* enemy, Character* character); bool enemyChoosesHeal(enemy* enemy); int switchTurns(int currentTurn); int fight(Character *character, enemy* enemy); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 9e171a0..bb49353 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -16,8 +16,11 @@ void setUp(void) testcharacter.dexterity = 5; testcharacter.healthPoints = 100; testcharacter.maxHealthPoints = 100; + testcharacter.attack = 10; testenemy.damage = 10; + testenemy.health = 50; + testenemy.armor = 10; } void teardown(void) @@ -127,31 +130,24 @@ void test_get_setEnemyArmor(void) void test_PlayerAttacksEnemy_DoesDamage(void) { // arrange - int playerDamage = 10; - int enemyHealth = 50; - int enemyArmor = 4; + setEnemyArmor(&testenemy, 4); // health - (damage - armor) int expectedEnemyHealth = 44; // act - enemy test = {enemyHealth, 4, enemyArmor}; - enemyDamaged(&test, playerDamage); + enemyDamaged(&testenemy, &testcharacter); // assert - TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); + TEST_ASSERT_EQUAL(expectedEnemyHealth, getEnemyHealth(&testenemy)); } void test_PlayerAttacksEnemy_DoesMinDamage(void) { // arrange - int playerDamage = 10; - int enemyHealth = 50; - int enemyArmor = 10; // health - (damage - armor) int expectedEnemyHealth = 49; // act - enemy test = {enemyHealth, 4, enemyArmor}; - enemyDamaged(&test, playerDamage); + enemyDamaged(&testenemy,&testcharacter); // assert - TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); + TEST_ASSERT_EQUAL(expectedEnemyHealth, getEnemyHealth(&testenemy)); } // A better way to get the value of a struct, REFACTORING if it works From b5236dad9c523c34d1b2a022e3e6aed0b9536c6a Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 15:57:12 +0100 Subject: [PATCH 09/12] prod: created struct weapon and getter setter for attack and Durability --- src/c/character.c | 16 ++++++++++++++++ src/c/character.h | 14 +++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/c/character.c b/src/c/character.c index 02254ec..f5f96ec 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -106,6 +106,22 @@ char* getCharacterName (Character *character) { return character->name; } +int getWaponAttack (Weapon *weapon) { + return weapon->attack; +} + +void setWeaponAttack (Weapon *weapon, int newAttack) { + weapon->attack = newAttack; +} + +int getWeaponDurability (Weapon *weapon) { + return weapon->durability; +} + +int setWeaponDurability (Weapon *weapon, int newDurability) { + weapon->durability = newDurability; +} + void increaseStat (Character *character, int stat, int amount) { switch (stat) { diff --git a/src/c/character.h b/src/c/character.h index d50b076..3f74401 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -3,9 +3,13 @@ typedef struct { int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; - int attack,armor,maxHealthPoints,gold; + int attack,armor,maxHealthPoints,gold,items[10]; char name [50]; } Character; +typedef struct { + int attack,durability; + char name [50]; +} Weapon; enum { STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6, EXP = 7, MAXEXP = 8 @@ -65,4 +69,12 @@ void setCharacterName (Character *character, char newName[]); char* getCharacterName (Character *character); +int getWaponAttack (Weapon *weapon); + +void setWeaponAttack (Weapon *weapon, int newAttack); + +int getWeaponDurability (Weapon *weapon); + +int setWeaponDurability (Weapon *weapon, int newDurability); + #endif \ No newline at end of file From 9c611496b99b76869257948237e392452fa90999 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 16:07:01 +0100 Subject: [PATCH 10/12] prod: getter and setter for weaponClass --- src/c/character.c | 10 +++++++++- src/c/character.h | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index f5f96ec..86627f8 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -106,7 +106,7 @@ char* getCharacterName (Character *character) { return character->name; } -int getWaponAttack (Weapon *weapon) { +int getWeaponAttack (Weapon *weapon) { return weapon->attack; } @@ -122,6 +122,14 @@ int setWeaponDurability (Weapon *weapon, int newDurability) { weapon->durability = newDurability; } +void setWeaponClass (Character* character, int weaponClass) { + character->weapon = weaponClass; +} + +int getWeaponClass (Character* character) { + return character->weapon; +} + void increaseStat (Character *character, int stat, int amount) { switch (stat) { diff --git a/src/c/character.h b/src/c/character.h index 3f74401..0c083a1 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]; + int attack,armor,maxHealthPoints,gold,items[10],weapon; char name [50]; } Character; typedef struct { @@ -11,6 +11,9 @@ typedef struct { char name [50]; } Weapon; +enum { + SWORD = 1, BOW = 2, STAFF = 3 +}; enum { STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6, EXP = 7, MAXEXP = 8 }; @@ -69,7 +72,7 @@ void setCharacterName (Character *character, char newName[]); char* getCharacterName (Character *character); -int getWaponAttack (Weapon *weapon); +int getWeaponAttack (Weapon *weapon); void setWeaponAttack (Weapon *weapon, int newAttack); From 0e26ef798c145ae4f8e9dbebe089e0d70f3a2d61 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 16:28:26 +0100 Subject: [PATCH 11/12] 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 From 143104533b0e9c860bd5dcbafce9fe5725d0e077 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Fri, 27 Jan 2023 16:38:04 +0100 Subject: [PATCH 12/12] refactoring: extracted function calls out of the assert --- test/c/test_encounter.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index bb49353..d74591e 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -55,8 +55,9 @@ void test_playerIsDamaged(void) // arrange int expectedHealth = 90; // act + int result = playerDamaged(&testenemy, &testcharacter); // assert - TEST_ASSERT_EQUAL(expectedHealth, playerDamaged(&testenemy, &testcharacter)); + TEST_ASSERT_EQUAL(expectedHealth, result); } void test_playerIsNotOverhealed(void) @@ -65,8 +66,9 @@ void test_playerIsNotOverhealed(void) setCharacterHealthPoints(&testcharacter, 95); int heal = 10; // act + int result = playerHeal(&testcharacter, heal); // assert - TEST_ASSERT_EQUAL(getCharacterMaxHealthPoints(&testcharacter), playerHeal(&testcharacter,heal)); + TEST_ASSERT_EQUAL(getCharacterMaxHealthPoints(&testcharacter), result); } void test_setEnemyHealth(void) @@ -389,6 +391,9 @@ void test_enemyHealsNoPotion(void) } int test_rollInitiative (void) { - TEST_ASSERT(testcharacter.dexterity + 1 <= rollInitiative(&testcharacter) && testcharacter.dexterity + 20 >= rollInitiative(&testcharacter)); + + int initiative = rollInitiative(&testcharacter); + + TEST_ASSERT(testcharacter.dexterity + 1 <= initiative && testcharacter.dexterity + 20 >= initiative); } #endif // TEST