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/32] 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/32] 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/32] 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/32] 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 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 05/32] 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 06/32] 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 07/32] 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 08/32] 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 09/32] 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 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 10/32] 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 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 11/32] 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 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 12/32] 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 13/32] 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 14/32] 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 15/32] 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 16/32] 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 17/32] 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 18/32] 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 19/32] 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 20/32] 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 21/32] 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 22/32] 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 23/32] 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 24/32] 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 25/32] 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 26/32] 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 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 27/32] 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 28/32] 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 29/32] 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 30/32] 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 31/32] 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 32/32] 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