From 0320602a0ad4b6066a3649e241edf799a525fd27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 20:03:48 +0100 Subject: [PATCH 01/11] Added new variables to character --- src/c/character.c | 24 ++++++++++++++++++++++++ src/c/character.h | 14 +++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/c/character.c b/src/c/character.c index 9c436ff..344b0e1 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -66,6 +66,30 @@ 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 increaseStat (Character *character, int stat, int amount) { switch (stat) { diff --git a/src/c/character.h b/src/c/character.h index 7ed09b4..acae866 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; char name [50]; } Character; @@ -42,5 +42,17 @@ 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 levelUp (Character *character); #endif \ No newline at end of file From 9ff319fbb48c67e88d5a682aac4577e84e97dc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 20:29:58 +0100 Subject: [PATCH 02/11] refactoring: replaced placeholders --- src/c/encounter.c | 7 ++++++- src/c/encounter.h | 2 +- test/c/test_encounter.c | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 5ea3027..260885d 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -3,7 +3,9 @@ #include #include +#include "character.h" #include "encounter.h" + #include "playerinput.h" #include "helper.h" @@ -68,8 +70,11 @@ 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 = getCharacterHealthPoints(character); + int playerDamage = getCharacterAttack(character); + int playerArmor = getCharacterArmor(character); int currentTurn = 2; char decision; while (playerAlive(playerH) && getEnemyHealth(enemy) > 0) diff --git a/src/c/encounter.h b/src/c/encounter.h index 312f35a..a6c01ac 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -17,7 +17,7 @@ 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); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 0f07a36..355fad9 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -1,14 +1,16 @@ #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" + void setUp(void) -{ +{ } void teardown(void) @@ -186,10 +188,12 @@ void test_FightPlayerWins(void) int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; 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,100}; 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); } @@ -201,10 +205,11 @@ void test_FightEnemyWins(void) int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; 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); } @@ -216,9 +221,10 @@ void test_FightPlayerChoosesAttack(void) int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; 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); @@ -231,13 +237,14 @@ void test_FightPlayerHeals_thenAttacks_Wins(void) int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; 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); } @@ -249,9 +256,10 @@ void test_FightPlayerFlees(void) int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; 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); } @@ -362,11 +370,12 @@ void test_enemyChoosesHeal_ThenAttackWins(void) int playerHealth = 10, playerDamage = 10, playerArmor = 5, playerAttack = 10; enemy test2 = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; //act + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; 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, &test2); //assert TEST_ASSERT_EQUAL(0, result); From 0d3e29f592cfedec9a9f6b079b20d701c274afc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Tue, 24 Jan 2023 12:39:06 +0100 Subject: [PATCH 03/11] refactoring: changed variable names in test_enc --- test/c/test_encounter.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 355fad9..2d205c0 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -185,12 +185,12 @@ 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 //strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp,attack,armor,maxHealthPoints; - Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; result = fight(&testChar, &test); @@ -201,11 +201,11 @@ 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,100}; + 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}; @@ -217,11 +217,11 @@ 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,100}; + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; fight(&testChar, &test); @@ -233,11 +233,11 @@ 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,100}; + 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); @@ -252,11 +252,11 @@ 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,100}; + Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInputChar_ExpectAndReturn('f'); result = fight(&testChar, &test); @@ -367,15 +367,15 @@ 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 = 5, playerMaxHealth = 10; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; //act - Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,100}; + 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(&testChar, &test2); + result = fight(&testChar, &test); //assert TEST_ASSERT_EQUAL(0, result); From e633c8c6d33d7129cc4f3a684e169bffc73204cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 10:26:55 +0100 Subject: [PATCH 04/11] refactoring: playerHealth() now uses Char Struct --- src/c/encounter.c | 8 ++++---- src/c/encounter.h | 2 +- test/c/test_encounter.c | 6 ++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 260885d..428273f 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -28,9 +28,9 @@ bool playerAlive(int health) } } -int playerHealth(int health, int damage, int armor) +int playerHealth(int health, int damage, int armor, Character* character) { - const int maxhealth = 100; + int maxhealth = getCharacterMaxHealthPoints(character); health = health - damage; if (health > maxhealth) { @@ -87,7 +87,7 @@ int fight(Character *character, enemy* enemy) enemyDamaged(enemy, playerDamage); break; case 'h': - playerH = playerHealth(playerH, -10, playerArmor); + playerH = playerHealth(playerH, -10, playerArmor, character); break; case 'f': return 2; @@ -102,7 +102,7 @@ int fight(Character *character, enemy* enemy) } else { - playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor); + playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor, character); } } currentTurn = switchTurns(currentTurn); diff --git a/src/c/encounter.h b/src/c/encounter.h index a6c01ac..d0fd484 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -12,7 +12,7 @@ typedef struct enemy{ bool playerAlive(int health); -int playerHealth(int health, int damage, int armor); +int playerHealth(int health, int damage, int armor, Character* character); void enemyHeal(enemy *enemy, int healAmount); void enemyDamaged(enemy* enemy, int damage); bool enemyChoosesHeal(enemy* enemy); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 2d205c0..5ed7f3a 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -48,7 +48,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 = playerHealth(health, damage, armor, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -61,7 +62,8 @@ void test_playerIsNotOverhealed(void) 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 = playerHealth(health, heal, armor, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } From d6cdcd00a6989632b2d84b93cbd9e5eb25c2bf7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 10:37:48 +0100 Subject: [PATCH 05/11] refactoring: fight function switched to structs --- src/c/encounter.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 428273f..ce999f5 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -72,22 +72,21 @@ int switchTurns(int currentTurn) int fight(Character *character, enemy* enemy) { - int playerH = getCharacterHealthPoints(character); - int playerDamage = getCharacterAttack(character); - int playerArmor = getCharacterArmor(character); + 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, character); + playerH = playerHealth(getCharacterHealthPoints(character), -10, getCharacterArmor(character), character); + setCharacterHealthPoints(character, playerH); break; case 'f': return 2; @@ -102,19 +101,20 @@ int fight(Character *character, enemy* enemy) } else { - playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor, character); + playerH = playerHealth(getCharacterHealthPoints(character), getEnemyDamage(enemy), getCharacterArmor(character), character); + setCharacterHealthPoints(character, playerH); } } currentTurn = switchTurns(currentTurn); } - if (playerAlive(playerH)) + if (playerAlive(getCharacterHealthPoints(character))) { return 1; } else { return 0; - } + } } int randomIntRange(int min, int max) From b2c7caa08be178e8d7c2ccd5f65e5eaeecaf6973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 10:51:52 +0100 Subject: [PATCH 06/11] refactoring: Split play Heal/Damage --- src/c/encounter.c | 17 ++++++++++++++--- src/c/encounter.h | 3 ++- test/c/test_encounter.c | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index ce999f5..ea86bd0 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -28,7 +28,18 @@ bool playerAlive(int health) } } -int playerHealth(int health, int damage, int armor, Character* character) +int playerHeal(int health, int damage, int armor, Character* character) +{ + int maxhealth = getCharacterMaxHealthPoints(character); + health = health - damage; + if (health > maxhealth) + { + health = maxhealth; + } + return health; +} + +int playerDamaged(int health, int damage, int armor, Character* character) { int maxhealth = getCharacterMaxHealthPoints(character); health = health - damage; @@ -85,7 +96,7 @@ int fight(Character *character, enemy* enemy) enemyDamaged(enemy, getCharacterAttack(character)); break; case 'h': - playerH = playerHealth(getCharacterHealthPoints(character), -10, getCharacterArmor(character), character); + playerH = playerHeal(getCharacterHealthPoints(character), -10, getCharacterArmor(character), character); setCharacterHealthPoints(character, playerH); break; case 'f': @@ -101,7 +112,7 @@ int fight(Character *character, enemy* enemy) } else { - playerH = playerHealth(getCharacterHealthPoints(character), getEnemyDamage(enemy), getCharacterArmor(character), character); + playerH = playerDamaged(getCharacterHealthPoints(character), getEnemyDamage(enemy), getCharacterArmor(character), character); setCharacterHealthPoints(character, playerH); } } diff --git a/src/c/encounter.h b/src/c/encounter.h index d0fd484..59b94f9 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -12,7 +12,8 @@ typedef struct enemy{ bool playerAlive(int health); -int playerHealth(int health, int damage, int armor, Character* character); +int playerHeals(int health, int damage, int armor, 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); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 5ed7f3a..e4229b8 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -49,7 +49,7 @@ void test_playerIsDamaged(void) int expectedHealth = 90; // act Character testChar = {10,10,10,health,100,1,0,100,damage,armor,100}; - health = playerHealth(health, damage, armor, &testChar); + health = playerDamaged(health, damage, armor, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -63,7 +63,7 @@ void test_playerIsNotOverhealed(void) int expectedHealth = 100; // act Character testChar = {10,10,10,health,100,1,0,100,10,armor,100}; - health = playerHealth(health, heal, armor, &testChar); + health = playerHeal(health, heal, armor, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } From d092c9f5b90b1e5cb55f4a02a17e6dfe7b5cd7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 10:59:47 +0100 Subject: [PATCH 07/11] Changed Heal and Damage Functions --- src/c/encounter.c | 15 +++++++-------- src/c/encounter.h | 2 +- test/c/test_encounter.c | 8 ++++---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index ea86bd0..326d498 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -28,10 +28,10 @@ bool playerAlive(int health) } } -int playerHeal(int health, int damage, int armor, Character* character) +int playerHeal(int health, int damage, Character* character) { int maxhealth = getCharacterMaxHealthPoints(character); - health = health - damage; + health = health + damage; if (health > maxhealth) { health = maxhealth; @@ -41,13 +41,12 @@ int playerHeal(int health, int damage, int armor, Character* character) int playerDamaged(int health, int damage, int armor, Character* character) { - int maxhealth = getCharacterMaxHealthPoints(character); - health = health - damage; - if (health > maxhealth) + int damagedealt = damage - armor; + if (damagedealt < 1) { - health = maxhealth; + damagedealt = 1; } - return health; + return health - damagedealt; } void enemyHeal(enemy *enemy, int healAmount) @@ -96,7 +95,7 @@ int fight(Character *character, enemy* enemy) enemyDamaged(enemy, getCharacterAttack(character)); break; case 'h': - playerH = playerHeal(getCharacterHealthPoints(character), -10, getCharacterArmor(character), character); + playerH = playerHeal(getCharacterHealthPoints(character), 10, character); setCharacterHealthPoints(character, playerH); break; case 'f': diff --git a/src/c/encounter.h b/src/c/encounter.h index 59b94f9..efc7e61 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -12,7 +12,7 @@ typedef struct enemy{ bool playerAlive(int health); -int playerHeals(int health, int damage, int armor, Character* character); +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); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index e4229b8..a5f08cb 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -57,13 +57,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 Character testChar = {10,10,10,health,100,1,0,100,10,armor,100}; - health = playerHeal(health, heal, armor, &testChar); + health = playerHeal(health, heal, &testChar); // assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -369,7 +369,7 @@ void test_enemyChoosesHeal_ThenAttackWins(void) //arange int result; int enemyHealth = 6, enemyDamage = 10, enemyArmor = 5, enemyMaxHealth = 100; - int playerHealth = 10, playerDamage = 10, playerArmor = 5, playerMaxHealth = 10; + int playerHealth = 10, playerDamage = 10, playerArmor = 0, playerMaxHealth = 10; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; //act Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; From 2b3c1809f14f825791c4ef406bfa1d5ed3755f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 14:52:58 +0100 Subject: [PATCH 08/11] enemy now needs potions to heal --- src/c/encounter.c | 20 +++++++++++++++++++- src/c/encounter.h | 4 ++++ test/c/test_encounter.c | 16 ++++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 326d498..54a5067 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -146,7 +146,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 @@ -183,4 +191,14 @@ 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; } \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index efc7e61..e5ca6d2 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -8,6 +8,7 @@ typedef struct enemy{ int damage; int armor; int maxHealth; + int healPotions; } enemy; @@ -38,4 +39,7 @@ int getEnemyHealth(enemy* enemy); int getEnemyArmor(enemy* enemy); int getEnemyDamage(enemy* enemy); +int getEnemyHealPotions(enemy* enemy); +void setEnemyHealPotions(enemy* enemy, int newPotions); + #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index a5f08cb..ecca7d9 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -341,7 +341,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); @@ -370,7 +370,7 @@ void test_enemyChoosesHeal_ThenAttackWins(void) int result; int enemyHealth = 6, enemyDamage = 10, enemyArmor = 5, enemyMaxHealth = 100; int playerHealth = 10, playerDamage = 10, playerArmor = 0, playerMaxHealth = 10; - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth,1 }; //act Character testChar = {10,10,10,playerHealth,100,1,0,100,playerDamage,playerArmor,playerMaxHealth}; playerInputChar_ExpectAndReturn('a'); @@ -383,4 +383,16 @@ void test_enemyChoosesHeal_ThenAttackWins(void) } +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 From 89a76a1a36a1d23eb58936f7a825e56a42f6702a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 19:46:22 +0100 Subject: [PATCH 09/11] player gets exp after combat --- src/c/encounter.c | 8 ++++++++ src/c/encounter.h | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index 54a5067..0f96c8c 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -119,6 +119,7 @@ int fight(Character *character, enemy* enemy) } if (playerAlive(getCharacterHealthPoints(character))) { + setCharacterExp(character, getCharacterExp(character) + getEnemyExp(enemy)); return 1; } else @@ -201,4 +202,11 @@ int getEnemyHealPotions(enemy* enemy) 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; } \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index e5ca6d2..5e1eef8 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -9,6 +9,7 @@ typedef struct enemy{ int armor; int maxHealth; int healPotions; + int exp; } enemy; @@ -42,4 +43,7 @@ int getEnemyDamage(enemy* enemy); 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 From a18fede745dc692dfbc504d4db44c87e91b02ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 19:51:39 +0100 Subject: [PATCH 10/11] refactoring: moved useful functions to dif library --- src/c/encounter.c | 12 +----------- src/c/utils.c | 15 +++++++++++++++ src/c/utils.h | 7 +++++++ test/c/test_encounter.c | 1 + 4 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 src/c/utils.c create mode 100644 src/c/utils.h diff --git a/src/c/encounter.c b/src/c/encounter.c index 0f96c8c..873d9f7 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -8,6 +8,7 @@ #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 @@ -128,18 +129,7 @@ int fight(Character *character, enemy* enemy) } } -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) { 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 ecca7d9..023d470 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -7,6 +7,7 @@ #include "mock_playerinput.h" #include "helper.h" #include "mock_helper.h" +#include "utils.h" void setUp(void) From 85979f40aed2d5a474eebec87949ad4e69ab3799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Wed, 25 Jan 2023 20:08:38 +0100 Subject: [PATCH 11/11] player can now get gold after a fight --- src/c/character.c | 9 +++++++++ src/c/character.h | 6 +++++- src/c/encounter.c | 5 +++++ src/c/encounter.h | 21 ++++++++++----------- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 344b0e1..2d1d317 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -90,6 +90,15 @@ 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 acae866..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,attack,armor,maxHealthPoints; + int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp,attack,armor,maxHealthPoints,gold; char name [50]; } Character; @@ -54,5 +54,9 @@ 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 873d9f7..80559ba 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -121,6 +121,7 @@ int fight(Character *character, enemy* enemy) if (playerAlive(getCharacterHealthPoints(character))) { setCharacterExp(character, getCharacterExp(character) + getEnemyExp(enemy)); + setCharacterGold(character, getCharacterGold(character) + getEnemyGold(enemy)); return 1; } else @@ -199,4 +200,8 @@ int getEnemyExp(enemy* enemy){ } 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 5e1eef8..4a2c1ff 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -10,6 +10,7 @@ typedef struct enemy{ int maxHealth; int healPotions; int exp; + int gold; } enemy; @@ -22,23 +23,21 @@ bool enemyChoosesHeal(enemy* enemy); int switchTurns(int currentTurn); 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);