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