diff --git a/src/c/character.c b/src/c/character.c index 2d1d317..3ccf7ba 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -98,6 +98,37 @@ int getCharacterGold (Character *character){ return character->gold; } +void setCharacterName (Character *character, char *newName) { + strcpy(character->name,newName); +} + +char* getCharacterName (Character *character) { + return character->name; +} + +int getWeaponAttack (Weapon *weapon) { + return weapon->attack; +} + +void setWeaponAttack (Weapon *weapon, int newAttack) { + weapon->attack = newAttack; +} + +int getWeaponDurability (Weapon *weapon) { + return weapon->durability; +} + +int setWeaponDurability (Weapon *weapon, int newDurability) { + weapon->durability = newDurability; +} + +void setCharacterWeaponClass (Character* character, int weaponClass) { + character->weaponClass = weaponClass; +} + +int getCharacterWeaponClass (Character* character) { + return character->weaponClass; +} void increaseStat (Character *character, int stat, int amount) { switch (stat) @@ -127,4 +158,18 @@ void levelUp (Character *character) { increaseStat(character,LEVEL,1); setCharacterExp(character,getCharacterExp(character)-getCharacterMaxExp(character)); } +} + +int calculateDamage (Character *character,Weapon *weapon) { + switch (character->weaponClass) + { + case 1: + return character->strength*weapon->attack; + case 2: + return character->dexterity*weapon->attack; + case 3: + return character->intelligence*weapon->attack; + default: + return 1; + } } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index 808c8d0..dff4d81 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -2,10 +2,18 @@ #define CHARACTER_H typedef struct { - int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp,attack,armor,maxHealthPoints,gold; + int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; + int attack,armor,maxHealthPoints,gold,items[10],weaponClass; char name [50]; } Character; +typedef struct { + int attack,durability; + char name [50]; +} Weapon; +enum { + SWORD = 1, BOW = 2, STAFF = 3 +}; enum { STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6, EXP = 7, MAXEXP = 8 }; @@ -59,4 +67,22 @@ void setCharacterGold (Character *character, int newGold); int getCharacterGold (Character *character); void levelUp (Character *character); + +void setCharacterName (Character *character, char newName[]); + +char* getCharacterName (Character *character); + +void setCharacterWeaponClass (Character *character, int newWeaponClass); + +int getCharacterWeaponClass (Character *character); + +int getWeaponAttack (Weapon *weapon); + +void setWeaponAttack (Weapon *weapon, int newAttack); + +int getWeaponDurability (Weapon *weapon); + +int setWeaponDurability (Weapon *weapon, int newDurability); + +int calculateDamage (Character *character,Weapon *weapon); #endif \ No newline at end of file diff --git a/src/c/encounter.c b/src/c/encounter.c index 80559ba..aa8287d 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -17,9 +17,9 @@ humanoide gegner haben heiltränke mit denen sie sich ggf heilen. heilung erfolgt dann, wenn bestimmte hp (50%) unterschritten wird. wird allerdings gewürfelt, je niedriger die hp%, desto höher die chance. */ -bool playerAlive(int health) +bool playerAlive(Character *character) { - if (health > 0) + if (getCharacterHealthPoints(character) > 0) { return true; } @@ -29,48 +29,43 @@ bool playerAlive(int health) } } -int playerHeal(int health, int damage, Character* character) +int playerHeal(Character* character, int amount) { - int maxhealth = getCharacterMaxHealthPoints(character); - health = health + damage; - if (health > maxhealth) + setCharacterHealthPoints(character, getCharacterHealthPoints(character)+ amount); + if (getCharacterHealthPoints(character) > getCharacterMaxHealthPoints(character)) { - health = maxhealth; + setCharacterHealthPoints(character, getCharacterMaxHealthPoints(character)); } - return health; + return getCharacterHealthPoints(character); } -int playerDamaged(int health, int damage, int armor, Character* character) +int playerDamaged(enemy* enemy, Character* character) { - int damagedealt = damage - armor; + int damagedealt = getEnemyDamage(enemy) - getCharacterArmor(character); if (damagedealt < 1) { damagedealt = 1; } - return health - damagedealt; + return getCharacterHealthPoints(character) - damagedealt; } void enemyHeal(enemy *enemy, int healAmount) { - int currentHealth = getEnemyHealth(enemy); - int maxHealth = getEnemyMaxHealth(enemy); - if (currentHealth + healAmount > maxHealth) + if (getEnemyHealth(enemy) + healAmount > getEnemyMaxHealth(enemy)) { - healAmount = maxHealth - currentHealth; + healAmount = getEnemyMaxHealth(enemy) - getEnemyHealth(enemy); } - setEnemyHealth(enemy, currentHealth + healAmount); + setEnemyHealth(enemy, getEnemyHealth(enemy) + healAmount); } -void enemyDamaged(enemy *enemy, int damage) +void enemyDamaged(enemy *enemy,Character* character) { - int armor = getEnemyArmor(enemy); - int currentHealth = getEnemyHealth(enemy); - int damagedealt = damage - armor; + int damagedealt = getCharacterAttack(character) - getEnemyArmor(enemy); if (damagedealt < 1) { damagedealt = 1; } - setEnemyHealth(enemy, currentHealth - damagedealt); + setEnemyHealth(enemy, getEnemyHealth(enemy) - damagedealt); } @@ -86,17 +81,17 @@ int fight(Character *character, enemy* enemy) int playerH = 0; int currentTurn = 2; char decision; - while (playerAlive(getCharacterHealthPoints(character)) && getEnemyHealth(enemy) > 0) + while (playerAlive(character) && getEnemyHealth(enemy) > 0) { if (currentTurn != 1) { decision = playerInputChar(); switch(decision){ case 'a': - enemyDamaged(enemy, getCharacterAttack(character)); + enemyDamaged(enemy, character); break; case 'h': - playerH = playerHeal(getCharacterHealthPoints(character), 10, character); + playerH = playerHeal(character,10); setCharacterHealthPoints(character, playerH); break; case 'f': @@ -112,13 +107,13 @@ int fight(Character *character, enemy* enemy) } else { - playerH = playerDamaged(getCharacterHealthPoints(character), getEnemyDamage(enemy), getCharacterArmor(character), character); + playerH = playerDamaged(enemy, character); setCharacterHealthPoints(character, playerH); } } currentTurn = switchTurns(currentTurn); } - if (playerAlive(getCharacterHealthPoints(character))) + if (playerAlive(character)) { setCharacterExp(character, getCharacterExp(character) + getEnemyExp(enemy)); setCharacterGold(character, getCharacterGold(character) + getEnemyGold(enemy)); @@ -130,7 +125,9 @@ int fight(Character *character, enemy* enemy) } } - +int rollInitiative (Character *character) { + return randomIntRange(1,20) + character->dexterity; +} bool enemyChoosesHeal(enemy* enemy) { diff --git a/src/c/encounter.h b/src/c/encounter.h index 4a2c1ff..071861f 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -14,11 +14,12 @@ typedef struct enemy{ } enemy; -bool playerAlive(int health); -int playerHeal(int health, int damage, Character* character); -int playerDamaged(int health, int damage, int armor, Character* character); +bool playerAlive(Character *character); +int playerHeal(Character* character,int amount); +int playerDamaged(enemy* enemy, Character* character); +int rollInitiative (Character *character); void enemyHeal(enemy *enemy, int healAmount); -void enemyDamaged(enemy* enemy, int damage); +void enemyDamaged(enemy* enemy, Character* character); bool enemyChoosesHeal(enemy* enemy); int switchTurns(int currentTurn); int fight(Character *character, enemy* enemy); diff --git a/test/c/test_character.c b/test/c/test_character.c index 6d9fd46..5733e4d 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -3,6 +3,7 @@ #include "character.h" Character testCharacter; Character testCharacter2; +Weapon testWeapon; void setUp(void) { @@ -20,6 +21,11 @@ void setUp(void) testCharacter2.exp = 110; testCharacter.maxExp = 100; testCharacter2.maxExp = 100; + testCharacter.attack = 5; + testCharacter2.weaponClass = SWORD; + + testWeapon.attack = 5; + testWeapon.durability = 100; } void tearDown(void) @@ -117,4 +123,19 @@ void test_levelUp_notEnoughExp_loseExp (void) { TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); } +void test_calculateDamage_Sword (void) { + TEST_ASSERT_EQUAL_INT(25,calculateDamage(&testCharacter2,&testWeapon)); +} + +void test_calculateDamage_Bow (void) { + setCharacterWeaponClass(&testCharacter2,BOW); + + TEST_ASSERT_EQUAL_INT(25,calculateDamage(&testCharacter2,&testWeapon)); +} + +void test_calculateDamage_Staff (void) { + setCharacterWeaponClass(&testCharacter2,STAFF); + + TEST_ASSERT_EQUAL_INT(35,calculateDamage(&testCharacter2,&testWeapon)); +} #endif // TEST \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 023d470..d74591e 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -9,9 +9,18 @@ #include "mock_helper.h" #include "utils.h" - +Character testcharacter; +enemy testenemy; void setUp(void) { + testcharacter.dexterity = 5; + testcharacter.healthPoints = 100; + testcharacter.maxHealthPoints = 100; + testcharacter.attack = 10; + + testenemy.damage = 10; + testenemy.health = 50; + testenemy.armor = 10; } void teardown(void) @@ -24,7 +33,7 @@ void test_isPlayerAlive_healthGreaterZero(void) int health = 100; bool result; // act - result = playerAlive(health); + result = playerAlive(&testcharacter); // assert TEST_ASSERT_TRUE(result); } @@ -32,10 +41,10 @@ void test_isPlayerAlive_healthGreaterZero(void) void test_isPlayerAlive_healthLowerZero(void) { // arrange - int health = -1; + setCharacterHealthPoints(&testcharacter, -1); bool result; // act - result = playerAlive(health); + result = playerAlive(&testcharacter); // assert TEST_ASSERT_FALSE(result); @@ -44,29 +53,22 @@ void test_isPlayerAlive_healthLowerZero(void) void test_playerIsDamaged(void) { // arrange - int health = 100; - int damage = 10; - int armor = 0; int expectedHealth = 90; // act - Character testChar = {10,10,10,health,100,1,0,100,damage,armor,100}; - health = playerDamaged(health, damage, armor, &testChar); + int result = playerDamaged(&testenemy, &testcharacter); // assert - TEST_ASSERT_EQUAL(expectedHealth, health); + TEST_ASSERT_EQUAL(expectedHealth, result); } void test_playerIsNotOverhealed(void) { // arrange - int health = 95; - int armor = 0; + setCharacterHealthPoints(&testcharacter, 95); int heal = 10; - int expectedHealth = 100; // act - Character testChar = {10,10,10,health,100,1,0,100,10,armor,100}; - health = playerHeal(health, heal, &testChar); + int result = playerHeal(&testcharacter, heal); // assert - TEST_ASSERT_EQUAL(expectedHealth, health); + TEST_ASSERT_EQUAL(getCharacterMaxHealthPoints(&testcharacter), result); } void test_setEnemyHealth(void) @@ -130,31 +132,24 @@ void test_get_setEnemyArmor(void) void test_PlayerAttacksEnemy_DoesDamage(void) { // arrange - int playerDamage = 10; - int enemyHealth = 50; - int enemyArmor = 4; + setEnemyArmor(&testenemy, 4); // health - (damage - armor) int expectedEnemyHealth = 44; // act - enemy test = {enemyHealth, 4, enemyArmor}; - enemyDamaged(&test, playerDamage); + enemyDamaged(&testenemy, &testcharacter); // assert - TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); + TEST_ASSERT_EQUAL(expectedEnemyHealth, getEnemyHealth(&testenemy)); } void test_PlayerAttacksEnemy_DoesMinDamage(void) { // arrange - int playerDamage = 10; - int enemyHealth = 50; - int enemyArmor = 10; // health - (damage - armor) int expectedEnemyHealth = 49; // act - enemy test = {enemyHealth, 4, enemyArmor}; - enemyDamaged(&test, playerDamage); + enemyDamaged(&testenemy,&testcharacter); // assert - TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); + TEST_ASSERT_EQUAL(expectedEnemyHealth, getEnemyHealth(&testenemy)); } // A better way to get the value of a struct, REFACTORING if it works @@ -395,5 +390,10 @@ void test_enemyHealsNoPotion(void) TEST_ASSERT_FALSE(result); } +int test_rollInitiative (void) { + int initiative = rollInitiative(&testcharacter); + + TEST_ASSERT(testcharacter.dexterity + 1 <= initiative && testcharacter.dexterity + 20 >= initiative); +} #endif // TEST