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] 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