diff --git a/src/c/encounter.c b/src/c/encounter.c index 599fa35..a82e847 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -46,8 +46,6 @@ void enemyDamaged(enemy *enemy, int damage) damagedealt = 1; } setEnemyHealth(enemy, currentHealth - damagedealt); - //enemy->health -= damagedealt; - //return enemy->health; } @@ -60,16 +58,20 @@ int switchTurns(int currentTurn) int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy* enemy) { - int currentTurn = 0; + int currentTurn = 0; + char decision; while (playerAlive(playerH) && getEnemyHealth(enemy) > 0) { - if (currentTurn == 0) + if (currentTurn != 1) { - char decision = playerInput(); + decision = playerInput(); switch(decision){ case 'a': enemyDamaged(enemy, playerDamage); break; + case 'h': + playerH = playerHealth(playerH, -10, playerArmor); + break; } } diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 56a100e..7ff85ea 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -5,160 +5,164 @@ #include "playerinput.h" #include "mock_playerinput.h" - - -void setUp(void){ - +void setUp(void) +{ } -void teardown(void){ - +void teardown(void) +{ } void test_isPlayerAlive_healthGreaterZero(void) { - //arrange + // arrange int health = 100; bool result; - //act + // act result = playerAlive(health); - //assert + // assert TEST_ASSERT_TRUE(result); } void test_isPlayerAlive_healthLowerZero(void) { - //arrange + // arrange int health = -1; bool result; - //act + // act result = playerAlive(health); - //assert + // assert TEST_ASSERT_FALSE(result); } void test_playerIsDamaged(void) { - //arrange + // arrange int health = 100; int damage = 10; int armor = 0; int expectedHealth = 90; - //act + // act health = playerHealth(health, damage, armor); - //assert + // assert TEST_ASSERT_EQUAL(expectedHealth, health); } -void test_playerIsNotOverhealed(void){ - //arrange +void test_playerIsNotOverhealed(void) +{ + // arrange int health = 100; int armor = 0; int heal = -10; int expectedHealth = 100; - //act + // act health = playerHealth(health, heal, armor); - //assert + // assert TEST_ASSERT_EQUAL(expectedHealth, health); } -void test_setEnemyHealth(void){ - //arrange +void test_setEnemyHealth(void) +{ + // arrange int health = 50, result; - //act - enemy test = {health*2}; + // act + enemy test = {health * 2}; setEnemyHealth(&test, health); result = test.health; - //assert + // assert TEST_ASSERT_EQUAL(health, result); } -void test_getEnemyHealth(void){ - //arrange +void test_getEnemyHealth(void) +{ + // arrange int health = 50, result; - //act + // act enemy test = {health}; result = getEnemyHealth(&test); - //assert + // assert TEST_ASSERT_EQUAL(health, result); } -void test_setEnemyDamage(void){ - //arrange +void test_setEnemyDamage(void) +{ + // arrange int damage = 4, result; - enemy test = {50, damage*2}; - //act + enemy test = {50, damage * 2}; + // act setEnemyDamage(&test, damage); result = test.damage; - //assert + // assert TEST_ASSERT_EQUAL(damage, result); } -void test_getEnemyDamage(void){ - //arrange +void test_getEnemyDamage(void) +{ + // arrange int damage = 4, result; - //act + // act enemy test = {50, damage}; result = getEnemyDamage(&test); - //assert + // assert TEST_ASSERT_EQUAL(damage, result); } -void test_get_setEnemyArmor(void){ - //arrange +void test_get_setEnemyArmor(void) +{ + // arrange int armor = 4, result; - enemy test = {50, 4, armor*2}; - //act + enemy test = {50, 4, armor * 2}; + // act setEnemyArmor(&test, armor); result = getEnemyArmor(&test); - //assert + // assert TEST_ASSERT_EQUAL(armor, result); } void test_PlayerAttacksEnemy_DoesDamage(void) { - //arrange + // arrange int playerDamage = 10; int enemyHealth = 50; int enemyArmor = 4; - //health - (damage - armor) + // health - (damage - armor) int expectedEnemyHealth = 44; - //act + // act enemy test = {enemyHealth, 4, enemyArmor}; enemyDamaged(&test, playerDamage); - //assert + // assert TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); } void test_PlayerAttacksEnemy_DoesMinDamage(void) { - //arrange + // arrange int playerDamage = 10; int enemyHealth = 50; int enemyArmor = 10; - //health - (damage - armor) + // health - (damage - armor) int expectedEnemyHealth = 49; - //act + // act enemy test = {enemyHealth, 4, enemyArmor}; enemyDamaged(&test, playerDamage); - //assert + // assert TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); } -//A better way to get the value of a struct, REFACTORING if it works +// A better way to get the value of a struct, REFACTORING if it works void test_getterWithParams(void) { - //arrange + // arrange int health = 50, armor = 4, damage = 4, attack = 5; int healthResult, armorResult, damageResult, attackResult; enemy test = {health, damage, armor, attack}; - //act + // act healthResult = getEnemyHealth(&test); armorResult = getEnemyArmor(&test); damageResult = getEnemyDamage(&test); attackResult = getEnemyAttack(&test); - //assert + // assert TEST_ASSERT_EQUAL(health, healthResult); TEST_ASSERT_EQUAL(armor, armorResult); TEST_ASSERT_EQUAL(damage, damageResult); @@ -166,56 +170,71 @@ void test_getterWithParams(void) } void test_switchingTurns(void) { - //arrange + // arrange int currentTurn = 0, result; - //act + // act result = switchTurns(currentTurn); - //assert - TEST_ASSERT_EQUAL(1,result); + // assert + TEST_ASSERT_EQUAL(1, result); } void test_FightPlayerWins(void) { - //arange + // arange int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; int result; - //aCt + // aCt playerInput_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); - //assert + // assert TEST_ASSERT_EQUAL(1, result); } void test_FightEnemyWins(void) { - //arange + // arange int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; int result; - //act + // act playerInput_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); - //assert + // assert TEST_ASSERT_EQUAL(0, result); } -void test_FightPlayerChoosesAttack(void){ - //arrange +void test_FightPlayerChoosesAttack(void) +{ + // arrange int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; - int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; int result; - //act + // act playerInput_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); result = getEnemyHealth(&test); + // assert + TEST_ASSERT_EQUAL(0, result); +} + +void test_FightPlayerHeals_thenAttacks_Wins(void) +{ + //arrange + int playerHealth = 2, playerDamage = 10, playerArmor = 4, playerAttack = 10; + int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int result; + //act + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + playerInput_ExpectAndReturn('h'); + playerInput_ExpectAndReturn('a'); + playerInput_ExpectAndReturn('a'); + result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); //assert - TEST_ASSERT_EQUAL(94, result); - + TEST_ASSERT_EQUAL(1, result); } - #endif // TEST