Browse Source

refactoring: changed enemyDamaged function parameter and variables in the function

remotes/origin/thirdcycle
Kai Kehres 2 years ago
parent
commit
557375b9b6
  1. 10
      src/c/encounter.c
  2. 2
      src/c/encounter.h
  3. 20
      test/c/test_encounter.c

10
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);

2
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);

20
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

Loading…
Cancel
Save