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