Browse Source

Changed the setters to avoid Segmentation faults

remotes/origin/encounter
János 2 years ago
parent
commit
cccc31e36a
  1. 64
      src/c/encounter.c
  2. 5
      src/c/encounter.h
  3. 14
      test/c/test_encounter.c

64
src/c/encounter.c

@ -37,7 +37,7 @@ int playerHealth(int health, int damage, int armor)
int enemyDamaged(enemy enemy, int damage) int enemyDamaged(enemy enemy, int damage)
{ {
int armor = getEnemyInt(&enemy.armor);
int armor = getEnemyArmor(&enemy);
int damagedealt = damage - armor; int damagedealt = damage - armor;
if (damagedealt < 1) if (damagedealt < 1)
{ {
@ -47,27 +47,7 @@ int enemyDamaged(enemy enemy, int damage)
return enemy.health; 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) 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 fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy enemy)
{ {
int currentTurn = 0; int currentTurn = 0;
while (playerAlive(playerH) && getEnemyInt(&enemy.health) > 0)
while (playerAlive(playerH) && getEnemyHealth(&enemy) > 0)
{ {
if (currentTurn == 0) if (currentTurn == 0)
{ {
@ -86,7 +66,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem
} }
else else
{ {
playerH = playerHealth(playerH, getEnemyInt(&enemy.damage), playerArmor);
playerH = playerHealth(playerH, getEnemyDamage(&enemy), playerArmor);
} }
currentTurn = switchTurns(currentTurn); currentTurn = switchTurns(currentTurn);
} }
@ -99,3 +79,41 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem
return 0; 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;
}

5
src/c/encounter.h

@ -28,6 +28,9 @@ void *setEnemyDamage(int *num, int damage);
//setEnemyArmor(&enemy.armor, armor); //setEnemyArmor(&enemy.armor, armor);
void *setEnemyArmor(int *num, int damage); void *setEnemyArmor(int *num, int damage);
//Function to get the value of Data in a struct, needs a pointer to the struct //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 #endif

14
test/c/test_encounter.c

@ -77,7 +77,7 @@ void test_getEnemyHealth(void){
int health = 50, result; int health = 50, result;
//act //act
enemy test = {health}; enemy test = {health};
result = getEnemyInt(&test.health);
result = getEnemyHealth(&test);
//assert //assert
TEST_ASSERT_EQUAL(health, result); TEST_ASSERT_EQUAL(health, result);
} }
@ -98,7 +98,7 @@ void test_getEnemyDamage(void){
int damage = 4, result; int damage = 4, result;
//act //act
enemy test = {50, damage}; enemy test = {50, damage};
result = getEnemyInt(&test.damage);
result = getEnemyDamage(&test);
//assert //assert
TEST_ASSERT_EQUAL(damage, result); TEST_ASSERT_EQUAL(damage, result);
} }
@ -109,7 +109,7 @@ void test_get_setEnemyArmor(void){
enemy test = {50, 4, armor*2}; enemy test = {50, 4, armor*2};
//act //act
setEnemyArmor(&test.armor, armor); setEnemyArmor(&test.armor, armor);
result = getEnemyInt(&test.armor);
result = getEnemyArmor(&test);
//assert //assert
TEST_ASSERT_EQUAL(armor, result); TEST_ASSERT_EQUAL(armor, result);
} }
@ -152,10 +152,10 @@ void test_getterWithParams(void)
int healthResult, armorResult, damageResult, attackResult; int healthResult, armorResult, damageResult, attackResult;
enemy test = {health, damage, armor, attack}; enemy test = {health, damage, armor, attack};
//act //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 //assert
TEST_ASSERT_EQUAL(health, healthResult); TEST_ASSERT_EQUAL(health, healthResult);
TEST_ASSERT_EQUAL(armor, armorResult); TEST_ASSERT_EQUAL(armor, armorResult);

Loading…
Cancel
Save