diff --git a/src/c/character.c b/src/c/character.c index 9c436ff..2d1d317 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -66,6 +66,39 @@ int calculateStatIncrease (Character *character, int amount) { return character->level*amount; } +void setCharacterMaxHealthPoints (Character *character, int newMaxHealthPoints){ + character->maxHealthPoints = newMaxHealthPoints; +} + +int getCharacterMaxHealthPoints (Character *character){ + return character->maxHealthPoints; +} + +void setCharacterAttack (Character *character, int newAttack){ + character->attack = newAttack; +} + +int getCharacterAttack (Character *character){ + return character->attack; +} + +void setCharacterArmor (Character *character, int newArmor){ + character->armor = newArmor; +} + +int getCharacterArmor (Character *character){ + return character->armor; +} + +void setCharacterGold (Character *character, int newGold){ + character->gold = newGold; +} + +int getCharacterGold (Character *character){ + return character->gold; +} + + void increaseStat (Character *character, int stat, int amount) { switch (stat) { diff --git a/src/c/character.h b/src/c/character.h index 7ed09b4..808c8d0 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -2,7 +2,7 @@ #define CHARACTER_H typedef struct { - int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; + int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp,attack,armor,maxHealthPoints,gold; char name [50]; } Character; @@ -42,5 +42,21 @@ void increaseStat (Character *character, int stat, int amount); int calculateStatIncrease (Character *character, int amount); +void setCharacterMaxHealthPoints (Character *character, int newMaxHealthPoints); + +int getCharacterMaxHealthPoints (Character *character); + +void setCharacterAttack (Character *character, int newAttack); + +int getCharacterAttack (Character *character); + +void setCharacterArmor (Character *character, int newArmor); + +int getCharacterArmor (Character *character); + +void setCharacterGold (Character *character, int newGold); + +int getCharacterGold (Character *character); + void levelUp (Character *character); #endif \ No newline at end of file diff --git a/src/c/encounter.c b/src/c/encounter.c index 5ea3027..80559ba 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -3,9 +3,12 @@ #include #include +#include "character.h" #include "encounter.h" + #include "playerinput.h" #include "helper.h" +#include "utils.h" /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt @@ -26,10 +29,10 @@ bool playerAlive(int health) } } -int playerHealth(int health, int damage, int armor) +int playerHeal(int health, int damage, Character* character) { - const int maxhealth = 100; - health = health - damage; + int maxhealth = getCharacterMaxHealthPoints(character); + health = health + damage; if (health > maxhealth) { health = maxhealth; @@ -37,6 +40,16 @@ int playerHealth(int health, int damage, int armor) return health; } +int playerDamaged(int health, int damage, int armor, Character* character) +{ + int damagedealt = damage - armor; + if (damagedealt < 1) + { + damagedealt = 1; + } + return health - damagedealt; +} + void enemyHeal(enemy *enemy, int healAmount) { int currentHealth = getEnemyHealth(enemy); @@ -68,21 +81,23 @@ int switchTurns(int currentTurn) return currentTurn; } -int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy* enemy) +int fight(Character *character, enemy* enemy) { + int playerH = 0; int currentTurn = 2; char decision; - while (playerAlive(playerH) && getEnemyHealth(enemy) > 0) + while (playerAlive(getCharacterHealthPoints(character)) && getEnemyHealth(enemy) > 0) { if (currentTurn != 1) { decision = playerInputChar(); switch(decision){ case 'a': - enemyDamaged(enemy, playerDamage); + enemyDamaged(enemy, getCharacterAttack(character)); break; case 'h': - playerH = playerHealth(playerH, -10, playerArmor); + playerH = playerHeal(getCharacterHealthPoints(character), 10, character); + setCharacterHealthPoints(character, playerH); break; case 'f': return 2; @@ -97,33 +112,25 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } else { - playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor); + playerH = playerDamaged(getCharacterHealthPoints(character), getEnemyDamage(enemy), getCharacterArmor(character), character); + setCharacterHealthPoints(character, playerH); } } currentTurn = switchTurns(currentTurn); } - if (playerAlive(playerH)) + if (playerAlive(getCharacterHealthPoints(character))) { + setCharacterExp(character, getCharacterExp(character) + getEnemyExp(enemy)); + setCharacterGold(character, getCharacterGold(character) + getEnemyGold(enemy)); return 1; } else { return 0; - } -} - -int randomIntRange(int min, int max) -{ - int value = randomInt(); - return (value % (max - min + 1)) + min; + } } -int map(int x, int in_min, int in_max, int out_min, int out_max) -{ - //vgl Arduino map() https://www.arduino.cc/reference/en/language/functions/math/map/ - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; -} bool enemyChoosesHeal(enemy* enemy) { @@ -131,7 +138,15 @@ bool enemyChoosesHeal(enemy* enemy) int maxHealth = getEnemyMaxHealth(enemy); int healthd20 = 20 - map(currentHealth, 0, maxHealth, 0, 20); int rolld20 = randomIntRange(1, 20); - return (healthd20 + rolld20) >= 30; + if((healthd20 + rolld20) >= 30 && getEnemyHealPotions(enemy) > 0) + { + setEnemyHealPotions(enemy, getEnemyHealPotions(enemy) - 1); + return true; + } + else + { + return false; + } } // Getter/Setter Funktionen @@ -168,4 +183,25 @@ int getEnemyDamage(enemy* enemy) int getEnemyMaxHealth(enemy* enemy) { return enemy->maxHealth; +} + +int getEnemyHealPotions(enemy* enemy) +{ + return enemy->healPotions; +} + +void setEnemyHealPotions(enemy* enemy, int newPotions) +{ + enemy->healPotions = newPotions; +} + +int getEnemyExp(enemy* enemy){ + return enemy->exp; +} +void setEnemyExp(enemy* enemy, int newExp){ + enemy->exp = newExp; +} + +int getEnemyGold(enemy* enemy){ + return enemy->gold; } \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index 312f35a..4a2c1ff 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -8,33 +8,41 @@ typedef struct enemy{ int damage; int armor; int maxHealth; + int healPotions; + int exp; + int gold; } enemy; bool playerAlive(int health); -int playerHealth(int health, int damage, int armor); +int playerHeal(int health, int damage, Character* character); +int playerDamaged(int health, int damage, int armor, Character* character); void enemyHeal(enemy *enemy, int healAmount); void enemyDamaged(enemy* enemy, int damage); bool enemyChoosesHeal(enemy* enemy); int switchTurns(int currentTurn); -int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy* enemy); +int fight(Character *character, enemy* enemy); -//Funktionen die Mathematische Berechnungen durchführen -int map(int x, int in_min, int in_max, int out_min, int out_max); -int randomIntRange(int min, int max); -//Getter/Setter Funktionen -//setEnemyHealth(&enemy, health); -void setEnemyHealth(enemy* enemy, int newhealth); -//setEnemyDamage(&enemy.damage, damage); -void setEnemyDamage(enemy* enemy, int newdamage); -//setEnemyArmor(&enemy.armor, armor); -void setEnemyArmor(enemy* enemy, int newarmor); -//Function to get the value of Data in a struct, needs a pointer to the struct +//Getter/Setter Funktionen int getEnemyMaxHealth(enemy* enemy); + +int getEnemyGold(enemy* enemy); + int getEnemyHealth(enemy* enemy); +void setEnemyHealth(enemy* enemy, int newhealth); + int getEnemyArmor(enemy* enemy); +void setEnemyArmor(enemy* enemy, int newarmor); + int getEnemyDamage(enemy* enemy); +void setEnemyDamage(enemy* enemy, int newdamage); + +int getEnemyHealPotions(enemy* enemy); +void setEnemyHealPotions(enemy* enemy, int newPotions); + +int getEnemyExp(enemy* enemy); +void setEnemyExp(enemy* enemy, int newExp); #endif \ No newline at end of file diff --git a/src/c/utils.c b/src/c/utils.c new file mode 100644 index 0000000..8686724 --- /dev/null +++ b/src/c/utils.c @@ -0,0 +1,15 @@ +#include "utils.h" +#include "helper.h" + +int randomIntRange(int min, int max) +{ + int value = randomInt(); + return (value % (max - min + 1)) + min; +} + + +int map(int x, int in_min, int in_max, int out_min, int out_max) +{ + //vgl Arduino map() https://www.arduino.cc/reference/en/language/functions/math/map/ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} \ No newline at end of file diff --git a/src/c/utils.h b/src/c/utils.h new file mode 100644 index 0000000..eed2e44 --- /dev/null +++ b/src/c/utils.h @@ -0,0 +1,7 @@ +#ifndef UTILS_H +#define UTILS_H + +int randomIntRange(int min, int max); +int map(int x, int in_min, int in_max, int out_min, int out_max); + +#endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 0f07a36..023d470 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -1,14 +1,17 @@ #ifdef TEST #include "unity.h" +#include "character.h" #include "encounter.h" #include "playerinput.h" #include "mock_playerinput.h" #include "helper.h" #include "mock_helper.h" +#include "utils.h" + void setUp(void) -{ +{ } void teardown(void) @@ -46,7 +49,8 @@ void test_playerIsDamaged(void) int armor = 0; int expectedHealth = 90; // act - health = playerHealth(health, damage, armor); + Character testChar = {10,10,10,health,100,1,0,100,damage,armor,100}; + health = playerDamaged(health, damage, armor, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -54,12 +58,13 @@ void test_playerIsDamaged(void) void test_playerIsNotOverhealed(void) { // arrange - int health = 100; + int health = 95; int armor = 0; - int heal = -10; + int heal = 10; int expectedHealth = 100; // act - health = playerHealth(health, heal, armor); + Character testChar = {10,10,10,health,100,1,0,100,10,armor,100}; + health = playerHeal(health, heal, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -183,13 +188,15 @@ void test_switchingTurns(void) void test_FightPlayerWins(void) { // arange - int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; + int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerMaxHealth = 100; int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; - // aCt + // act + //strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp,attack,armor,maxHealthPoints; + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + result = fight(&testChar, &test); // assert TEST_ASSERT_EQUAL(1, result); } @@ -197,14 +204,15 @@ void test_FightPlayerWins(void) void test_FightEnemyWins(void) { // arange - int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; + int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerMaxHealth = 5; int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(1); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + result = fight(&testChar, &test); // assert TEST_ASSERT_EQUAL(0, result); } @@ -212,13 +220,14 @@ void test_FightEnemyWins(void) void test_FightPlayerChoosesAttack(void) { // arrange - int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; + int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerMaxHealth = 5; int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; - fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + fight(&testChar, &test); result = getEnemyHealth(&test); // assert TEST_ASSERT_EQUAL(0, result); @@ -227,17 +236,18 @@ void test_FightPlayerChoosesAttack(void) void test_FightPlayerHeals_thenAttacks_Wins(void) { // arrange - int playerHealth = 2, playerDamage = 10, playerArmor = 4, playerAttack = 10; + int playerHealth = 2, playerDamage = 10, playerArmor = 4, playerMaxHealth = 10; int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInputChar_ExpectAndReturn('h'); randomInt_ExpectAndReturn(1); playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(1); playerInputChar_ExpectAndReturn('a'); - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + result = fight(&testChar, &test); // assert TEST_ASSERT_EQUAL(1, result); } @@ -245,13 +255,14 @@ void test_FightPlayerHeals_thenAttacks_Wins(void) void test_FightPlayerFlees(void) { // arrange - int playerHealth = 10, playerDamage = 10, playerArmor = 4, playerAttack = 10; + int playerHealth = 10, playerDamage = 10, playerArmor = 4, playerMaxHealth = 10; int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInputChar_ExpectAndReturn('f'); - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + result = fight(&testChar, &test); // assert TEST_ASSERT_EQUAL(2, result); } @@ -331,7 +342,7 @@ void test_enemyChoosesHeal(void) //arange bool result; int enemyHealth = 50, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth, 1}; //act randomInt_ExpectAndReturn(39); //39%20 = 19 , 19 + 1 = 20 result = enemyChoosesHeal(&test); @@ -359,17 +370,30 @@ 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}; + int playerHealth = 10, playerDamage = 10, playerArmor = 0, playerMaxHealth = 10; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth,1 }; //act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(39); //39%20 = 19 , 19 + 1 = 20 playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(0); //0%20 = 0 , 0 + 1 = 1 - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test2); + result = fight(&testChar, &test); //assert TEST_ASSERT_EQUAL(0, result); } +void test_enemyHealsNoPotion(void) +{ + int enemyHealth = 6, enemyDamage = 10, enemyArmor = 5, enemyMaxHealth = 100; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth, 0}; + + randomInt_ExpectAndReturn(39); //39%20 = 19 , 19 + 1 = 20 + bool result = enemyChoosesHeal(&test); + + TEST_ASSERT_FALSE(result); +} + + #endif // TEST