From 4ac4f2b6d80352226b60b6fe2b4d5cbf1a6ff9d5 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 17:55:34 +0100 Subject: [PATCH] refactoring: prod combined addCharacterHealthPoints and addCharacterStrength to increaseStat function also --- src/c/character.c | 28 ++++++++++++++++++++-------- src/c/character.h | 10 ++++++---- test/c/test_character.c | 7 +++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index c3010be..40051d6 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -3,6 +3,7 @@ #include #include "character.h" + Character character; void setCharacterHealthPoints (Character *character, int newHealthPoints){ @@ -13,10 +14,6 @@ int getCharacterHealthPoints (Character *character) { return character->healthPoints; } -void addCharacterHealthPoints (Character *character, int addedHealthPoints) { - character->healthPoints += addedHealthPoints; -} - void setCharacterStrength (Character *character, int newStrength) { character->strength = newStrength; } @@ -25,10 +22,6 @@ int getCharacterStrength (Character *character) { return character->strength; } -void addCharacterStrength (Character *character, int addStrength) { - character->strength += addStrength; -} - void setCharacterDexterity (Character *character, int newDexterity) { character->dexterity = newDexterity; } @@ -43,4 +36,23 @@ void setCharacterIntelligence (Character *character, int newIntelligence) { int getCharacterIntelligence (Character *character) { return character->intelligence; +} + + +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; + default: + break; + } } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index 5caca85..02b4630 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -6,18 +6,18 @@ typedef struct { char name [50]; } Character; +enum { + STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5 +}; + void setCharacterHealthPoints (Character *character, int newHealthPoints); int getCharacterHealthPoints (Character *character); -void addCharacterHealthPoints (Character *character, int addedHealthPoints); - void setCharacterStrength (Character *character, int newStrength); int getCharacterStrength (Character *character); -void addCharacterStrength (Character *character, int addStrength); - void setCharacterDexterity (Character *character, int newDexterity); int getCharacterDexterity (Character *character); @@ -26,4 +26,6 @@ void setCharacterIntelligence (Character *character, int newIntelligence); int getCharacterIntelligence (Character *character); +void increaseStat (Character *character, int Stat, int amount); + #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index d948c3c..083b842 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -32,9 +32,8 @@ void test_getCharacterHealthPoints (void) { } void test_addCharacterHealthPoints (void) { - addCharacterHealthPoints(&testCharacter2,5); + increaseStat(&testCharacter2,HEALTHPOINTS,5); TEST_ASSERT_EQUAL_INT(20,testCharacter2.healthPoints); - } void test_setCharacterStrenght(void) { @@ -48,8 +47,8 @@ void test_getCharacterStrength(void) { } void test_addCharacterStrength (void) { - addCharacterStrength(&testCharacter2,15); - TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); + increaseStat(&testCharacter2,STRENGTH,5); + TEST_ASSERT_EQUAL_INT(10,testCharacter2.strength); } void test_setCharacterDexterity(void) {