From 557375b9b690542272a68dd340b2644fc3c9a648 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 26 Jan 2023 13:26:37 +0100 Subject: [PATCH] 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