From 286477ea599f9aa1be993c8ba68b1969a979257d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 19 Jan 2023 12:37:18 +0100 Subject: [PATCH] refactoring: changed getters to avoid seg.fault --- src/c/encounter.c | 64 ++++++++++++++++++++++++++--------------- src/c/encounter.h | 5 +++- test/c/test_encounter.c | 14 ++++----- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 2bf1159..c5c8984 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -37,7 +37,7 @@ int playerHealth(int health, int damage, int armor) int enemyDamaged(enemy enemy, int damage) { - int armor = getEnemyInt(&enemy.armor); + int armor = getEnemyArmor(&enemy); int damagedealt = damage - armor; if (damagedealt < 1) { @@ -47,27 +47,7 @@ int enemyDamaged(enemy enemy, int damage) return enemy.health; } -// Getter/Setter Funktionen -void *setEnemyHealth(int *num, int health) -{ - *num = health; - // return health; -} - -void *setEnemyDamage(int *num, int damage) -{ - *num = damage; -} - -void *setEnemyArmor(int *num, int armor) -{ - *num = armor; -} -int getEnemyInt(int *structParam) -{ - return *structParam; -} int switchTurns(int currentTurn) { @@ -78,7 +58,7 @@ int switchTurns(int currentTurn) int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy enemy) { int currentTurn = 0; - while (playerAlive(playerH) && getEnemyInt(&enemy.health) > 0) + while (playerAlive(playerH) && getEnemyHealth(&enemy) > 0) { if (currentTurn == 0) { @@ -86,7 +66,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } else { - playerH = playerHealth(playerH, getEnemyInt(&enemy.damage), playerArmor); + playerH = playerHealth(playerH, getEnemyDamage(&enemy), playerArmor); } currentTurn = switchTurns(currentTurn); } @@ -99,3 +79,41 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem return 0; } } + + +// Getter/Setter Funktionen +void *setEnemyHealth(int *num, int health) +{ + *num = health; + // return health; +} + +void *setEnemyDamage(int *num, int damage) +{ + *num = damage; +} + +void *setEnemyArmor(int *num, int armor) +{ + *num = armor; +} + +int getEnemyHealth(enemy* enemy) +{ + return enemy->health; +} + +int getEnemyArmor(enemy* enemy) +{ + return enemy->armor; +} + +int getEnemyDamage(enemy* enemy) +{ + return enemy->damage; +} + +int getEnemyAttack(enemy* enemy) +{ + return enemy->attack; +} \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index 22bb1d9..0a3031b 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -28,6 +28,9 @@ void *setEnemyDamage(int *num, int damage); //setEnemyArmor(&enemy.armor, armor); void *setEnemyArmor(int *num, int damage); //Function to get the value of Data in a struct, needs a pointer to the struct -int getEnemyInt(int *structParam); +int getEnemyAttack(enemy* enemy); +int getEnemyHealth(enemy* enemy); +int getEnemyArmor(enemy* enemy); +int getEnemyDamage(enemy* enemy); #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 72e8d4b..df41cdc 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -77,7 +77,7 @@ void test_getEnemyHealth(void){ int health = 50, result; //act enemy test = {health}; - result = getEnemyInt(&test.health); + result = getEnemyHealth(&test); //assert TEST_ASSERT_EQUAL(health, result); } @@ -98,7 +98,7 @@ void test_getEnemyDamage(void){ int damage = 4, result; //act enemy test = {50, damage}; - result = getEnemyInt(&test.damage); + result = getEnemyDamage(&test); //assert TEST_ASSERT_EQUAL(damage, result); } @@ -109,7 +109,7 @@ void test_get_setEnemyArmor(void){ enemy test = {50, 4, armor*2}; //act setEnemyArmor(&test.armor, armor); - result = getEnemyInt(&test.armor); + result = getEnemyArmor(&test); //assert TEST_ASSERT_EQUAL(armor, result); } @@ -152,10 +152,10 @@ void test_getterWithParams(void) int healthResult, armorResult, damageResult, attackResult; enemy test = {health, damage, armor, attack}; //act - healthResult = getEnemyInt(&test.health); - armorResult = getEnemyInt(&test.armor); - damageResult = getEnemyInt(&test.damage); - attackResult = getEnemyInt(&test.attack); + healthResult = getEnemyHealth(&test); + armorResult = getEnemyArmor(&test); + damageResult = getEnemyDamage(&test); + attackResult = getEnemyAttack(&test); //assert TEST_ASSERT_EQUAL(health, healthResult); TEST_ASSERT_EQUAL(armor, armorResult);