From 5cf386590b304a6b77fed9edec6ad1e61fbd3ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 19:22:31 +0100 Subject: [PATCH] Added a Function to apply Damage to the enemy --- src/c/encounter.c | 6 ++++++ src/c/encounter.h | 2 ++ test/c/test_encounter.c | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index bc6887f..955736e 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -62,3 +62,9 @@ int getEnemyArmor(enemy enemy){ return enemy.armor; } +int enemyDamaged(enemy enemy, int damage){ + int armor = getEnemyArmor(enemy); + int damagedealt = damage - armor; + enemy.health = enemy.health - damagedealt; + return enemy.health; +} \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index 182aad3..d2b0fba 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -23,5 +23,7 @@ int getEnemyDamage(enemy enemy); void *setEnemyArmor(int *num, int damage); int getEnemyArmor(enemy enemy); +int enemyDamaged(enemy enemy, int damage); + #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 2954ffa..8bef335 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -84,4 +84,21 @@ void test_get_setEnemyArmor(void){ result = getEnemyArmor(test); TEST_ASSERT_EQUAL(armor, result); } + +void test_PlayerAttacksEnemy_DoesDamage(void) +{ + //arrange + int playerDamage = 10; + int enemyHealth = 50; + int enemyArmor = 4; + //health - (damage - armor) + int expectedEnemyHealth = 44; + //act + enemy test = {enemyHealth, 4, enemyArmor}; + test.health = enemyDamaged(test, playerDamage); + //assert + TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); +} + + #endif // TEST