From e6dd447cc57586bb07ecae173f94ce327576bef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 9 Jan 2023 22:02:25 +0100 Subject: [PATCH 01/53] added encounter.c --- src/c/encounter.c | 24 +++++++++++++++++++++ src/c/encounter.h | 9 ++++++++ test/c/test_encounter.c | 46 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/c/encounter.c create mode 100644 src/c/encounter.h create mode 100644 test/c/test_encounter.c diff --git a/src/c/encounter.c b/src/c/encounter.c new file mode 100644 index 0000000..87eca2d --- /dev/null +++ b/src/c/encounter.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +#include + +bool playerAlive(int health){ + if(health > 0){ + return true; + } + else{ + return false; + } +} + +int playerHealth(int health, int damage){ + const int maxhealth = 100; + health = health - damage; + if(health > maxhealth){ + health = maxhealth; + } + return health; +} diff --git a/src/c/encounter.h b/src/c/encounter.h new file mode 100644 index 0000000..16cea26 --- /dev/null +++ b/src/c/encounter.h @@ -0,0 +1,9 @@ +#ifndef ENCOUNTER_H +#define ENCOUNTER_H + +#include + +bool playerAlive(int health); +int playerHealth(int health, int damage); + +#endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c new file mode 100644 index 0000000..2af8a8b --- /dev/null +++ b/test/c/test_encounter.c @@ -0,0 +1,46 @@ +#ifdef TEST +#include "unity.h" + +#include "encounter.h" + +void setUp(void){ + +} + +void teardown(void){ + +} + +void test_isPlayerAlive_healthGreaterZero(void) +{ + //arrange + int health = 100; + //act + //assert + TEST_ASSERT_TRUE(playerAlive(health)); +} + +void test_playerIsDamaged(void) +{ + //arrange + int health = 100; + int damage = 10; + int expectedHealth = 90; + //act + health = playerHealth(health, damage); + //assert + TEST_ASSERT_EQUAL(expectedHealth, health); +} + +void test_playerIsNotOverhealed(void){ + //arrange + int health = 100; + int heal = -10; + int expectedHealth = 100; + //act + health = playerHealth(health, heal); + //assert + TEST_ASSERT_EQUAL(expectedHealth, health); +} + +#endif // TEST From b28be0a441c8c30347c6cefb8303540b5c65ab53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 12 Jan 2023 11:50:28 +0100 Subject: [PATCH 02/53] structs for enemies and a way to set values --- project.yml | 1 + src/c/encounter.c | 16 ++++++++++++++++ src/c/encounter.h | 6 ++++++ test/c/test_encounter.c | 13 +++++++++++++ 4 files changed, 36 insertions(+) diff --git a/project.yml b/project.yml index e253248..33ad5c2 100644 --- a/project.yml +++ b/project.yml @@ -98,4 +98,5 @@ :enabled: - stdout_pretty_tests_report - module_generator + - xml_tests_report ... diff --git a/src/c/encounter.c b/src/c/encounter.c index 87eca2d..a43acba 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -22,3 +22,19 @@ int playerHealth(int health, int damage){ } return health; } + +enemy createEnemy(int health) +{ + enemy test; + test.health = health; + return test; +} + +void *setEnemyHealth(int *num, int health){ + *num = health; + //return health; +} + +int getEnemyHealth(enemy enemy){ + return enemy.health; +} diff --git a/src/c/encounter.h b/src/c/encounter.h index 16cea26..b42072d 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -3,6 +3,12 @@ #include +typedef struct { + int health; +} enemy; + +//setEnemyHealth(&enemy.health, health); +void *setEnemyHealth(int *num, int health); bool playerAlive(int health); int playerHealth(int health, int damage); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 2af8a8b..6b4abdf 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -3,6 +3,7 @@ #include "encounter.h" + void setUp(void){ } @@ -43,4 +44,16 @@ void test_playerIsNotOverhealed(void){ TEST_ASSERT_EQUAL(expectedHealth, health); } +void test_setEnemyHealth(void){ + //arrange + int health = 50, result; + //act + enemy test = {health*2}; + setEnemyHealth(&test.health, health); + //assert + TEST_ASSERT_EQUAL(health, test.health); +} + + + #endif // TEST From 0e98febc243eb555d02850b8ef74ee37bdba7ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 12 Jan 2023 11:58:58 +0100 Subject: [PATCH 03/53] Added a way to read values out of the enemy struct --- src/c/encounter.c | 1 + src/c/encounter.h | 1 + test/c/test_encounter.c | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index a43acba..9bd8a89 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -36,5 +36,6 @@ void *setEnemyHealth(int *num, int health){ } int getEnemyHealth(enemy enemy){ + //gibt health zurück, benötigt keine adress pointer return enemy.health; } diff --git a/src/c/encounter.h b/src/c/encounter.h index b42072d..4dd6bf3 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -9,6 +9,7 @@ typedef struct { //setEnemyHealth(&enemy.health, health); void *setEnemyHealth(int *num, int health); +int getEnemyHealth(enemy enemy); bool playerAlive(int health); int playerHealth(int health, int damage); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 6b4abdf..eb40728 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -54,6 +54,15 @@ void test_setEnemyHealth(void){ TEST_ASSERT_EQUAL(health, test.health); } +void test_getEnemyHealth(void){ + //arrange + int health = 50; + //act + enemy test = {health}; + //assert + TEST_ASSERT_EQUAL(health, getEnemyHealth(test)); +} + #endif // TEST From 6af6e3d224f5ea0d711aad937fc9be486dbe22ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 12 Jan 2023 13:26:21 +0100 Subject: [PATCH 04/53] added damage value and funtions to enemy struct --- src/c/encounter.c | 8 ++++++++ src/c/encounter.h | 10 ++++++++-- test/c/test_encounter.c | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 9bd8a89..d6b8710 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -39,3 +39,11 @@ int getEnemyHealth(enemy enemy){ //gibt health zurück, benötigt keine adress pointer return enemy.health; } + +void *setEnemyDamage(int *num, int damage){ + *num = damage; +} + +int getEnemyDamage(enemy enemy){ + return enemy.damage; +} diff --git a/src/c/encounter.h b/src/c/encounter.h index 4dd6bf3..6fbedfe 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -5,12 +5,18 @@ typedef struct { int health; + int damage; } enemy; + +bool playerAlive(int health); +int playerHealth(int health, int damage); + //setEnemyHealth(&enemy.health, health); void *setEnemyHealth(int *num, int health); int getEnemyHealth(enemy enemy); -bool playerAlive(int health); -int playerHealth(int health, int damage); +//setEnemyDamage(&enemy.damage, damage); +void *setEnemyDamage(int *num, int damage); +int getEnemyDamage(enemy enemy); #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index eb40728..cc57da1 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -63,6 +63,19 @@ void test_getEnemyHealth(void){ TEST_ASSERT_EQUAL(health, getEnemyHealth(test)); } +void test_setEnemyDamage(void){ + int damage = 4, result; + enemy test = {50, damage*2}; + setEnemyDamage(&test.damage, damage); + TEST_ASSERT_EQUAL(damage, test.damage); +} + +void test_getEnemyDamage(void){ + int damage = 4, result; + enemy test = {50, damage}; + result = getEnemyDamage(test); + TEST_ASSERT_EQUAL(damage, result); +} #endif // TEST From ef8a6f5fe122e7e69a202453fa77c41f1e382505 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 14:53:23 +0100 Subject: [PATCH 05/53] Prod/Test: created Character, setCharacterHealthPoints and test_setCharacterHealthPoints --- src/c/character.c | 10 ++++++++++ src/c/character.h | 12 ++++++++++++ test/c/test_character.c | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/c/character.c create mode 100644 src/c/character.h create mode 100644 test/c/test_character.c diff --git a/src/c/character.c b/src/c/character.c new file mode 100644 index 0000000..5e9d001 --- /dev/null +++ b/src/c/character.c @@ -0,0 +1,10 @@ +#include +#include +#include + +#include "character.h" +Character character; + +void setCharacterHealthPoints (Character *character, int newHealthPoints){ + character->healthPoints = newHealthPoints; +} \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h new file mode 100644 index 0000000..be8bf95 --- /dev/null +++ b/src/c/character.h @@ -0,0 +1,12 @@ +#ifndef CHARACTER_H +#define CHARACTER_H + +typedef struct { + int strength,dexterity,intelligence,healthPoints,manaPoints; + char name [50]; +} Character; + +void setCharacterHealthPoints (Character *character,int newHealthPoints); + + +#endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c new file mode 100644 index 0000000..d4aca2f --- /dev/null +++ b/test/c/test_character.c @@ -0,0 +1,22 @@ +#ifdef TEST +#include "unity.h" +#include "character.h" +Character testCharacter; + +void setUp(void) +{ + testCharacter.healthPoints = 0; +} + +void tearDown(void) +{ +} + +void test_setCharacterHealthPoints(void) +{ + TEST_ASSERT_EQUAL_INT(0,testCharacter.healthPoints); + setCharacterHealthPoints(&testCharacter,50); + TEST_ASSERT_EQUAL_INT(50,testCharacter.healthPoints); +} + +#endif // TEST \ No newline at end of file From 4c638d6af2f31cd3520597e0d7be3432372171c1 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 15:05:48 +0100 Subject: [PATCH 06/53] prod/test: created getCharacterHealthPoints and test_getCharacterHealthPoints --- src/c/character.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index 5e9d001..3da86cd 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -7,4 +7,8 @@ Character character; void setCharacterHealthPoints (Character *character, int newHealthPoints){ character->healthPoints = newHealthPoints; +} + +int getCharacterHealthPoints (Character *character) { + return character->healthPoints; } \ No newline at end of file From b13d7494bbefbfbead968971341b5917cd973cd4 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 18:36:34 +0100 Subject: [PATCH 07/53] prod/test: created setCharacterStrength and test_setCharacterStrength --- src/c/character.c | 7 ++++++- src/c/character.h | 5 +++++ test/c/test_character.c | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 3da86cd..1664eea 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -11,4 +11,9 @@ void setCharacterHealthPoints (Character *character, int newHealthPoints){ int getCharacterHealthPoints (Character *character) { return character->healthPoints; -} \ No newline at end of file +} + +void setCharacterStrength(Character *character, int newStrength) +{ + character->strength = newStrength; +} diff --git a/src/c/character.h b/src/c/character.h index be8bf95..999692c 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -8,5 +8,10 @@ typedef struct { void setCharacterHealthPoints (Character *character,int newHealthPoints); +int getCharacterHealthPoints (Character *character); + +void setCharacterStrength (Character *character, int newStrength); + + #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index d4aca2f..3e26839 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -2,10 +2,14 @@ #include "unity.h" #include "character.h" Character testCharacter; +Character testCharacter2; void setUp(void) { - testCharacter.healthPoints = 0; + testCharacter.healthPoints = 0; + testCharacter2.healthPoints = 15; + testCharacter.strength = 0; + testCharacter2.strength = 5; } void tearDown(void) @@ -19,4 +23,16 @@ void test_setCharacterHealthPoints(void) TEST_ASSERT_EQUAL_INT(50,testCharacter.healthPoints); } +void test_getCharacterHealthPoints (void) { + TEST_ASSERT_EQUAL_INT(15,testCharacter2.healthPoints); +} +void test_setCharacterStrenght(void) +{ + TEST_ASSERT_EQUAL_INT(0,testCharacter.strength); + setCharacterStrength(&testCharacter,50); + TEST_ASSERT_EQUAL_INT(50,testCharacter.strength); +} + + + #endif // TEST \ No newline at end of file From 3d4a9e9f4dcc1711d952a7a7eb76512acef464f5 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 19:02:46 +0100 Subject: [PATCH 08/53] prod/test: created getCharacterStrength and test_getCharacterStrength --- src/c/character.c | 4 ++++ src/c/character.h | 2 +- test/c/test_character.c | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 1664eea..9d3517e 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -17,3 +17,7 @@ void setCharacterStrength(Character *character, int newStrength) { character->strength = newStrength; } + +int getCharacterStrength(Character *character) { + return character->strength; +} diff --git a/src/c/character.h b/src/c/character.h index 999692c..2396bc7 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -12,6 +12,6 @@ int getCharacterHealthPoints (Character *character); void setCharacterStrength (Character *character, int newStrength); - +int getCharacterStrength(Character *character) #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 3e26839..d4012d2 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -33,6 +33,8 @@ void test_setCharacterStrenght(void) TEST_ASSERT_EQUAL_INT(50,testCharacter.strength); } - +void test_getCharacterStrength(void) { + TEST_ASSERT_EQUAL_INT(5,testCharacter2.strength); +} #endif // TEST \ No newline at end of file From 1d083d7d5635e4030fca7e89588e956d6988b711 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 19:11:42 +0100 Subject: [PATCH 09/53] prod/test: created setCharacterDexterity and test_setCharacterDexterity --- src/c/character.c | 9 ++++++--- src/c/character.h | 4 +++- test/c/test_character.c | 11 +++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 9d3517e..b0ff012 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -13,11 +13,14 @@ int getCharacterHealthPoints (Character *character) { return character->healthPoints; } -void setCharacterStrength(Character *character, int newStrength) -{ +void setCharacterStrength (Character *character, int newStrength) { character->strength = newStrength; } -int getCharacterStrength(Character *character) { +int getCharacterStrength (Character *character) { return character->strength; } + +void setCharacterDexterity (Character *character, int newDexterity) { + character->dexterity = newDexterity; +} \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index 2396bc7..55ce4eb 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -12,6 +12,8 @@ int getCharacterHealthPoints (Character *character); void setCharacterStrength (Character *character, int newStrength); -int getCharacterStrength(Character *character) +int getCharacterStrength(Character *character); + +void setCharacterDexterity (Character *character, int newDexterity); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index d4012d2..0119f86 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -10,6 +10,8 @@ void setUp(void) testCharacter2.healthPoints = 15; testCharacter.strength = 0; testCharacter2.strength = 5; + testCharacter.dexterity = 0; + testCharacter2.dexterity = 5; } void tearDown(void) @@ -26,8 +28,7 @@ void test_setCharacterHealthPoints(void) void test_getCharacterHealthPoints (void) { TEST_ASSERT_EQUAL_INT(15,testCharacter2.healthPoints); } -void test_setCharacterStrenght(void) -{ +void test_setCharacterStrenght(void) { TEST_ASSERT_EQUAL_INT(0,testCharacter.strength); setCharacterStrength(&testCharacter,50); TEST_ASSERT_EQUAL_INT(50,testCharacter.strength); @@ -37,4 +38,10 @@ void test_getCharacterStrength(void) { TEST_ASSERT_EQUAL_INT(5,testCharacter2.strength); } +void test_setCharacterDexterity(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.dexterity); + setCharacterDexterity(&testCharacter,50); + TEST_ASSERT_EQUAL_INT(50,testCharacter.dexterity); +} + #endif // TEST \ No newline at end of file From 520b94900528428a6aec812ce7da43c011e28021 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 19:15:22 +0100 Subject: [PATCH 10/53] prod/test: created getCharacterDexterity and test_getCharacterDexterity --- src/c/character.c | 4 ++++ src/c/character.h | 2 ++ test/c/test_character.c | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index b0ff012..d9642ca 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -23,4 +23,8 @@ int getCharacterStrength (Character *character) { void setCharacterDexterity (Character *character, int newDexterity) { character->dexterity = newDexterity; +} + +int getCharacterDexterity (Character *character) { + return character->dexterity; } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index 55ce4eb..a0345e3 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -16,4 +16,6 @@ int getCharacterStrength(Character *character); void setCharacterDexterity (Character *character, int newDexterity); +int getCharacterDexterity(Character *character); + #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 0119f86..0f12bcc 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -44,4 +44,9 @@ void test_setCharacterDexterity(void) { TEST_ASSERT_EQUAL_INT(50,testCharacter.dexterity); } + +void test_getCharacterDexterity(void) { + TEST_ASSERT_EQUAL_INT(5,testCharacter2.dexterity); +} + #endif // TEST \ No newline at end of file From df493e8f850084f0de1dee44cfa8a8d1de1ee30e Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 19:22:54 +0100 Subject: [PATCH 11/53] prod/test: created setCharacterIntelligence and test_setCharacterIntelligence --- src/c/character.c | 4 ++++ src/c/character.h | 8 +++++--- test/c/test_character.c | 9 ++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index d9642ca..5938afa 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -27,4 +27,8 @@ void setCharacterDexterity (Character *character, int newDexterity) { int getCharacterDexterity (Character *character) { return character->dexterity; +} + +void setCharacterIntelligence (Character *character, int newIntelligence) { + character->intelligence = newIntelligence; } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index a0345e3..a5960fe 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -6,16 +6,18 @@ typedef struct { char name [50]; } Character; -void setCharacterHealthPoints (Character *character,int newHealthPoints); +void setCharacterHealthPoints (Character *character, int newHealthPoints); int getCharacterHealthPoints (Character *character); void setCharacterStrength (Character *character, int newStrength); -int getCharacterStrength(Character *character); +int getCharacterStrength (Character *character); void setCharacterDexterity (Character *character, int newDexterity); -int getCharacterDexterity(Character *character); +int getCharacterDexterity (Character *character); + +void setCharacterIntelligence (Character *character, int newIntelligence); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 0f12bcc..c204b09 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -12,6 +12,8 @@ void setUp(void) testCharacter2.strength = 5; testCharacter.dexterity = 0; testCharacter2.dexterity = 5; + testCharacter.intelligence = 0; + testCharacter2.intelligence = 5; } void tearDown(void) @@ -44,9 +46,14 @@ void test_setCharacterDexterity(void) { TEST_ASSERT_EQUAL_INT(50,testCharacter.dexterity); } - void test_getCharacterDexterity(void) { TEST_ASSERT_EQUAL_INT(5,testCharacter2.dexterity); } +void test_setCharacterIntelligence(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.intelligence); + setCharacterIntelligence(&testCharacter,45); + TEST_ASSERT_EQUAL_INT(45,testCharacter.intelligence); +} + #endif // TEST \ No newline at end of file From 3dab622ef26e55121601f98bd7e9f83c4050dc8f Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Thu, 12 Jan 2023 19:25:20 +0100 Subject: [PATCH 12/53] prod/test: created getCharacterIntelligence and test_getCharacterIntelligence --- src/c/character.c | 4 ++++ src/c/character.h | 2 ++ test/c/test_character.c | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/c/character.c b/src/c/character.c index 5938afa..d529731 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -31,4 +31,8 @@ int getCharacterDexterity (Character *character) { void setCharacterIntelligence (Character *character, int newIntelligence) { character->intelligence = newIntelligence; +} + +int getCharacterIntelligence (Character *character) { + return character->intelligence; } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index a5960fe..80b6dd1 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -20,4 +20,6 @@ int getCharacterDexterity (Character *character); void setCharacterIntelligence (Character *character, int newIntelligence); +int getCharacterIntelligence (Character *character); + #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index c204b09..e216d43 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -13,7 +13,7 @@ void setUp(void) testCharacter.dexterity = 0; testCharacter2.dexterity = 5; testCharacter.intelligence = 0; - testCharacter2.intelligence = 5; + testCharacter2.intelligence = 7; } void tearDown(void) @@ -56,4 +56,8 @@ void test_setCharacterIntelligence(void) { TEST_ASSERT_EQUAL_INT(45,testCharacter.intelligence); } +void test_getCharacterIntelligence(void) { + TEST_ASSERT_EQUAL_INT(7,testCharacter2.intelligence); +} + #endif // TEST \ No newline at end of file From efc4d59e925a69c5b81748ef1ff8fb066838deaa Mon Sep 17 00:00:00 2001 From: Mac10goesBRRRT Date: Fri, 13 Jan 2023 15:59:02 +0100 Subject: [PATCH 13/53] Enabled gcov options --- project.yml | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/project.yml b/project.yml index e253248..b7f71dd 100644 --- a/project.yml +++ b/project.yml @@ -98,4 +98,127 @@ :enabled: - stdout_pretty_tests_report - module_generator + - xml_tests_report + - gcov + +:gcov: + #:utilities: + # - gcovr + :reports: + # Make an HTML report with line by line coverage of each source file. + # Supported utilities: gcovr, ReportGenerator + - HtmlDetailed + :gcovr: + # HTML report filename. + :html_artifact_filename: my-project name + + # Use 'title' as title for the HTML report. + # Default is 'Head'. (gcovr --html-title) + :html_title: my-project name--- + +# Notes: +# Sample project C code is not presently written to produce a release artifact. +# As such, release build options are disabled. +# This sample, therefore, only demonstrates running a collection of unit tests. + +:project: + :use_exceptions: FALSE + :use_test_preprocessor: TRUE + :use_auxiliary_dependencies: TRUE + :build_root: build +# :release_build: TRUE + :test_file_prefix: test_ + :which_ceedling: gem + :ceedling_version: 0.31.1 + :default_tasks: + - test:all + +#:test_build: +# :use_assembly: TRUE + +#:release_build: +# :output: MyApp.out +# :use_assembly: FALSE + +:environment: + +:extension: + :executable: .out + +:paths: + :test: + - +:test/** + - -:test/support + :source: + - src/** + :support: + - test/support + :libraries: [] + +:defines: + # in order to add common defines: + # 1) remove the trailing [] from the :common: section + # 2) add entries to the :common: section (e.g. :test: has TEST defined) + :common: &common_defines [] + :test: + - *common_defines + - TEST + :test_preprocess: + - *common_defines + - TEST + +:cmock: + :mock_prefix: mock_ + :when_no_prototypes: :warn + :enforce_strict_ordering: TRUE + :plugins: + - :ignore + - :callback + :treat_as: + uint8: HEX8 + uint16: HEX16 + uint32: UINT32 + int8: INT8 + bool: UINT8 + +# Add -gcov to the plugins list to make sure of the gcov plugin +# You will need to have gcov and gcovr both installed to make it work. +# For more information on these options, see docs in plugins/gcov +:gcov: + :utilities: + -gcovr + :reports: + - HtmlDetailed + :gcovr: + :html_artifact_filename: my-project name + :html_title: my-project name + :html_medium_threshold: 75 + :html_high_threshold: 90 + +#:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + +# LIBRARIES +# These libraries are automatically injected into the build process. Those specified as +# common will be used in all types of builds. Otherwise, libraries can be injected in just +# tests or releases. These options are MERGED with the options in supplemental yaml files. +:libraries: + :placement: :end + :flag: "-l${1}" + :path_flag: "-L ${1}" + :system: [] # for example, you might list 'm' to grab the math library + :test: [] + :release: [] + +:plugins: + :load_paths: + - "#{Ceedling.load_path}" + :enabled: + - stdout_pretty_tests_report + - module_generator + - xml_tests_report + - gcov + ... From 7bf2091f257f4b0ebd68bc8a7dcf154b52ab4beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 18:03:32 +0100 Subject: [PATCH 14/53] added armor and needed functions to enemy --- src/c/encounter.c | 19 +++++++++++++++++-- src/c/encounter.h | 5 +++++ test/c/test_encounter.c | 8 +++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index d6b8710..bc6887f 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -4,6 +4,12 @@ #include #include +/*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein +evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt +gegner erhalten eine zufällige menge Gold, die beim tod an den spieler gegeben werden +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){ if(health > 0){ @@ -23,12 +29,12 @@ int playerHealth(int health, int damage){ return health; } -enemy createEnemy(int health) +/* enemy createEnemy(int health) { enemy test; test.health = health; return test; -} +} */ void *setEnemyHealth(int *num, int health){ *num = health; @@ -47,3 +53,12 @@ void *setEnemyDamage(int *num, int damage){ int getEnemyDamage(enemy enemy){ return enemy.damage; } + +void *setEnemyArmor(int *num, int armor){ + *num = armor; +} + +int getEnemyArmor(enemy enemy){ + return enemy.armor; +} + diff --git a/src/c/encounter.h b/src/c/encounter.h index 6fbedfe..182aad3 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -6,6 +6,7 @@ typedef struct { int health; int damage; + int armor; } enemy; @@ -18,5 +19,9 @@ int getEnemyHealth(enemy enemy); //setEnemyDamage(&enemy.damage, damage); void *setEnemyDamage(int *num, int damage); int getEnemyDamage(enemy enemy); +//setEnemyArmor(&enemy.armor, armor); +void *setEnemyArmor(int *num, int damage); +int getEnemyArmor(enemy enemy); + #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index cc57da1..2954ffa 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -77,5 +77,11 @@ void test_getEnemyDamage(void){ TEST_ASSERT_EQUAL(damage, result); } - +void test_get_setEnemyArmor(void){ + int armor = 4, result; + enemy test = {50, 4, armor*2}; + setEnemyArmor(&test.armor, armor); + result = getEnemyArmor(test); + TEST_ASSERT_EQUAL(armor, result); +} #endif // TEST From 5cf386590b304a6b77fed9edec6ad1e61fbd3ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 19:22:31 +0100 Subject: [PATCH 15/53] Added a Function to apply Damage to the enemy --- src/c/encounter.c | 6 ++++++ src/c/encounter.h | 2 ++ test/c/test_encounter.c | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index bc6887f..955736e 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -62,3 +62,9 @@ int getEnemyArmor(enemy enemy){ return enemy.armor; } +int enemyDamaged(enemy enemy, int damage){ + int armor = getEnemyArmor(enemy); + int damagedealt = damage - armor; + enemy.health = enemy.health - damagedealt; + return enemy.health; +} \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index 182aad3..d2b0fba 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -23,5 +23,7 @@ int getEnemyDamage(enemy enemy); void *setEnemyArmor(int *num, int damage); int getEnemyArmor(enemy enemy); +int enemyDamaged(enemy enemy, int damage); + #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 2954ffa..8bef335 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -84,4 +84,21 @@ void test_get_setEnemyArmor(void){ result = getEnemyArmor(test); TEST_ASSERT_EQUAL(armor, result); } + +void test_PlayerAttacksEnemy_DoesDamage(void) +{ + //arrange + int playerDamage = 10; + int enemyHealth = 50; + int enemyArmor = 4; + //health - (damage - armor) + int expectedEnemyHealth = 44; + //act + enemy test = {enemyHealth, 4, enemyArmor}; + test.health = enemyDamaged(test, playerDamage); + //assert + TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); +} + + #endif // TEST From e318727370cbcc99a84812dfe69f1ccc2c7643eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 19:30:44 +0100 Subject: [PATCH 16/53] added a way to not apply negative damage/ only min --- src/c/encounter.c | 3 +++ test/c/test_encounter.c | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 955736e..fe37c67 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -65,6 +65,9 @@ int getEnemyArmor(enemy enemy){ int enemyDamaged(enemy enemy, int damage){ int armor = getEnemyArmor(enemy); int damagedealt = damage - armor; + if(damagedealt < 1){ + damagedealt = 1; + } enemy.health = enemy.health - damagedealt; return enemy.health; } \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 8bef335..1f501ae 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -100,5 +100,18 @@ void test_PlayerAttacksEnemy_DoesDamage(void) TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); } - +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}; + test.health = enemyDamaged(test, playerDamage); + //assert + TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); +} #endif // TEST From 608e76dbe475e62b92c9c516d78ea4edd0a35392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 20:23:34 +0100 Subject: [PATCH 17/53] Added AttackValues and A better getter function --- src/c/encounter.c | 32 ++++++++++++++++---------------- src/c/encounter.h | 11 ++++++++--- test/c/test_encounter.c | 15 +++++++++++++-- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index fe37c67..29678c8 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -3,7 +3,7 @@ #include #include -#include +#include "encounter.h" /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt gegner erhalten eine zufällige menge Gold, die beim tod an den spieler gegeben werden @@ -29,13 +29,19 @@ int playerHealth(int health, int damage){ return health; } -/* enemy createEnemy(int health) -{ - enemy test; - test.health = health; - return test; -} */ +int enemyDamaged(enemy enemy, int damage){ + int armor = getEnemyArmor(enemy); + int damagedealt = damage - armor; + if(damagedealt < 1){ + damagedealt = 1; + } + enemy.health = enemy.health - damagedealt; + return enemy.health; +} + + +//Getter/Setter Funktionen void *setEnemyHealth(int *num, int health){ *num = health; //return health; @@ -62,12 +68,6 @@ int getEnemyArmor(enemy enemy){ return enemy.armor; } -int enemyDamaged(enemy enemy, int damage){ - int armor = getEnemyArmor(enemy); - int damagedealt = damage - armor; - if(damagedealt < 1){ - damagedealt = 1; - } - enemy.health = enemy.health - damagedealt; - return enemy.health; -} \ No newline at end of file +int getEnemyInt(int *structParam){ + return *structParam; +} diff --git a/src/c/encounter.h b/src/c/encounter.h index d2b0fba..0beafea 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -7,11 +7,17 @@ typedef struct { int health; int damage; int armor; + int attack; } enemy; bool playerAlive(int health); int playerHealth(int health, int damage); +int enemyDamaged(enemy enemy, int damage); + + + +//Getter/Setter Funktionen //setEnemyHealth(&enemy.health, health); void *setEnemyHealth(int *num, int health); @@ -22,8 +28,7 @@ int getEnemyDamage(enemy enemy); //setEnemyArmor(&enemy.armor, armor); void *setEnemyArmor(int *num, int damage); int getEnemyArmor(enemy enemy); - -int enemyDamaged(enemy enemy, int damage); - +//Function to get the value of Data in a struct, needs a pointer to the struct +int getEnemyInt(int *structParam); #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 1f501ae..e68f866 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -56,11 +56,12 @@ void test_setEnemyHealth(void){ void test_getEnemyHealth(void){ //arrange - int health = 50; + int health = 50, result; //act enemy test = {health}; + result = getEnemyHealth(test); //assert - TEST_ASSERT_EQUAL(health, getEnemyHealth(test)); + TEST_ASSERT_EQUAL(health, result); } void test_setEnemyDamage(void){ @@ -114,4 +115,14 @@ void test_PlayerAttacksEnemy_DoesMinDamage(void) //assert TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); } + +//A better way to get the value of a struct, REFACTORING if it works +void test_getterWithParams(void){ + int health = 50, armor = 4, damage = 4, attack = 5; + enemy test = {health, damage, armor, attack}; + TEST_ASSERT_EQUAL(health, getEnemyInt(&test.health)); + TEST_ASSERT_EQUAL(armor, getEnemyInt(&test.armor)); + TEST_ASSERT_EQUAL(damage, getEnemyInt(&test.damage)); + TEST_ASSERT_EQUAL(attack, getEnemyInt(&test.attack)); +} #endif // TEST From 133c4fe8c3c4d11e91532f5b3a6d850944e7c336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 20:42:56 +0100 Subject: [PATCH 18/53] refactoring: replaced getter func w universal func --- src/c/encounter.c | 15 +-------------- src/c/encounter.h | 4 +--- test/c/test_encounter.c | 6 +++--- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 29678c8..5ee3f85 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -30,7 +30,7 @@ int playerHealth(int health, int damage){ } int enemyDamaged(enemy enemy, int damage){ - int armor = getEnemyArmor(enemy); + int armor = getEnemyInt(&enemy.armor); int damagedealt = damage - armor; if(damagedealt < 1){ damagedealt = 1; @@ -47,27 +47,14 @@ void *setEnemyHealth(int *num, int health){ //return health; } -int getEnemyHealth(enemy enemy){ - //gibt health zurück, benötigt keine adress pointer - return enemy.health; -} - void *setEnemyDamage(int *num, int damage){ *num = damage; } -int getEnemyDamage(enemy enemy){ - return enemy.damage; -} - void *setEnemyArmor(int *num, int armor){ *num = armor; } -int getEnemyArmor(enemy enemy){ - return enemy.armor; -} - int getEnemyInt(int *structParam){ return *structParam; } diff --git a/src/c/encounter.h b/src/c/encounter.h index 0beafea..d6ddb68 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -21,13 +21,11 @@ int enemyDamaged(enemy enemy, int damage); //setEnemyHealth(&enemy.health, health); void *setEnemyHealth(int *num, int health); -int getEnemyHealth(enemy enemy); + //setEnemyDamage(&enemy.damage, damage); void *setEnemyDamage(int *num, int damage); -int getEnemyDamage(enemy enemy); //setEnemyArmor(&enemy.armor, armor); void *setEnemyArmor(int *num, int damage); -int getEnemyArmor(enemy enemy); //Function to get the value of Data in a struct, needs a pointer to the struct int getEnemyInt(int *structParam); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index e68f866..f6782dd 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -59,7 +59,7 @@ void test_getEnemyHealth(void){ int health = 50, result; //act enemy test = {health}; - result = getEnemyHealth(test); + result = getEnemyInt(&test.health); //assert TEST_ASSERT_EQUAL(health, result); } @@ -74,7 +74,7 @@ void test_setEnemyDamage(void){ void test_getEnemyDamage(void){ int damage = 4, result; enemy test = {50, damage}; - result = getEnemyDamage(test); + result = getEnemyInt(&test.damage); TEST_ASSERT_EQUAL(damage, result); } @@ -82,7 +82,7 @@ void test_get_setEnemyArmor(void){ int armor = 4, result; enemy test = {50, 4, armor*2}; setEnemyArmor(&test.armor, armor); - result = getEnemyArmor(test); + result = getEnemyInt(&test.armor); TEST_ASSERT_EQUAL(armor, result); } From 6ed2513a3c33b5c0cc666430c57dab93e134a8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 13 Jan 2023 21:22:19 +0100 Subject: [PATCH 19/53] enabled govc for better test visualization --- project.yml | 120 ---------------------------------------------------- 1 file changed, 120 deletions(-) diff --git a/project.yml b/project.yml index b7f71dd..b3f24b9 100644 --- a/project.yml +++ b/project.yml @@ -101,124 +101,4 @@ - xml_tests_report - gcov -:gcov: - #:utilities: - # - gcovr - :reports: - # Make an HTML report with line by line coverage of each source file. - # Supported utilities: gcovr, ReportGenerator - - HtmlDetailed - :gcovr: - # HTML report filename. - :html_artifact_filename: my-project name - - # Use 'title' as title for the HTML report. - # Default is 'Head'. (gcovr --html-title) - :html_title: my-project name--- - -# Notes: -# Sample project C code is not presently written to produce a release artifact. -# As such, release build options are disabled. -# This sample, therefore, only demonstrates running a collection of unit tests. - -:project: - :use_exceptions: FALSE - :use_test_preprocessor: TRUE - :use_auxiliary_dependencies: TRUE - :build_root: build -# :release_build: TRUE - :test_file_prefix: test_ - :which_ceedling: gem - :ceedling_version: 0.31.1 - :default_tasks: - - test:all - -#:test_build: -# :use_assembly: TRUE - -#:release_build: -# :output: MyApp.out -# :use_assembly: FALSE - -:environment: - -:extension: - :executable: .out - -:paths: - :test: - - +:test/** - - -:test/support - :source: - - src/** - :support: - - test/support - :libraries: [] - -:defines: - # in order to add common defines: - # 1) remove the trailing [] from the :common: section - # 2) add entries to the :common: section (e.g. :test: has TEST defined) - :common: &common_defines [] - :test: - - *common_defines - - TEST - :test_preprocess: - - *common_defines - - TEST - -:cmock: - :mock_prefix: mock_ - :when_no_prototypes: :warn - :enforce_strict_ordering: TRUE - :plugins: - - :ignore - - :callback - :treat_as: - uint8: HEX8 - uint16: HEX16 - uint32: UINT32 - int8: INT8 - bool: UINT8 - -# Add -gcov to the plugins list to make sure of the gcov plugin -# You will need to have gcov and gcovr both installed to make it work. -# For more information on these options, see docs in plugins/gcov -:gcov: - :utilities: - -gcovr - :reports: - - HtmlDetailed - :gcovr: - :html_artifact_filename: my-project name - :html_title: my-project name - :html_medium_threshold: 75 - :html_high_threshold: 90 - -#:tools: -# Ceedling defaults to using gcc for compiling, linking, etc. -# As [:tools] is blank, gcc will be used (so long as it's in your system path) -# See documentation to configure a given toolchain for use - -# LIBRARIES -# These libraries are automatically injected into the build process. Those specified as -# common will be used in all types of builds. Otherwise, libraries can be injected in just -# tests or releases. These options are MERGED with the options in supplemental yaml files. -:libraries: - :placement: :end - :flag: "-l${1}" - :path_flag: "-L ${1}" - :system: [] # for example, you might list 'm' to grab the math library - :test: [] - :release: [] - -:plugins: - :load_paths: - - "#{Ceedling.load_path}" - :enabled: - - stdout_pretty_tests_report - - module_generator - - xml_tests_report - - gcov - ... From 129fb04dcb5a7177b879494477584a57c6944e32 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 17:14:43 +0100 Subject: [PATCH 20/53] prod/test: created test_addCharacterHealthPoints and addCharacterHealthPoints --- src/c/character.c | 4 ++++ src/c/character.h | 2 ++ test/c/test_character.c | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index d529731..2409dab 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -13,6 +13,10 @@ int getCharacterHealthPoints (Character *character) { return character->healthPoints; } +void addCharacterHealthPoints (Character *character, int addedHealthPoints) { + character->healthPoints += addedHealthPoints; +} + void setCharacterStrength (Character *character, int newStrength) { character->strength = newStrength; } diff --git a/src/c/character.h b/src/c/character.h index 80b6dd1..0c18944 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -10,6 +10,8 @@ void setCharacterHealthPoints (Character *character, int newHealthPoints); int getCharacterHealthPoints (Character *character); +void addCharacterHealthPoints (Character *character, int addedHealthPoints); + void setCharacterStrength (Character *character, int newStrength); int getCharacterStrength (Character *character); diff --git a/test/c/test_character.c b/test/c/test_character.c index e216d43..71b43ec 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -30,6 +30,13 @@ void test_setCharacterHealthPoints(void) void test_getCharacterHealthPoints (void) { TEST_ASSERT_EQUAL_INT(15,testCharacter2.healthPoints); } + +void test_addCharacterHealthPoints (void) { + addCharacterHealthPoints(&testCharacter2,5); + TEST_ASSERT_EQUAL_INT(20,testCharacter2.healthPoints); + +} + void test_setCharacterStrenght(void) { TEST_ASSERT_EQUAL_INT(0,testCharacter.strength); setCharacterStrength(&testCharacter,50); From 9cefe4afedc5e47bb79bf49182bdf425b5e964d9 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 17:20:54 +0100 Subject: [PATCH 21/53] prod/test: created test_addCharacterStrength and addCharacterStrength --- src/c/character.c | 4 ++++ src/c/character.h | 2 ++ test/c/test_character.c | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index 2409dab..c3010be 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -25,6 +25,10 @@ int getCharacterStrength (Character *character) { return character->strength; } +void addCharacterStrength (Character *character, int addStrength) { + character->strength += addStrength; +} + void setCharacterDexterity (Character *character, int newDexterity) { character->dexterity = newDexterity; } diff --git a/src/c/character.h b/src/c/character.h index 0c18944..5caca85 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -16,6 +16,8 @@ void setCharacterStrength (Character *character, int newStrength); int getCharacterStrength (Character *character); +void addCharacterStrength (Character *character, int addStrength); + void setCharacterDexterity (Character *character, int newDexterity); int getCharacterDexterity (Character *character); diff --git a/test/c/test_character.c b/test/c/test_character.c index 71b43ec..d948c3c 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -47,6 +47,11 @@ void test_getCharacterStrength(void) { TEST_ASSERT_EQUAL_INT(5,testCharacter2.strength); } +void test_addCharacterStrength (void) { + addCharacterStrength(&testCharacter2,15); + TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); +} + void test_setCharacterDexterity(void) { TEST_ASSERT_EQUAL_INT(0,testCharacter.dexterity); setCharacterDexterity(&testCharacter,50); From 4ac4f2b6d80352226b60b6fe2b4d5cbf1a6ff9d5 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 17:55:34 +0100 Subject: [PATCH 22/53] refactoring: prod combined addCharacterHealthPoints and addCharacterStrength to increaseStat function also --- src/c/character.c | 28 ++++++++++++++++++++-------- src/c/character.h | 10 ++++++---- test/c/test_character.c | 7 +++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index c3010be..40051d6 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -3,6 +3,7 @@ #include #include "character.h" + Character character; void setCharacterHealthPoints (Character *character, int newHealthPoints){ @@ -13,10 +14,6 @@ int getCharacterHealthPoints (Character *character) { return character->healthPoints; } -void addCharacterHealthPoints (Character *character, int addedHealthPoints) { - character->healthPoints += addedHealthPoints; -} - void setCharacterStrength (Character *character, int newStrength) { character->strength = newStrength; } @@ -25,10 +22,6 @@ int getCharacterStrength (Character *character) { return character->strength; } -void addCharacterStrength (Character *character, int addStrength) { - character->strength += addStrength; -} - void setCharacterDexterity (Character *character, int newDexterity) { character->dexterity = newDexterity; } @@ -43,4 +36,23 @@ void setCharacterIntelligence (Character *character, int newIntelligence) { int getCharacterIntelligence (Character *character) { return character->intelligence; +} + + +void increaseStat (Character *character, int Stat, int amount) { + switch (Stat) + { + case 1: + character->strength += amount;break; + case 2: + character->dexterity += amount;break; + case 3: + character->intelligence += amount;break; + case 4: + character->healthPoints += amount;break; + case 5: + character->manaPoints += amount;break; + default: + break; + } } \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index 5caca85..02b4630 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -6,18 +6,18 @@ typedef struct { char name [50]; } Character; +enum { + STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5 +}; + void setCharacterHealthPoints (Character *character, int newHealthPoints); int getCharacterHealthPoints (Character *character); -void addCharacterHealthPoints (Character *character, int addedHealthPoints); - void setCharacterStrength (Character *character, int newStrength); int getCharacterStrength (Character *character); -void addCharacterStrength (Character *character, int addStrength); - void setCharacterDexterity (Character *character, int newDexterity); int getCharacterDexterity (Character *character); @@ -26,4 +26,6 @@ void setCharacterIntelligence (Character *character, int newIntelligence); int getCharacterIntelligence (Character *character); +void increaseStat (Character *character, int Stat, int amount); + #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index d948c3c..083b842 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -32,9 +32,8 @@ void test_getCharacterHealthPoints (void) { } void test_addCharacterHealthPoints (void) { - addCharacterHealthPoints(&testCharacter2,5); + increaseStat(&testCharacter2,HEALTHPOINTS,5); TEST_ASSERT_EQUAL_INT(20,testCharacter2.healthPoints); - } void test_setCharacterStrenght(void) { @@ -48,8 +47,8 @@ void test_getCharacterStrength(void) { } void test_addCharacterStrength (void) { - addCharacterStrength(&testCharacter2,15); - TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); + increaseStat(&testCharacter2,STRENGTH,5); + TEST_ASSERT_EQUAL_INT(10,testCharacter2.strength); } void test_setCharacterDexterity(void) { From 62b522558c546ea9c9533214082e27f3c6aeb71b Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 17:59:03 +0100 Subject: [PATCH 23/53] refactoring: test combined the test_addCharacterHealthpoints and test_addCharacterStrength Unittests --- test/c/test_character.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/c/test_character.c b/test/c/test_character.c index 083b842..20d175c 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -31,11 +31,6 @@ void test_getCharacterHealthPoints (void) { TEST_ASSERT_EQUAL_INT(15,testCharacter2.healthPoints); } -void test_addCharacterHealthPoints (void) { - increaseStat(&testCharacter2,HEALTHPOINTS,5); - TEST_ASSERT_EQUAL_INT(20,testCharacter2.healthPoints); -} - void test_setCharacterStrenght(void) { TEST_ASSERT_EQUAL_INT(0,testCharacter.strength); setCharacterStrength(&testCharacter,50); @@ -46,11 +41,6 @@ void test_getCharacterStrength(void) { TEST_ASSERT_EQUAL_INT(5,testCharacter2.strength); } -void test_addCharacterStrength (void) { - increaseStat(&testCharacter2,STRENGTH,5); - TEST_ASSERT_EQUAL_INT(10,testCharacter2.strength); -} - void test_setCharacterDexterity(void) { TEST_ASSERT_EQUAL_INT(0,testCharacter.dexterity); setCharacterDexterity(&testCharacter,50); @@ -71,4 +61,11 @@ void test_getCharacterIntelligence(void) { TEST_ASSERT_EQUAL_INT(7,testCharacter2.intelligence); } +void test_increaseStat(void) { + increaseStat(&testCharacter2,STRENGTH,15); + TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); + increaseStat(&testCharacter2,INTELLIGENCE,15); + TEST_ASSERT_EQUAL_INT(22,testCharacter2.intelligence); +} + #endif // TEST \ No newline at end of file From 25432ede8bcaf80dc2e22b29c1bfcf8dd56f9410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Sat, 14 Jan 2023 18:12:39 +0100 Subject: [PATCH 24/53] Added TurnSwitch and More Tests for all paths --- .gitignore | 3 ++- src/c/encounter.c | 7 +++++++ src/c/encounter.h | 3 ++- test/c/test_encounter.c | 24 +++++++++++++++++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 7893b49..9ee5328 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build/gcov builld/logs build/temp build/test -.vscode \ No newline at end of file +.vscode +encounter_test.sh \ No newline at end of file diff --git a/src/c/encounter.c b/src/c/encounter.c index 5ee3f85..2956c58 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -4,6 +4,7 @@ #include #include "encounter.h" + /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt gegner erhalten eine zufällige menge Gold, die beim tod an den spieler gegeben werden @@ -58,3 +59,9 @@ void *setEnemyArmor(int *num, int armor){ int getEnemyInt(int *structParam){ return *structParam; } + + +int switchTurns(int currentTurn){ + currentTurn = currentTurn % 2 + 1; + return currentTurn; +} \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index d6ddb68..185688d 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -14,7 +14,8 @@ typedef struct { bool playerAlive(int health); int playerHealth(int health, int damage); int enemyDamaged(enemy enemy, int damage); - +int switchTurns(int currentTurn); +int charInputToIntReturn(); //Getter/Setter Funktionen diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index f6782dd..4641b76 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -4,6 +4,7 @@ #include "encounter.h" + void setUp(void){ } @@ -21,6 +22,15 @@ void test_isPlayerAlive_healthGreaterZero(void) TEST_ASSERT_TRUE(playerAlive(health)); } +void test_isPlayerAlive_healthLowerZero(void) +{ + //arrange + int health = -1; + //act + //assert + TEST_ASSERT_TRUE(playerAlive(health)); +} + void test_playerIsDamaged(void) { //arrange @@ -117,7 +127,8 @@ void test_PlayerAttacksEnemy_DoesMinDamage(void) } //A better way to get the value of a struct, REFACTORING if it works -void test_getterWithParams(void){ +void test_getterWithParams(void) +{ int health = 50, armor = 4, damage = 4, attack = 5; enemy test = {health, damage, armor, attack}; TEST_ASSERT_EQUAL(health, getEnemyInt(&test.health)); @@ -125,4 +136,15 @@ void test_getterWithParams(void){ TEST_ASSERT_EQUAL(damage, getEnemyInt(&test.damage)); TEST_ASSERT_EQUAL(attack, getEnemyInt(&test.attack)); } +void test_switchingTurns(void) +{ + //arrange + int currentTurn = 0, result; + //act + result = switchTurns(currentTurn); + //assert + TEST_ASSERT_EQUAL(1,result); +} + + #endif // TEST From 788b458687faf6499e98fb628b520a62ba6ec371 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 18:24:02 +0100 Subject: [PATCH 25/53] prod/test: added Level stat and created test_setCharacterLevel and setCharacterLevel --- src/c/character.c | 10 +++++++--- src/c/character.h | 7 ++++--- test/c/test_character.c | 8 ++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 40051d6..1b12e2c 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -38,9 +38,13 @@ int getCharacterIntelligence (Character *character) { return character->intelligence; } +void setCharacterLevel (Character *character, int newLevel) { + character->level = newLevel; +} + -void increaseStat (Character *character, int Stat, int amount) { - switch (Stat) +void increaseStat (Character *character, int stat, int amount) { + switch (stat) { case 1: character->strength += amount;break; @@ -55,4 +59,4 @@ void increaseStat (Character *character, int Stat, int amount) { default: break; } -} \ No newline at end of file +} diff --git a/src/c/character.h b/src/c/character.h index 02b4630..411a1e3 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -2,12 +2,12 @@ #define CHARACTER_H typedef struct { - int strength,dexterity,intelligence,healthPoints,manaPoints; + int strength,dexterity,intelligence,healthPoints,manaPoints,level; char name [50]; } Character; enum { - STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5 + STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6 }; void setCharacterHealthPoints (Character *character, int newHealthPoints); @@ -26,6 +26,7 @@ void setCharacterIntelligence (Character *character, int newIntelligence); int getCharacterIntelligence (Character *character); -void increaseStat (Character *character, int Stat, int amount); +void setCharacterLevel (Character *character, int newLevel); +void increaseStat (Character *character, int stat, int amount); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 20d175c..8dff325 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -14,6 +14,8 @@ void setUp(void) testCharacter2.dexterity = 5; testCharacter.intelligence = 0; testCharacter2.intelligence = 7; + testCharacter.level = 0; + testCharacter2.level = 5; } void tearDown(void) @@ -61,6 +63,12 @@ void test_getCharacterIntelligence(void) { TEST_ASSERT_EQUAL_INT(7,testCharacter2.intelligence); } +void test_setCharacterLevel(void) { + TEST_ASSERT_EQUAL_INT(0,testCharacter.level); + setCharacterLevel(&testCharacter,1); + TEST_ASSERT_EQUAL_INT(1,testCharacter.level); +} + void test_increaseStat(void) { increaseStat(&testCharacter2,STRENGTH,15); TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); From b2232479decc409718a189576f711b0a99d7683f Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 18:27:40 +0100 Subject: [PATCH 26/53] prod/test: created getCharacterLevel and test_getCharacterLevel --- src/c/character.c | 4 ++++ src/c/character.h | 2 ++ test/c/test_character.c | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index 1b12e2c..2229b51 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -42,6 +42,10 @@ void setCharacterLevel (Character *character, int newLevel) { character->level = newLevel; } +int getCharacterLevel (Character *character) { + return character->level; +} + void increaseStat (Character *character, int stat, int amount) { switch (stat) diff --git a/src/c/character.h b/src/c/character.h index 411a1e3..6e53350 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -28,5 +28,7 @@ int getCharacterIntelligence (Character *character); void setCharacterLevel (Character *character, int newLevel); +int getCharacterLevel (Character *character); + void increaseStat (Character *character, int stat, int amount); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 8dff325..44fdd1c 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -69,6 +69,10 @@ void test_setCharacterLevel(void) { TEST_ASSERT_EQUAL_INT(1,testCharacter.level); } +void test_getCharacterLevel(void) { + TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); +} + void test_increaseStat(void) { increaseStat(&testCharacter2,STRENGTH,15); TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); From 26578ac18c18a8002d0dc255ba2037b471fd9bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Sat, 14 Jan 2023 18:34:50 +0100 Subject: [PATCH 27/53] Added Test for all paths and switchPlayer Function --- .gitignore | 3 ++- src/c/encounter.c | 5 +++++ src/c/encounter.h | 2 +- test/c/test_encounter.c | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7893b49..9ee5328 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build/gcov builld/logs build/temp build/test -.vscode \ No newline at end of file +.vscode +encounter_test.sh \ No newline at end of file diff --git a/src/c/encounter.c b/src/c/encounter.c index 5ee3f85..e6d6c88 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -58,3 +58,8 @@ void *setEnemyArmor(int *num, int armor){ int getEnemyInt(int *structParam){ return *structParam; } + +int switchTurns(int currentTurn){ + currentTurn = currentTurn % 2 + 1; + return currentTurn; +} diff --git a/src/c/encounter.h b/src/c/encounter.h index d6ddb68..1e5ea08 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -14,7 +14,7 @@ typedef struct { bool playerAlive(int health); int playerHealth(int health, int damage); int enemyDamaged(enemy enemy, int damage); - +int switchTurns(int currentTurn); //Getter/Setter Funktionen diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index f6782dd..dde9b12 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -21,6 +21,16 @@ void test_isPlayerAlive_healthGreaterZero(void) TEST_ASSERT_TRUE(playerAlive(health)); } + +void test_isPlayerAlive_healthLowerZero(void) +{ + //arrange + int health = -1; + //act + //assert + TEST_ASSERT_FALSE(playerAlive(health)); +} + void test_playerIsDamaged(void) { //arrange @@ -125,4 +135,14 @@ void test_getterWithParams(void){ TEST_ASSERT_EQUAL(damage, getEnemyInt(&test.damage)); TEST_ASSERT_EQUAL(attack, getEnemyInt(&test.attack)); } + +void test_switchingTurns(void) +{ + //arrange + int currentTurn = 0, result; + //act + result = switchTurns(currentTurn); + //assert + TEST_ASSERT_EQUAL(1,result); +} #endif // TEST From 98ea061df8f5ce703239effe313583ba0c72995e Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 18:40:15 +0100 Subject: [PATCH 28/53] prod/test: created test_calculateStatIncrease and calculateStatIncrease --- src/c/character.c | 4 ++++ src/c/character.h | 2 ++ test/c/test_character.c | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/src/c/character.c b/src/c/character.c index 2229b51..cb8f3b8 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -46,6 +46,10 @@ int getCharacterLevel (Character *character) { return character->level; } +int calculateStatIncrease (Character *character, int amount) { + return character->level*amount; +} + void increaseStat (Character *character, int stat, int amount) { switch (stat) diff --git a/src/c/character.h b/src/c/character.h index 6e53350..633d3e1 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -31,4 +31,6 @@ void setCharacterLevel (Character *character, int newLevel); int getCharacterLevel (Character *character); void increaseStat (Character *character, int stat, int amount); + +int calculateStatIncrease (Character *character, int amount); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index 44fdd1c..f6c9f75 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -80,4 +80,8 @@ void test_increaseStat(void) { TEST_ASSERT_EQUAL_INT(22,testCharacter2.intelligence); } +void test_calculateStatIncrease(void) { + TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); + TEST_ASSERT_EQUAL_INT(25,calculateStatIncrease(&testCharacter2,5)); +} #endif // TEST \ No newline at end of file From a0c3371d454233f0097ff035b5e609d782a15e77 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 14 Jan 2023 18:43:23 +0100 Subject: [PATCH 29/53] test: updated test_get* Unittests as i had an error and didn't actually tested the correct value --- test/c/test_character.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/c/test_character.c b/test/c/test_character.c index f6c9f75..ad72b33 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -30,7 +30,7 @@ void test_setCharacterHealthPoints(void) } void test_getCharacterHealthPoints (void) { - TEST_ASSERT_EQUAL_INT(15,testCharacter2.healthPoints); + TEST_ASSERT_EQUAL_INT(15,getCharacterHealthPoints(&testCharacter2)); } void test_setCharacterStrenght(void) { @@ -40,7 +40,7 @@ void test_setCharacterStrenght(void) { } void test_getCharacterStrength(void) { - TEST_ASSERT_EQUAL_INT(5,testCharacter2.strength); + TEST_ASSERT_EQUAL_INT(5,getCharacterStrength(&testCharacter2)); } void test_setCharacterDexterity(void) { @@ -50,7 +50,7 @@ void test_setCharacterDexterity(void) { } void test_getCharacterDexterity(void) { - TEST_ASSERT_EQUAL_INT(5,testCharacter2.dexterity); + TEST_ASSERT_EQUAL_INT(5,getCharacterDexterity(&testCharacter2)); } void test_setCharacterIntelligence(void) { @@ -60,7 +60,7 @@ void test_setCharacterIntelligence(void) { } void test_getCharacterIntelligence(void) { - TEST_ASSERT_EQUAL_INT(7,testCharacter2.intelligence); + TEST_ASSERT_EQUAL_INT(7,getCharacterIntelligence(&testCharacter2)); } void test_setCharacterLevel(void) { @@ -70,7 +70,7 @@ void test_setCharacterLevel(void) { } void test_getCharacterLevel(void) { - TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); + TEST_ASSERT_EQUAL_INT(5,getCharacterLevel(&testCharacter2)); } void test_increaseStat(void) { From b0a5408cbbbba0ce9b904da47d9bafee1f199a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Sat, 14 Jan 2023 19:25:10 +0100 Subject: [PATCH 30/53] Added first implementation of Fighting System --- src/c/encounter.c | 11 +++++++++++ src/c/encounter.h | 1 + test/c/test_encounter.c | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index 6a46528..18f79d8 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -64,3 +64,14 @@ int switchTurns(int currentTurn){ currentTurn = currentTurn % 2 + 1; return currentTurn; } + +int fight(int playerHealth, int playerDamage, int playerArmor, int playerAttack, enemy enemy){ + int currentTurn = 0; + while(playerAlive(playerHealth) && getEnemyInt(&enemy.health) > 0){ + setEnemyHealth(&enemy.health, enemyDamaged(enemy, playerDamage)); + } + if(playerAlive(playerHealth)){ + return 1; + } + return 0; +} diff --git a/src/c/encounter.h b/src/c/encounter.h index 1e5ea08..540247e 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -15,6 +15,7 @@ bool playerAlive(int health); int playerHealth(int health, int damage); int enemyDamaged(enemy enemy, int damage); int switchTurns(int currentTurn); +int fight(int playerHealth, int playerDamage, int playerArmor, int playerAttack, enemy enemy); //Getter/Setter Funktionen diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 09105c8..33edaa9 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -145,4 +145,17 @@ void test_switchingTurns(void) //assert TEST_ASSERT_EQUAL(1,result); } + +void test_FightPlayerWins(void) +{ + //arange + int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int result; + //arrange + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + result = fight(playerHealth, playerDamage, playerArmor, playerAttack, test); + //assert + TEST_ASSERT_EQUAL(1, result); +} #endif // TEST From 81d5f54a4ac7c90c5dab1eaaf236bccbdbd5293d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Sat, 14 Jan 2023 20:21:44 +0100 Subject: [PATCH 31/53] player and enemy take turns, enemy can loose --- src/c/encounter.c | 70 +++++++++++++++++++++++++++-------------- src/c/encounter.h | 4 +-- test/c/test_encounter.c | 21 +++++++++++-- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 18f79d8..2bf1159 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -8,70 +8,94 @@ /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt gegner erhalten eine zufällige menge Gold, die beim tod an den spieler gegeben werden -humanoide gegner haben heiltränke mit denen sie sich ggf heilen. -heilung erfolgt dann, wenn bestimmte hp (50%) unterschritten wird. wird allerdings gewürfelt, +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){ - if(health > 0){ +bool playerAlive(int health) +{ + if (health > 0) + { return true; } - else{ + else + { return false; } } -int playerHealth(int health, int damage){ +int playerHealth(int health, int damage, int armor) +{ const int maxhealth = 100; health = health - damage; - if(health > maxhealth){ + if (health > maxhealth) + { health = maxhealth; } return health; } -int enemyDamaged(enemy enemy, int damage){ +int enemyDamaged(enemy enemy, int damage) +{ int armor = getEnemyInt(&enemy.armor); int damagedealt = damage - armor; - if(damagedealt < 1){ + if (damagedealt < 1) + { damagedealt = 1; } enemy.health = enemy.health - damagedealt; return enemy.health; } - - -//Getter/Setter Funktionen -void *setEnemyHealth(int *num, int health){ +// Getter/Setter Funktionen +void *setEnemyHealth(int *num, int health) +{ *num = health; - //return health; + // return health; } -void *setEnemyDamage(int *num, int damage){ +void *setEnemyDamage(int *num, int damage) +{ *num = damage; } -void *setEnemyArmor(int *num, int armor){ +void *setEnemyArmor(int *num, int armor) +{ *num = armor; } -int getEnemyInt(int *structParam){ +int getEnemyInt(int *structParam) +{ return *structParam; } -int switchTurns(int currentTurn){ +int switchTurns(int currentTurn) +{ currentTurn = currentTurn % 2 + 1; return currentTurn; } -int fight(int playerHealth, int playerDamage, int playerArmor, int playerAttack, enemy enemy){ +int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy enemy) +{ int currentTurn = 0; - while(playerAlive(playerHealth) && getEnemyInt(&enemy.health) > 0){ - setEnemyHealth(&enemy.health, enemyDamaged(enemy, playerDamage)); + while (playerAlive(playerH) && getEnemyInt(&enemy.health) > 0) + { + if (currentTurn == 0) + { + setEnemyHealth(&enemy.health, enemyDamaged(enemy, playerDamage)); + } + else + { + playerH = playerHealth(playerH, getEnemyInt(&enemy.damage), playerArmor); + } + currentTurn = switchTurns(currentTurn); } - if(playerAlive(playerHealth)){ + if (playerAlive(playerH)) + { return 1; } - return 0; + else + { + return 0; + } } diff --git a/src/c/encounter.h b/src/c/encounter.h index 540247e..22bb1d9 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -12,10 +12,10 @@ typedef struct { bool playerAlive(int health); -int playerHealth(int health, int damage); +int playerHealth(int health, int damage, int armor); int enemyDamaged(enemy enemy, int damage); int switchTurns(int currentTurn); -int fight(int playerHealth, int playerDamage, int playerArmor, int playerAttack, enemy enemy); +int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy enemy); //Getter/Setter Funktionen diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 33edaa9..f3f7d61 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -36,9 +36,10 @@ void test_playerIsDamaged(void) //arrange int health = 100; int damage = 10; + int armor = 0; int expectedHealth = 90; //act - health = playerHealth(health, damage); + health = playerHealth(health, damage, armor); //assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -46,10 +47,11 @@ void test_playerIsDamaged(void) void test_playerIsNotOverhealed(void){ //arrange int health = 100; + int armor = 0; int heal = -10; int expectedHealth = 100; //act - health = playerHealth(health, heal); + health = playerHealth(health, heal, armor); //assert TEST_ASSERT_EQUAL(expectedHealth, health); } @@ -158,4 +160,19 @@ void test_FightPlayerWins(void) //assert TEST_ASSERT_EQUAL(1, result); } + +void test_FightEnemyWins(void) +{ + //arange + int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; + int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int result; + //arrange + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + result = fight(playerHealth, playerDamage, playerArmor, playerAttack, test); + //assert + TEST_ASSERT_EQUAL(0, result); +} + + #endif // TEST From b8dc8b5705d0b57663d08b36d57877b1e4b8537d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Sat, 14 Jan 2023 20:42:09 +0100 Subject: [PATCH 32/53] refactoring: unittest arrange/act/assert structure --- test/c/test_encounter.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index f3f7d61..72e8d4b 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -17,18 +17,23 @@ void test_isPlayerAlive_healthGreaterZero(void) { //arrange int health = 100; + bool result; //act + result = playerAlive(health); //assert - TEST_ASSERT_TRUE(playerAlive(health)); + TEST_ASSERT_TRUE(result); } void test_isPlayerAlive_healthLowerZero(void) { //arrange int health = -1; + bool result; //act + result = playerAlive(health); + //assert - TEST_ASSERT_FALSE(playerAlive(health)); + TEST_ASSERT_FALSE(result); } void test_playerIsDamaged(void) @@ -62,8 +67,9 @@ void test_setEnemyHealth(void){ //act enemy test = {health*2}; setEnemyHealth(&test.health, health); + result = test.health; //assert - TEST_ASSERT_EQUAL(health, test.health); + TEST_ASSERT_EQUAL(health, result); } void test_getEnemyHealth(void){ @@ -77,24 +83,34 @@ void test_getEnemyHealth(void){ } void test_setEnemyDamage(void){ + //arrange int damage = 4, result; enemy test = {50, damage*2}; + //act setEnemyDamage(&test.damage, damage); - TEST_ASSERT_EQUAL(damage, test.damage); + result = test.damage; + //assert + TEST_ASSERT_EQUAL(damage, result); } void test_getEnemyDamage(void){ + //arrange int damage = 4, result; + //act enemy test = {50, damage}; result = getEnemyInt(&test.damage); + //assert TEST_ASSERT_EQUAL(damage, result); } void test_get_setEnemyArmor(void){ + //arrange int armor = 4, result; enemy test = {50, 4, armor*2}; + //act setEnemyArmor(&test.armor, armor); result = getEnemyInt(&test.armor); + //assert TEST_ASSERT_EQUAL(armor, result); } @@ -131,12 +147,20 @@ void test_PlayerAttacksEnemy_DoesMinDamage(void) //A better way to get the value of a struct, REFACTORING if it works void test_getterWithParams(void) { + //arrange int health = 50, armor = 4, damage = 4, attack = 5; + int healthResult, armorResult, damageResult, attackResult; enemy test = {health, damage, armor, attack}; - TEST_ASSERT_EQUAL(health, getEnemyInt(&test.health)); - TEST_ASSERT_EQUAL(armor, getEnemyInt(&test.armor)); - TEST_ASSERT_EQUAL(damage, getEnemyInt(&test.damage)); - TEST_ASSERT_EQUAL(attack, getEnemyInt(&test.attack)); + //act + healthResult = getEnemyInt(&test.health); + armorResult = getEnemyInt(&test.armor); + damageResult = getEnemyInt(&test.damage); + attackResult = getEnemyInt(&test.attack); + //assert + TEST_ASSERT_EQUAL(health, healthResult); + TEST_ASSERT_EQUAL(armor, armorResult); + TEST_ASSERT_EQUAL(damage, damageResult); + TEST_ASSERT_EQUAL(attack, attackResult); } void test_switchingTurns(void) { From cccc31e36aa760cc06598de231319a444e9de48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 19 Jan 2023 12:13:59 +0100 Subject: [PATCH 33/53] Changed the setters to avoid Segmentation faults --- src/c/encounter.c | 64 ++++++++++++++++++++++++++--------------- src/c/encounter.h | 5 +++- test/c/test_encounter.c | 14 ++++----- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 2bf1159..c5c8984 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -37,7 +37,7 @@ int playerHealth(int health, int damage, int armor) int enemyDamaged(enemy enemy, int damage) { - int armor = getEnemyInt(&enemy.armor); + int armor = getEnemyArmor(&enemy); int damagedealt = damage - armor; if (damagedealt < 1) { @@ -47,27 +47,7 @@ int enemyDamaged(enemy enemy, int damage) return enemy.health; } -// Getter/Setter Funktionen -void *setEnemyHealth(int *num, int health) -{ - *num = health; - // return health; -} - -void *setEnemyDamage(int *num, int damage) -{ - *num = damage; -} - -void *setEnemyArmor(int *num, int armor) -{ - *num = armor; -} -int getEnemyInt(int *structParam) -{ - return *structParam; -} int switchTurns(int currentTurn) { @@ -78,7 +58,7 @@ int switchTurns(int currentTurn) int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy enemy) { int currentTurn = 0; - while (playerAlive(playerH) && getEnemyInt(&enemy.health) > 0) + while (playerAlive(playerH) && getEnemyHealth(&enemy) > 0) { if (currentTurn == 0) { @@ -86,7 +66,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } else { - playerH = playerHealth(playerH, getEnemyInt(&enemy.damage), playerArmor); + playerH = playerHealth(playerH, getEnemyDamage(&enemy), playerArmor); } currentTurn = switchTurns(currentTurn); } @@ -99,3 +79,41 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem return 0; } } + + +// Getter/Setter Funktionen +void *setEnemyHealth(int *num, int health) +{ + *num = health; + // return health; +} + +void *setEnemyDamage(int *num, int damage) +{ + *num = damage; +} + +void *setEnemyArmor(int *num, int armor) +{ + *num = armor; +} + +int getEnemyHealth(enemy* enemy) +{ + return enemy->health; +} + +int getEnemyArmor(enemy* enemy) +{ + return enemy->armor; +} + +int getEnemyDamage(enemy* enemy) +{ + return enemy->damage; +} + +int getEnemyAttack(enemy* enemy) +{ + return enemy->attack; +} \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index 22bb1d9..0a3031b 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -28,6 +28,9 @@ void *setEnemyDamage(int *num, int damage); //setEnemyArmor(&enemy.armor, armor); void *setEnemyArmor(int *num, int damage); //Function to get the value of Data in a struct, needs a pointer to the struct -int getEnemyInt(int *structParam); +int getEnemyAttack(enemy* enemy); +int getEnemyHealth(enemy* enemy); +int getEnemyArmor(enemy* enemy); +int getEnemyDamage(enemy* enemy); #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 72e8d4b..df41cdc 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -77,7 +77,7 @@ void test_getEnemyHealth(void){ int health = 50, result; //act enemy test = {health}; - result = getEnemyInt(&test.health); + result = getEnemyHealth(&test); //assert TEST_ASSERT_EQUAL(health, result); } @@ -98,7 +98,7 @@ void test_getEnemyDamage(void){ int damage = 4, result; //act enemy test = {50, damage}; - result = getEnemyInt(&test.damage); + result = getEnemyDamage(&test); //assert TEST_ASSERT_EQUAL(damage, result); } @@ -109,7 +109,7 @@ void test_get_setEnemyArmor(void){ enemy test = {50, 4, armor*2}; //act setEnemyArmor(&test.armor, armor); - result = getEnemyInt(&test.armor); + result = getEnemyArmor(&test); //assert TEST_ASSERT_EQUAL(armor, result); } @@ -152,10 +152,10 @@ void test_getterWithParams(void) int healthResult, armorResult, damageResult, attackResult; enemy test = {health, damage, armor, attack}; //act - healthResult = getEnemyInt(&test.health); - armorResult = getEnemyInt(&test.armor); - damageResult = getEnemyInt(&test.damage); - attackResult = getEnemyInt(&test.attack); + healthResult = getEnemyHealth(&test); + armorResult = getEnemyArmor(&test); + damageResult = getEnemyDamage(&test); + attackResult = getEnemyAttack(&test); //assert TEST_ASSERT_EQUAL(health, healthResult); TEST_ASSERT_EQUAL(armor, armorResult); From 286477ea599f9aa1be993c8ba68b1969a979257d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 19 Jan 2023 12:37:18 +0100 Subject: [PATCH 34/53] refactoring: changed getters to avoid seg.fault --- src/c/encounter.c | 64 ++++++++++++++++++++++++++--------------- src/c/encounter.h | 5 +++- test/c/test_encounter.c | 14 ++++----- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 2bf1159..c5c8984 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -37,7 +37,7 @@ int playerHealth(int health, int damage, int armor) int enemyDamaged(enemy enemy, int damage) { - int armor = getEnemyInt(&enemy.armor); + int armor = getEnemyArmor(&enemy); int damagedealt = damage - armor; if (damagedealt < 1) { @@ -47,27 +47,7 @@ int enemyDamaged(enemy enemy, int damage) return enemy.health; } -// Getter/Setter Funktionen -void *setEnemyHealth(int *num, int health) -{ - *num = health; - // return health; -} - -void *setEnemyDamage(int *num, int damage) -{ - *num = damage; -} - -void *setEnemyArmor(int *num, int armor) -{ - *num = armor; -} -int getEnemyInt(int *structParam) -{ - return *structParam; -} int switchTurns(int currentTurn) { @@ -78,7 +58,7 @@ int switchTurns(int currentTurn) int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy enemy) { int currentTurn = 0; - while (playerAlive(playerH) && getEnemyInt(&enemy.health) > 0) + while (playerAlive(playerH) && getEnemyHealth(&enemy) > 0) { if (currentTurn == 0) { @@ -86,7 +66,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } else { - playerH = playerHealth(playerH, getEnemyInt(&enemy.damage), playerArmor); + playerH = playerHealth(playerH, getEnemyDamage(&enemy), playerArmor); } currentTurn = switchTurns(currentTurn); } @@ -99,3 +79,41 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem return 0; } } + + +// Getter/Setter Funktionen +void *setEnemyHealth(int *num, int health) +{ + *num = health; + // return health; +} + +void *setEnemyDamage(int *num, int damage) +{ + *num = damage; +} + +void *setEnemyArmor(int *num, int armor) +{ + *num = armor; +} + +int getEnemyHealth(enemy* enemy) +{ + return enemy->health; +} + +int getEnemyArmor(enemy* enemy) +{ + return enemy->armor; +} + +int getEnemyDamage(enemy* enemy) +{ + return enemy->damage; +} + +int getEnemyAttack(enemy* enemy) +{ + return enemy->attack; +} \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index 22bb1d9..0a3031b 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -28,6 +28,9 @@ void *setEnemyDamage(int *num, int damage); //setEnemyArmor(&enemy.armor, armor); void *setEnemyArmor(int *num, int damage); //Function to get the value of Data in a struct, needs a pointer to the struct -int getEnemyInt(int *structParam); +int getEnemyAttack(enemy* enemy); +int getEnemyHealth(enemy* enemy); +int getEnemyArmor(enemy* enemy); +int getEnemyDamage(enemy* enemy); #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 72e8d4b..df41cdc 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -77,7 +77,7 @@ void test_getEnemyHealth(void){ int health = 50, result; //act enemy test = {health}; - result = getEnemyInt(&test.health); + result = getEnemyHealth(&test); //assert TEST_ASSERT_EQUAL(health, result); } @@ -98,7 +98,7 @@ void test_getEnemyDamage(void){ int damage = 4, result; //act enemy test = {50, damage}; - result = getEnemyInt(&test.damage); + result = getEnemyDamage(&test); //assert TEST_ASSERT_EQUAL(damage, result); } @@ -109,7 +109,7 @@ void test_get_setEnemyArmor(void){ enemy test = {50, 4, armor*2}; //act setEnemyArmor(&test.armor, armor); - result = getEnemyInt(&test.armor); + result = getEnemyArmor(&test); //assert TEST_ASSERT_EQUAL(armor, result); } @@ -152,10 +152,10 @@ void test_getterWithParams(void) int healthResult, armorResult, damageResult, attackResult; enemy test = {health, damage, armor, attack}; //act - healthResult = getEnemyInt(&test.health); - armorResult = getEnemyInt(&test.armor); - damageResult = getEnemyInt(&test.damage); - attackResult = getEnemyInt(&test.attack); + healthResult = getEnemyHealth(&test); + armorResult = getEnemyArmor(&test); + damageResult = getEnemyDamage(&test); + attackResult = getEnemyAttack(&test); //assert TEST_ASSERT_EQUAL(health, healthResult); TEST_ASSERT_EQUAL(armor, armorResult); From 0a10e4f5b9bbf58a5387b6eaad5185ba1fd93aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 19 Jan 2023 13:11:32 +0100 Subject: [PATCH 35/53] refactoring: also changed the getters --- src/c/encounter.c | 13 ++++++------- src/c/encounter.h | 8 ++++---- src/c/playerinput.c | 6 ++++++ src/c/playerinput.h | 6 ++++++ test/c/test_encounter.c | 6 +++--- 5 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 src/c/playerinput.c create mode 100644 src/c/playerinput.h diff --git a/src/c/encounter.c b/src/c/encounter.c index c5c8984..a4bbc22 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -82,20 +82,19 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem // Getter/Setter Funktionen -void *setEnemyHealth(int *num, int health) +void setEnemyHealth(enemy* enemy, int newhealth) { - *num = health; - // return health; + enemy->health = newhealth; } -void *setEnemyDamage(int *num, int damage) +void setEnemyDamage(enemy* enemy, int newdamage) { - *num = damage; + enemy->damage = newdamage; } -void *setEnemyArmor(int *num, int armor) +void setEnemyArmor(enemy* enemy, int newarmor) { - *num = armor; + enemy->armor = newarmor; } int getEnemyHealth(enemy* enemy) diff --git a/src/c/encounter.h b/src/c/encounter.h index 0a3031b..9e2e3df 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -20,13 +20,13 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem //Getter/Setter Funktionen -//setEnemyHealth(&enemy.health, health); -void *setEnemyHealth(int *num, int health); +//setEnemyHealth(&enemy, health); +void setEnemyHealth(enemy* enemy, int newhealth); //setEnemyDamage(&enemy.damage, damage); -void *setEnemyDamage(int *num, int damage); +void setEnemyDamage(enemy* enemy, int newdamage); //setEnemyArmor(&enemy.armor, armor); -void *setEnemyArmor(int *num, int damage); +void setEnemyArmor(enemy* enemy, int newarmor); //Function to get the value of Data in a struct, needs a pointer to the struct int getEnemyAttack(enemy* enemy); int getEnemyHealth(enemy* enemy); diff --git a/src/c/playerinput.c b/src/c/playerinput.c new file mode 100644 index 0000000..26a0a8b --- /dev/null +++ b/src/c/playerinput.c @@ -0,0 +1,6 @@ +#include "playerinput.h" + +char playerInput(){ + char c; + return c; +} \ No newline at end of file diff --git a/src/c/playerinput.h b/src/c/playerinput.h new file mode 100644 index 0000000..66f4325 --- /dev/null +++ b/src/c/playerinput.h @@ -0,0 +1,6 @@ +#ifndef PLAYERINPUT_H +#define PLAYERINPUT_H + +char playerInput(); + +#endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index df41cdc..eec8a3f 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -66,7 +66,7 @@ void test_setEnemyHealth(void){ int health = 50, result; //act enemy test = {health*2}; - setEnemyHealth(&test.health, health); + setEnemyHealth(&test, health); result = test.health; //assert TEST_ASSERT_EQUAL(health, result); @@ -87,7 +87,7 @@ void test_setEnemyDamage(void){ int damage = 4, result; enemy test = {50, damage*2}; //act - setEnemyDamage(&test.damage, damage); + setEnemyDamage(&test, damage); result = test.damage; //assert TEST_ASSERT_EQUAL(damage, result); @@ -108,7 +108,7 @@ void test_get_setEnemyArmor(void){ int armor = 4, result; enemy test = {50, 4, armor*2}; //act - setEnemyArmor(&test.armor, armor); + setEnemyArmor(&test, armor); result = getEnemyArmor(&test); //assert TEST_ASSERT_EQUAL(armor, result); From e60ea7fe9ec075c4c2b8d5c8db9b83be3ae0fca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 19 Jan 2023 18:28:05 +0100 Subject: [PATCH 36/53] Added PlayerInputs for Combat --- src/c/encounter.c | 29 +++++++++++++++++++---------- src/c/encounter.h | 6 +++--- test/c/test_encounter.c | 31 +++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index a4bbc22..599fa35 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -4,6 +4,7 @@ #include #include "encounter.h" +#include "playerinput.h" /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt @@ -35,16 +36,18 @@ int playerHealth(int health, int damage, int armor) return health; } -int enemyDamaged(enemy enemy, int damage) +void enemyDamaged(enemy *enemy, int damage) { - int armor = getEnemyArmor(&enemy); + int armor = getEnemyArmor(enemy); + int currentHealth = getEnemyHealth(enemy); int damagedealt = damage - armor; if (damagedealt < 1) { damagedealt = 1; } - enemy.health = enemy.health - damagedealt; - return enemy.health; + setEnemyHealth(enemy, currentHealth - damagedealt); + //enemy->health -= damagedealt; + //return enemy->health; } @@ -55,18 +58,24 @@ int switchTurns(int currentTurn) return 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; - while (playerAlive(playerH) && getEnemyHealth(&enemy) > 0) + int currentTurn = 0; + while (playerAlive(playerH) && getEnemyHealth(enemy) > 0) { if (currentTurn == 0) { - setEnemyHealth(&enemy.health, enemyDamaged(enemy, playerDamage)); + char decision = playerInput(); + switch(decision){ + case 'a': + enemyDamaged(enemy, playerDamage); + break; + } + } else { - playerH = playerHealth(playerH, getEnemyDamage(&enemy), playerArmor); + playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor); } currentTurn = switchTurns(currentTurn); } @@ -77,7 +86,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem else { return 0; - } + } } diff --git a/src/c/encounter.h b/src/c/encounter.h index 9e2e3df..154a91c 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -3,7 +3,7 @@ #include -typedef struct { +typedef struct enemy{ int health; int damage; int armor; @@ -13,9 +13,9 @@ typedef struct { bool playerAlive(int health); int playerHealth(int health, int damage, int armor); -int enemyDamaged(enemy enemy, int damage); +void enemyDamaged(enemy* enemy, int damage); 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); //Getter/Setter Funktionen diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index eec8a3f..56a100e 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -2,6 +2,8 @@ #include "unity.h" #include "encounter.h" +#include "playerinput.h" +#include "mock_playerinput.h" @@ -124,7 +126,7 @@ void test_PlayerAttacksEnemy_DoesDamage(void) int expectedEnemyHealth = 44; //act enemy test = {enemyHealth, 4, enemyArmor}; - test.health = enemyDamaged(test, playerDamage); + enemyDamaged(&test, playerDamage); //assert TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); } @@ -139,7 +141,7 @@ void test_PlayerAttacksEnemy_DoesMinDamage(void) int expectedEnemyHealth = 49; //act enemy test = {enemyHealth, 4, enemyArmor}; - test.health = enemyDamaged(test, playerDamage); + enemyDamaged(&test, playerDamage); //assert TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); } @@ -178,9 +180,10 @@ void test_FightPlayerWins(void) int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; int result; - //arrange + //aCt + playerInput_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, test); + result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); //assert TEST_ASSERT_EQUAL(1, result); } @@ -191,12 +194,28 @@ void test_FightEnemyWins(void) int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; int result; - //arrange + //act + playerInput_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; - result = fight(playerHealth, playerDamage, playerArmor, playerAttack, test); + result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); //assert TEST_ASSERT_EQUAL(0, result); } +void test_FightPlayerChoosesAttack(void){ + //arrange + int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; + int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int result; + //act + playerInput_ExpectAndReturn('a'); + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + result = getEnemyHealth(&test); + //assert + TEST_ASSERT_EQUAL(94, result); + +} + #endif // TEST From c0f1ce05f6c6bb13b0a6e82272bdcc9c6de49fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Thu, 19 Jan 2023 21:21:47 +0100 Subject: [PATCH 37/53] Heal in Combat now possible --- src/c/encounter.c | 12 +-- test/c/test_encounter.c | 161 ++++++++++++++++++++++------------------ 2 files changed, 97 insertions(+), 76 deletions(-) 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 From 43024c9e1914effcc9462bf82c7a162bb5d7c5c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 09:44:59 +0100 Subject: [PATCH 38/53] Player can now flee from combat --- src/c/encounter.c | 2 ++ test/c/test_encounter.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index a82e847..4937d79 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -72,6 +72,8 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem case 'h': playerH = playerHealth(playerH, -10, playerArmor); break; + case 'f': + return 2; } } diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 7ff85ea..34c5412 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -237,4 +237,18 @@ void test_FightPlayerHeals_thenAttacks_Wins(void) TEST_ASSERT_EQUAL(1, result); } +void test_FightPlayerFlees(void) +{ + //arrange + int playerHealth = 10, 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('f'); + result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); + //assert + TEST_ASSERT_EQUAL(2, result); +} + #endif // TEST From 081739ea4e2e01d663d37c20b0c3fc401f1a8aec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 10:24:50 +0100 Subject: [PATCH 39/53] Added helper lib to mock std. func. --- src/c/helper.c | 6 ++++++ src/c/helper.h | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 src/c/helper.c create mode 100644 src/c/helper.h diff --git a/src/c/helper.c b/src/c/helper.c new file mode 100644 index 0000000..7f94482 --- /dev/null +++ b/src/c/helper.c @@ -0,0 +1,6 @@ +#include "helper.h" + +int randomInt() +{ + return rand(); +} diff --git a/src/c/helper.h b/src/c/helper.h new file mode 100644 index 0000000..d7ceb04 --- /dev/null +++ b/src/c/helper.h @@ -0,0 +1,7 @@ +#ifndef HELPER_H +#define HELPER_H +//erlaubt es z.b. rand() zu mocken + +int randomInt; + +#endif \ No newline at end of file From e26d3268531405e83a2a4ba023c371e72983a457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 12:06:53 +0100 Subject: [PATCH 40/53] Function to get random nums in range --- src/c/encounter.c | 7 +++++++ src/c/encounter.h | 2 ++ src/c/helper.c | 2 +- src/c/helper.h | 3 ++- test/c/test_encounter.c | 13 +++++++++++++ test/c/test_helper.c | 23 +++++++++++++++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/c/test_helper.c diff --git a/src/c/encounter.c b/src/c/encounter.c index 4937d79..c8d441a 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -5,6 +5,7 @@ #include "encounter.h" #include "playerinput.h" +#include "helper.h" /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt @@ -93,6 +94,12 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } } +int randomIntRange(int min, int max) +{ + int value = randomInt(); + return (value % (max - min + 1)) + min; +} + // Getter/Setter Funktionen void setEnemyHealth(enemy* enemy, int newhealth) diff --git a/src/c/encounter.h b/src/c/encounter.h index 154a91c..c92a649 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -18,6 +18,8 @@ int switchTurns(int currentTurn); int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy* enemy); + +int randomIntRange(int min, int max); //Getter/Setter Funktionen //setEnemyHealth(&enemy, health); diff --git a/src/c/helper.c b/src/c/helper.c index 7f94482..7b3381d 100644 --- a/src/c/helper.c +++ b/src/c/helper.c @@ -3,4 +3,4 @@ int randomInt() { return rand(); -} +} \ No newline at end of file diff --git a/src/c/helper.h b/src/c/helper.h index d7ceb04..c1a5799 100644 --- a/src/c/helper.h +++ b/src/c/helper.h @@ -2,6 +2,7 @@ #define HELPER_H //erlaubt es z.b. rand() zu mocken -int randomInt; +int randomInt(); + #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 34c5412..2595ad6 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -4,9 +4,13 @@ #include "encounter.h" #include "playerinput.h" #include "mock_playerinput.h" +#include "helper.h" +#include "mock_helper.h" + void setUp(void) { + } void teardown(void) @@ -251,4 +255,13 @@ void test_FightPlayerFlees(void) TEST_ASSERT_EQUAL(2, result); } +void test_randomIntRange(void) +{ + int expected = 4; + randomInt_ExpectAndReturn(15); + int result = randomIntRange(1, 6); + TEST_ASSERT_EQUAL_INT(expected, result); +} + + #endif // TEST diff --git a/test/c/test_helper.c b/test/c/test_helper.c new file mode 100644 index 0000000..732f30c --- /dev/null +++ b/test/c/test_helper.c @@ -0,0 +1,23 @@ +#ifdef TEST +#include "unity.h" +#include "helper.h" +#include "mock_helper.h" + +void setUp(void) +{ +} + +void teardown(void) +{ +} + +//Just to test the mock +void test_randomInt(void) +{ + int expected = 42; + randomInt_ExpectAndReturn(expected); + TEST_ASSERT_EQUAL_INT(expected, randomInt()); +} + + +#endif//Test \ No newline at end of file From 15f117fd48670cf69d9525f01302300e10192243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 14:26:23 +0100 Subject: [PATCH 41/53] refactoring: renamed unused vars for future use --- src/c/encounter.c | 4 ++-- src/c/encounter.h | 4 ++-- test/c/test_encounter.c | 30 +++++++++++++++--------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index c8d441a..d192259 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -132,7 +132,7 @@ int getEnemyDamage(enemy* enemy) return enemy->damage; } -int getEnemyAttack(enemy* enemy) +int getEnemyMaxHealth(enemy* enemy) { - return enemy->attack; + return enemy->maxHealth; } \ No newline at end of file diff --git a/src/c/encounter.h b/src/c/encounter.h index c92a649..4005d70 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -7,7 +7,7 @@ typedef struct enemy{ int health; int damage; int armor; - int attack; + int maxHealth; } enemy; @@ -30,7 +30,7 @@ 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 -int getEnemyAttack(enemy* enemy); +int getEnemyMaxHealth(enemy* enemy); int getEnemyHealth(enemy* enemy); int getEnemyArmor(enemy* enemy); int getEnemyDamage(enemy* enemy); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 2595ad6..8284076 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -158,19 +158,19 @@ void test_PlayerAttacksEnemy_DoesMinDamage(void) void test_getterWithParams(void) { // arrange - int health = 50, armor = 4, damage = 4, attack = 5; - int healthResult, armorResult, damageResult, attackResult; - enemy test = {health, damage, armor, attack}; + int health = 50, armor = 4, damage = 4, maxHealth = 5; + int healthResult, armorResult, damageResult, maxHealthResult; + enemy test = {health, damage, armor, maxHealth}; // act healthResult = getEnemyHealth(&test); armorResult = getEnemyArmor(&test); damageResult = getEnemyDamage(&test); - attackResult = getEnemyAttack(&test); + maxHealthResult = getEnemyMaxHealth(&test); // assert TEST_ASSERT_EQUAL(health, healthResult); TEST_ASSERT_EQUAL(armor, armorResult); TEST_ASSERT_EQUAL(damage, damageResult); - TEST_ASSERT_EQUAL(attack, attackResult); + TEST_ASSERT_EQUAL(maxHealth, maxHealthResult); } void test_switchingTurns(void) { @@ -186,11 +186,11 @@ void test_FightPlayerWins(void) { // arange int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; - int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; // aCt playerInput_ExpectAndReturn('a'); - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert TEST_ASSERT_EQUAL(1, result); @@ -200,11 +200,11 @@ void test_FightEnemyWins(void) { // arange int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; - int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; // act playerInput_ExpectAndReturn('a'); - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert TEST_ASSERT_EQUAL(0, result); @@ -214,11 +214,11 @@ void test_FightPlayerChoosesAttack(void) { // arrange int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; - int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; // act playerInput_ExpectAndReturn('a'); - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); result = getEnemyHealth(&test); // assert @@ -229,10 +229,10 @@ 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 enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; //act - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInput_ExpectAndReturn('h'); playerInput_ExpectAndReturn('a'); playerInput_ExpectAndReturn('a'); @@ -245,10 +245,10 @@ void test_FightPlayerFlees(void) { //arrange int playerHealth = 10, playerDamage = 10, playerArmor = 4, playerAttack = 10; - int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyAttack = 5; + int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; //act - enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInput_ExpectAndReturn('f'); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); //assert From 8994bd77167ddc565971135ef78020f1959799dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 22:24:45 +0100 Subject: [PATCH 42/53] Enemies can now heal --- src/c/encounter.c | 6 ++++++ src/c/encounter.h | 1 + test/c/test_encounter.c | 14 ++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/c/encounter.c b/src/c/encounter.c index d192259..cdd4c39 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -37,6 +37,12 @@ int playerHealth(int health, int damage, int armor) return health; } +void enemyHeal(enemy *enemy, int healAmount) +{ + int currentHealth = getEnemyHealth(enemy); + setEnemyHealth(enemy, currentHealth + healAmount); +} + void enemyDamaged(enemy *enemy, int damage) { int armor = getEnemyArmor(enemy); diff --git a/src/c/encounter.h b/src/c/encounter.h index 4005d70..e70d454 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -13,6 +13,7 @@ typedef struct enemy{ bool playerAlive(int health); int playerHealth(int health, int damage, int armor); +void enemyHeal(enemy *enemy, int healAmount); void enemyDamaged(enemy* enemy, int damage); int switchTurns(int currentTurn); int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy* enemy); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 8284076..3cc2cc8 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -263,5 +263,19 @@ void test_randomIntRange(void) TEST_ASSERT_EQUAL_INT(expected, result); } +void test_enemyCanHeal(void) +{ + //arrange + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; + int healAmount= 10, expectedHealth = 11; + int result; + //act + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + enemyHeal(&test, healAmount); + result = getEnemyHealth(&test); + //assert + TEST_ASSERT_EQUAL(expectedHealth, result); +} + #endif // TEST From ac871cbdd6477432df33ea22e99c9be83cec0e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 22:26:45 +0100 Subject: [PATCH 43/53] Enemies can no longer Overheal --- src/c/encounter.c | 5 +++++ test/c/test_encounter.c | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index cdd4c39..0f48860 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -40,6 +40,11 @@ int playerHealth(int health, int damage, int armor) void enemyHeal(enemy *enemy, int healAmount) { int currentHealth = getEnemyHealth(enemy); + int maxHealth = getEnemyMaxHealth(enemy); + if (currentHealth + healAmount > maxHealth) + { + healAmount = maxHealth - currentHealth; + } setEnemyHealth(enemy, currentHealth + healAmount); } diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 3cc2cc8..32d6d00 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -266,7 +266,7 @@ void test_randomIntRange(void) void test_enemyCanHeal(void) { //arrange - int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 20; int healAmount= 10, expectedHealth = 11; int result; //act @@ -277,5 +277,18 @@ void test_enemyCanHeal(void) TEST_ASSERT_EQUAL(expectedHealth, result); } +void test_enemyNoOverheal(void) +{ + //arrange + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; + int healAmount= 10, expectedHealth = 5; + int result; + //act + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + enemyHeal(&test, healAmount); + result = getEnemyHealth(&test); + //assert + TEST_ASSERT_EQUAL(expectedHealth, result); +} #endif // TEST From 29c90f0e15318d3d0ca13b26cabbff348756ec78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 22:33:54 +0100 Subject: [PATCH 44/53] Added some additional files for easier testing --- .gitignore | 2 +- encounter_test.sh | 8 ++++++++ project.yml | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 encounter_test.sh diff --git a/.gitignore b/.gitignore index 9ee5328..9b9f152 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ builld/logs build/temp build/test .vscode -encounter_test.sh \ No newline at end of file +#encounter_test.sh \ No newline at end of file diff --git a/encounter_test.sh b/encounter_test.sh new file mode 100644 index 0000000..1a2253d --- /dev/null +++ b/encounter_test.sh @@ -0,0 +1,8 @@ +#Small scirpt to run the tests and generate the coverage report +#cleans artifacts before building +date +ceedling clean +ceedling gcov:encounter +ceedling utils:gcov +cd build/artifacts/gcov +xdg-open GcovCoverageResults.html & diff --git a/project.yml b/project.yml index 33ad5c2..cf73cc6 100644 --- a/project.yml +++ b/project.yml @@ -99,4 +99,5 @@ - stdout_pretty_tests_report - module_generator - xml_tests_report -... + - gcov +... From 8b4b35b46138df882b991db83fd4cd38f481f431 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 21 Jan 2023 16:45:11 +0100 Subject: [PATCH 45/53] prod: Added new stats exp and maxExp and added their getter and setter no Unittest as that is apparently not neccessary for them --- src/c/character.c | 16 ++++++++++++++++ src/c/character.h | 10 +++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/c/character.c b/src/c/character.c index cb8f3b8..779e795 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -46,6 +46,22 @@ int getCharacterLevel (Character *character) { return character->level; } +void setCharacterExp (Character *character, int newExp) { + character->exp = newExp; +} + +int getCharacterExp (Character *character) { + return character->exp; +} + +void setCharacterMaxExp (Character *character, int newMaxExp) { + character->maxExp = newMaxExp; +} + +int getCharacterMaxExp (Character *character) { + return character->maxExp; +} + int calculateStatIncrease (Character *character, int amount) { return character->level*amount; } diff --git a/src/c/character.h b/src/c/character.h index 633d3e1..c7ee567 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; + int strength,dexterity,intelligence,healthPoints,manaPoints,level,exp,maxExp; char name [50]; } Character; @@ -30,6 +30,14 @@ void setCharacterLevel (Character *character, int newLevel); int getCharacterLevel (Character *character); +void setCharacterExp (Character *character, int newExp); + +int getCharacterExp (Character *character); + +void setCharacterMaxExp (Character *character, int newMaxExp); + +int getCharacterMaxExp (Character *character); + void increaseStat (Character *character, int stat, int amount); int calculateStatIncrease (Character *character, int amount); From 3f03db161eecba8a883d191a286106ca2982458d Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 21 Jan 2023 18:05:18 +0100 Subject: [PATCH 46/53] prod/test: added new stats to increase stat function created levelUp function and 2 Unittests test_levelUp_* --- src/c/character.c | 12 +++++++++++- src/c/character.h | 4 +++- test/c/test_character.c | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index 779e795..f5a5bd4 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -66,7 +66,6 @@ int calculateStatIncrease (Character *character, int amount) { return character->level*amount; } - void increaseStat (Character *character, int stat, int amount) { switch (stat) { @@ -80,7 +79,18 @@ void increaseStat (Character *character, int stat, int amount) { character->healthPoints += amount;break; case 5: character->manaPoints += amount;break; + case 6: + character->level += amount;break; + case 7: + character->exp += amount;break; + case 8: + character->maxExp += amount;break; default: break; } } +void levelUp (Character *character) { + if (getCharacterExp(character) > getCharacterMaxExp(character)) { + increaseStat(character,LEVEL,1); + } +} \ No newline at end of file diff --git a/src/c/character.h b/src/c/character.h index c7ee567..7ed09b4 100644 --- a/src/c/character.h +++ b/src/c/character.h @@ -7,7 +7,7 @@ typedef struct { } Character; enum { - STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6 + STRENGTH = 1, DEXTERITY = 2, INTELLIGENCE = 3, HEALTHPOINTS = 4, MANAPOINTS = 5, LEVEL = 6, EXP = 7, MAXEXP = 8 }; void setCharacterHealthPoints (Character *character, int newHealthPoints); @@ -41,4 +41,6 @@ int getCharacterMaxExp (Character *character); void increaseStat (Character *character, int stat, int amount); int calculateStatIncrease (Character *character, int amount); + +void levelUp (Character *character); #endif \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index ad72b33..ba52049 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -16,6 +16,10 @@ void setUp(void) testCharacter2.intelligence = 7; testCharacter.level = 0; testCharacter2.level = 5; + testCharacter.exp = 50; + testCharacter2.exp = 110; + testCharacter.maxExp = 100; + testCharacter2.maxExp = 100; } void tearDown(void) @@ -78,10 +82,30 @@ void test_increaseStat(void) { TEST_ASSERT_EQUAL_INT(20,testCharacter2.strength); increaseStat(&testCharacter2,INTELLIGENCE,15); TEST_ASSERT_EQUAL_INT(22,testCharacter2.intelligence); + increaseStat(&testCharacter2,LEVEL,15); + TEST_ASSERT_EQUAL_INT(20,testCharacter2.level); } void test_calculateStatIncrease(void) { TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); TEST_ASSERT_EQUAL_INT(25,calculateStatIncrease(&testCharacter2,5)); } + +void test_levelUp_enoughExp (void) { + TEST_ASSERT_EQUAL_INT(110,testCharacter2.exp); + TEST_ASSERT_EQUAL_INT(100,testCharacter2.maxExp); + TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); + levelUp(&testCharacter2); + TEST_ASSERT_EQUAL_INT(6,testCharacter2.level); +} + +void test_levelUp_notEnoughExp (void) { + TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); + TEST_ASSERT_EQUAL_INT(100,testCharacter.maxExp); + TEST_ASSERT_EQUAL_INT(0,testCharacter.level); + levelUp(&testCharacter); + TEST_ASSERT_EQUAL_INT(0,testCharacter.level); +} + + #endif // TEST \ No newline at end of file From 95189c0ff044d71de6b54cc4ca0fa0e60b5edd01 Mon Sep 17 00:00:00 2001 From: Kai Kehres Date: Sat, 21 Jan 2023 18:12:13 +0100 Subject: [PATCH 47/53] prod/test: added functionality to levelUp created new Unittest for levelUp --- src/c/character.c | 1 + test/c/test_character.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/c/character.c b/src/c/character.c index f5a5bd4..9c436ff 100644 --- a/src/c/character.c +++ b/src/c/character.c @@ -92,5 +92,6 @@ void increaseStat (Character *character, int stat, int amount) { void levelUp (Character *character) { if (getCharacterExp(character) > getCharacterMaxExp(character)) { increaseStat(character,LEVEL,1); + setCharacterExp(character,getCharacterExp(character)-getCharacterMaxExp(character)); } } \ No newline at end of file diff --git a/test/c/test_character.c b/test/c/test_character.c index ba52049..6d9fd46 100644 --- a/test/c/test_character.c +++ b/test/c/test_character.c @@ -92,20 +92,29 @@ void test_calculateStatIncrease(void) { } void test_levelUp_enoughExp (void) { - TEST_ASSERT_EQUAL_INT(110,testCharacter2.exp); - TEST_ASSERT_EQUAL_INT(100,testCharacter2.maxExp); TEST_ASSERT_EQUAL_INT(5,testCharacter2.level); levelUp(&testCharacter2); TEST_ASSERT_EQUAL_INT(6,testCharacter2.level); } +void test_levelUp_enoughExp_loseExp (void) { + TEST_ASSERT_EQUAL_INT(110,testCharacter2.exp); + TEST_ASSERT_EQUAL_INT(100,testCharacter2.maxExp); + levelUp(&testCharacter2); + TEST_ASSERT_EQUAL_INT(10,testCharacter2.exp); +} + void test_levelUp_notEnoughExp (void) { - TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); - TEST_ASSERT_EQUAL_INT(100,testCharacter.maxExp); TEST_ASSERT_EQUAL_INT(0,testCharacter.level); levelUp(&testCharacter); TEST_ASSERT_EQUAL_INT(0,testCharacter.level); } +void test_levelUp_notEnoughExp_loseExp (void) { + TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); + TEST_ASSERT_EQUAL_INT(100,testCharacter.maxExp); + levelUp(&testCharacter); + TEST_ASSERT_EQUAL_INT(50,testCharacter.exp); +} #endif // TEST \ No newline at end of file From ae5e7ef9c472402f79e586deff30e344a66687f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 13:30:56 +0100 Subject: [PATCH 48/53] Added tests for randIntRange --- test/c/test_encounter.c | 56 ++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 32d6d00..b9bc4de 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -7,10 +7,8 @@ #include "helper.h" #include "mock_helper.h" - void setUp(void) { - } void teardown(void) @@ -223,35 +221,35 @@ void test_FightPlayerChoosesAttack(void) result = getEnemyHealth(&test); // assert TEST_ASSERT_EQUAL(0, result); -} +} -void test_FightPlayerHeals_thenAttacks_Wins(void) +void test_FightPlayerHeals_thenAttacks_Wins(void) { - //arrange + // arrange int playerHealth = 2, playerDamage = 10, playerArmor = 4, playerAttack = 10; int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; - //act + // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInput_ExpectAndReturn('h'); playerInput_ExpectAndReturn('a'); playerInput_ExpectAndReturn('a'); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); - //assert + // assert TEST_ASSERT_EQUAL(1, result); } void test_FightPlayerFlees(void) { - //arrange + // arrange int playerHealth = 10, playerDamage = 10, playerArmor = 4, playerAttack = 10; int enemyHealth = 11, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; - //act + // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInput_ExpectAndReturn('f'); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); - //assert + // assert TEST_ASSERT_EQUAL(2, result); } @@ -265,30 +263,52 @@ void test_randomIntRange(void) void test_enemyCanHeal(void) { - //arrange + // arrange int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 20; - int healAmount= 10, expectedHealth = 11; + int healAmount = 10, expectedHealth = 11; int result; - //act + // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; enemyHeal(&test, healAmount); result = getEnemyHealth(&test); - //assert + // assert TEST_ASSERT_EQUAL(expectedHealth, result); } void test_enemyNoOverheal(void) { - //arrange + // arrange int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; - int healAmount= 10, expectedHealth = 5; + int healAmount = 10, expectedHealth = 5; int result; - //act + // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; enemyHeal(&test, healAmount); result = getEnemyHealth(&test); - //assert + // assert TEST_ASSERT_EQUAL(expectedHealth, result); } +void test_roll_20() +{ + int expected = 20; + int result; + + randomInt_ExpectAndReturn(39); + result = randomIntRange(1, 20); + + TEST_ASSERT_EQUAL(expected, result); +} + +void test_roll_01() +{ + int expected = 1; + int result; + + randomInt_ExpectAndReturn(40); + result = randomIntRange(1, 20); + + TEST_ASSERT_EQUAL(expected, result); +} + #endif // TEST From 4add252f4582a4c136615f1fef684a1f17e46b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 13:36:39 +0100 Subject: [PATCH 49/53] Added Map Function to Map Values --- src/c/encounter.c | 5 +++++ src/c/encounter.h | 4 ++-- test/c/test_encounter.c | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 0f48860..d3b14e6 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -112,6 +112,11 @@ int randomIntRange(int min, int max) } +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; +} + // Getter/Setter Funktionen void setEnemyHealth(enemy* enemy, int newhealth) { diff --git a/src/c/encounter.h b/src/c/encounter.h index e70d454..e2070d5 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -18,8 +18,8 @@ void enemyDamaged(enemy* enemy, int damage); int switchTurns(int currentTurn); int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, 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 diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index b9bc4de..6e4a30d 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -311,4 +311,16 @@ void test_roll_01() TEST_ASSERT_EQUAL(expected, result); } +void test_map10(){ + int expected1 = 10, expected2 = 0, expected3 = 20; + int result; + + result = map(50, 0, 100, 0, 20); + TEST_ASSERT_EQUAL(expected1, result); + result = map(0, 0, 100, 0, 20); + TEST_ASSERT_EQUAL(expected2, result); + result = map(100, 0, 100, 0, 20); + TEST_ASSERT_EQUAL(expected3, result); +} + #endif // TEST From 457e8b5b3c0b945d06237fec069f033bef2d747b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 13:55:30 +0100 Subject: [PATCH 50/53] Function to make Enemy Heal --- src/c/encounter.c | 11 ++++++++++- src/c/encounter.h | 1 + test/c/test_encounter.c | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index d3b14e6..2e9e547 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -112,11 +112,20 @@ int randomIntRange(int min, int max) } -int map(int x, int in_min, int in_max, int out_min, int out_max){ +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) +{ + int currentHealth = getEnemyHealth(enemy); + int maxHealth = getEnemyMaxHealth(enemy); + int healthd20 = map(currentHealth, 0, maxHealth, 0, 20); + return healthd20 <= 10; +} + // Getter/Setter Funktionen void setEnemyHealth(enemy* enemy, int newhealth) { diff --git a/src/c/encounter.h b/src/c/encounter.h index e2070d5..312f35a 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -15,6 +15,7 @@ bool playerAlive(int health); int playerHealth(int health, int damage, int armor); 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); diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 6e4a30d..c4246de 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -323,4 +323,17 @@ void test_map10(){ TEST_ASSERT_EQUAL(expected3, result); } +void test_enemyChoosesHeal(void) +{ + //arange + bool result; + int enemyHealth = 50, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + //act + result = enemyChoosesHeal(&test); + //assert + TEST_ASSERT_TRUE(result); + +} + #endif // TEST From 184c41412a8f0640887696572fe63fc8a2bf53ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 14:27:54 +0100 Subject: [PATCH 51/53] Healing is weighted, lesser health more heal --- src/c/encounter.c | 5 +++-- test/c/test_encounter.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 2e9e547..9904048 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -122,8 +122,9 @@ bool enemyChoosesHeal(enemy* enemy) { int currentHealth = getEnemyHealth(enemy); int maxHealth = getEnemyMaxHealth(enemy); - int healthd20 = map(currentHealth, 0, maxHealth, 0, 20); - return healthd20 <= 10; + int healthd20 = 20 - map(currentHealth, 0, maxHealth, 0, 20); + int rolld20 = randomIntRange(1, 20); + return (healthd20 + rolld20) >= 30; } // Getter/Setter Funktionen diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index c4246de..5ef5b65 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -330,10 +330,25 @@ void test_enemyChoosesHeal(void) int enemyHealth = 50, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; //act + randomInt_ExpectAndReturn(39); //39%20 = 19 , 19 + 1 = 20 result = enemyChoosesHeal(&test); //assert TEST_ASSERT_TRUE(result); } +void test_enemyDoesNotChoosesHeal(void) +{ + //arange + bool result; + int enemyHealth = 50, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + //act + randomInt_ExpectAndReturn(14); //14%20 = 14 , 14 + 1 = 15 + result = enemyChoosesHeal(&test); + //assert + TEST_ASSERT_FALSE(result); + +} + #endif // TEST From 198e7b9b291cf18e2716ec8e4ad778a029128695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 18:29:15 +0100 Subject: [PATCH 52/53] Added Healing to the fight function --- src/c/encounter.c | 11 +++++++++-- test/c/test_encounter.c | 29 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index 9904048..e0ef993 100644 --- a/src/c/encounter.c +++ b/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 currentTurn = 0; + int currentTurn = 2; char decision; while (playerAlive(playerH) && getEnemyHealth(enemy) > 0) { @@ -91,7 +91,14 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } else { - playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor); + if(enemyChoosesHeal(enemy)) + { + enemyHeal(enemy, 10); + } + else + { + playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor); + } } currentTurn = switchTurns(currentTurn); } diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 5ef5b65..d33bca3 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -198,10 +198,11 @@ void test_FightEnemyWins(void) { // arange 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; // act playerInput_ExpectAndReturn('a'); + randomInt_ExpectAndReturn(1); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert @@ -212,7 +213,7 @@ void test_FightPlayerChoosesAttack(void) { // arrange 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; // act playerInput_ExpectAndReturn('a'); @@ -227,12 +228,14 @@ void test_FightPlayerHeals_thenAttacks_Wins(void) { // arrange 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; // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; playerInput_ExpectAndReturn('h'); + randomInt_ExpectAndReturn(1); playerInput_ExpectAndReturn('a'); + randomInt_ExpectAndReturn(1); playerInput_ExpectAndReturn('a'); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert @@ -243,7 +246,7 @@ void test_FightPlayerFlees(void) { // arrange 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; // act 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 From f34bfa37ab574c7aff9c2d75a6fc751aa9081e96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Mon, 23 Jan 2023 18:50:19 +0100 Subject: [PATCH 53/53] refactoring: renamed some variables --- src/c/encounter.c | 2 +- src/c/playerinput.c | 2 +- src/c/playerinput.h | 2 +- test/c/test_encounter.c | 18 +++++++++--------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index e0ef993..5ea3027 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -76,7 +76,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem { if (currentTurn != 1) { - decision = playerInput(); + decision = playerInputChar(); switch(decision){ case 'a': enemyDamaged(enemy, playerDamage); diff --git a/src/c/playerinput.c b/src/c/playerinput.c index 26a0a8b..478d563 100644 --- a/src/c/playerinput.c +++ b/src/c/playerinput.c @@ -1,6 +1,6 @@ #include "playerinput.h" -char playerInput(){ +char playerInputChar(){ char c; return c; } \ No newline at end of file diff --git a/src/c/playerinput.h b/src/c/playerinput.h index 66f4325..d6d6ffa 100644 --- a/src/c/playerinput.h +++ b/src/c/playerinput.h @@ -1,6 +1,6 @@ #ifndef PLAYERINPUT_H #define PLAYERINPUT_H -char playerInput(); +char playerInputChar(); #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index d33bca3..0f07a36 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -187,7 +187,7 @@ void test_FightPlayerWins(void) int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; int result; // aCt - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert @@ -201,7 +201,7 @@ void test_FightEnemyWins(void) int enemyHealth = 100, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(1); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); @@ -216,7 +216,7 @@ void test_FightPlayerChoosesAttack(void) int enemyHealth = 6, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 100; int result; // act - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); result = getEnemyHealth(&test); @@ -232,11 +232,11 @@ void test_FightPlayerHeals_thenAttacks_Wins(void) int result; // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; - playerInput_ExpectAndReturn('h'); + playerInputChar_ExpectAndReturn('h'); randomInt_ExpectAndReturn(1); - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(1); - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert TEST_ASSERT_EQUAL(1, result); @@ -250,7 +250,7 @@ void test_FightPlayerFlees(void) int result; // act enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; - playerInput_ExpectAndReturn('f'); + playerInputChar_ExpectAndReturn('f'); result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test); // assert TEST_ASSERT_EQUAL(2, result); @@ -362,9 +362,9 @@ void test_enemyChoosesHeal_ThenAttackWins(void) int playerHealth = 10, playerDamage = 10, playerArmor = 5, playerAttack = 10; enemy test2 = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; //act - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(39); //39%20 = 19 , 19 + 1 = 20 - playerInput_ExpectAndReturn('a'); + playerInputChar_ExpectAndReturn('a'); randomInt_ExpectAndReturn(0); //0%20 = 0 , 0 + 1 = 1 result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test2); //assert