Browse Source

refactoring: changed playerHealfunction paramter and Unittest setup to resemble the changes

remotes/origin/thirdcycle
Kai Kehres 2 years ago
parent
commit
1e1d7697a3
  1. 3
      src/c/character.h
  2. 13
      src/c/encounter.c
  3. 2
      src/c/encounter.h
  4. 9
      test/c/test_encounter.c

3
src/c/character.h

@ -2,7 +2,8 @@
#define CHARACTER_H #define CHARACTER_H
typedef struct { 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]; char name [50];
} Character; } Character;

13
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) int playerDamaged(int health, int damage, int armor, Character* character)
@ -96,7 +95,7 @@ int fight(Character *character, enemy* enemy)
enemyDamaged(enemy, getCharacterAttack(character)); enemyDamaged(enemy, getCharacterAttack(character));
break; break;
case 'h': case 'h':
playerH = playerHeal(getCharacterHealthPoints(character), 10, character);
playerH = playerHeal(character,10);
setCharacterHealthPoints(character, playerH); setCharacterHealthPoints(character, playerH);
break; break;
case 'f': case 'f':

2
src/c/encounter.h

@ -15,7 +15,7 @@ typedef struct enemy{
bool playerAlive(Character *character); 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 playerDamaged(int health, int damage, int armor, Character* character);
int rollInitiative (Character *character); int rollInitiative (Character *character);
void enemyHeal(enemy *enemy, int healAmount); void enemyHeal(enemy *enemy, int healAmount);

9
test/c/test_encounter.c

@ -14,6 +14,7 @@ void setUp(void)
{ {
testcharacter.dexterity = 5; testcharacter.dexterity = 5;
testcharacter.healthPoints = 100; testcharacter.healthPoints = 100;
testcharacter.maxHealthPoints = 100;
} }
void teardown(void) void teardown(void)
@ -60,15 +61,11 @@ void test_playerIsDamaged(void)
void test_playerIsNotOverhealed(void) void test_playerIsNotOverhealed(void)
{ {
// arrange // arrange
int health = 95;
int armor = 0;
setCharacterHealthPoints(&testcharacter, 95);
int heal = 10; int heal = 10;
int expectedHealth = 100;
// act // act
Character testChar = {10,10,10,health,100,1,0,100,10,armor,100};
health = playerHeal(health, heal, &testChar);
// assert // assert
TEST_ASSERT_EQUAL(expectedHealth, health);
TEST_ASSERT_EQUAL(getCharacterMaxHealthPoints(&testcharacter), playerHeal(&testcharacter,heal));
} }
void test_setEnemyHealth(void) void test_setEnemyHealth(void)

Loading…
Cancel
Save