Browse Source

Added Healing to the fight function

remotes/origin/encounter
János 2 years ago
parent
commit
198e7b9b29
  1. 11
      src/c/encounter.c
  2. 29
      test/c/test_encounter.c

11
src/c/encounter.c

@ -70,7 +70,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 = 2;
char decision; char decision;
while (playerAlive(playerH) && getEnemyHealth(enemy) > 0) while (playerAlive(playerH) && getEnemyHealth(enemy) > 0)
{ {
@ -91,7 +91,14 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem
} }
else else
{ {
playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor);
if(enemyChoosesHeal(enemy))
{
enemyHeal(enemy, 10);
}
else
{
playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor);
}
} }
currentTurn = switchTurns(currentTurn); currentTurn = switchTurns(currentTurn);
} }

29
test/c/test_encounter.c

@ -198,10 +198,11 @@ void test_FightEnemyWins(void)
{ {
// arange // arange
int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5;
int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5;
int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100;
int result; int result;
// act // act
playerInput_ExpectAndReturn('a'); playerInput_ExpectAndReturn('a');
randomInt_ExpectAndReturn(1);
enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth};
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test);
// assert // assert
@ -212,7 +213,7 @@ void test_FightPlayerChoosesAttack(void)
{ {
// arrange // arrange
int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5;
int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5;
int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100;
int result; int result;
// act // act
playerInput_ExpectAndReturn('a'); playerInput_ExpectAndReturn('a');
@ -227,12 +228,14 @@ void test_FightPlayerHeals_thenAttacks_Wins(void)
{ {
// arrange // arrange
int playerHealth = 2, playerDamage = 10, playerArmor = 4, playerAttack = 10; int playerHealth = 2, playerDamage = 10, playerArmor = 4, playerAttack = 10;
int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5;
int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100;
int result; int result;
// act // act
enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth};
playerInput_ExpectAndReturn('h'); playerInput_ExpectAndReturn('h');
randomInt_ExpectAndReturn(1);
playerInput_ExpectAndReturn('a'); playerInput_ExpectAndReturn('a');
randomInt_ExpectAndReturn(1);
playerInput_ExpectAndReturn('a'); playerInput_ExpectAndReturn('a');
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test);
// assert // assert
@ -243,7 +246,7 @@ void test_FightPlayerFlees(void)
{ {
// arrange // arrange
int playerHealth = 10, playerDamage = 10, playerArmor = 4, playerAttack = 10; int playerHealth = 10, playerDamage = 10, playerArmor = 4, playerAttack = 10;
int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5;
int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100;
int result; int result;
// act // act
enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth};
@ -351,4 +354,22 @@ void test_enemyDoesNotChoosesHeal(void)
} }
void test_enemyChoosesHeal_ThenAttackWins(void)
{
//arange
int result;
int enemyHealth = 6, enemyDamage = 10, enemyArmor = 5, enemyMaxHealth = 100;
int playerHealth = 10, playerDamage = 10, playerArmor = 5, playerAttack = 10;
enemy test2 = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth};
//act
playerInput_ExpectAndReturn('a');
randomInt_ExpectAndReturn(39); //39%20 = 19 , 19 + 1 = 20
playerInput_ExpectAndReturn('a');
randomInt_ExpectAndReturn(0); //0%20 = 0 , 0 + 1 = 1
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test2);
//assert
TEST_ASSERT_EQUAL(0, result);
}
#endif // TEST #endif // TEST
Loading…
Cancel
Save