Browse Source

Added PlayerInputs for Combat

remotes/origin/encounter
János 2 years ago
parent
commit
e60ea7fe9e
  1. 29
      src/c/encounter.c
  2. 6
      src/c/encounter.h
  3. 31
      test/c/test_encounter.c

29
src/c/encounter.c

@ -4,6 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "encounter.h" #include "encounter.h"
#include "playerinput.h"
/*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein
evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt 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; 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; int damagedealt = damage - armor;
if (damagedealt < 1) if (damagedealt < 1)
{ {
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; 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) if (currentTurn == 0)
{ {
setEnemyHealth(&enemy.health, enemyDamaged(enemy, playerDamage));
char decision = playerInput();
switch(decision){
case 'a':
enemyDamaged(enemy, playerDamage);
break;
}
} }
else else
{ {
playerH = playerHealth(playerH, getEnemyDamage(&enemy), playerArmor);
playerH = playerHealth(playerH, getEnemyDamage(enemy), playerArmor);
} }
currentTurn = switchTurns(currentTurn); currentTurn = switchTurns(currentTurn);
} }
@ -77,7 +86,7 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem
else else
{ {
return 0; return 0;
}
}
} }

6
src/c/encounter.h

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
typedef struct {
typedef struct enemy{
int health; int health;
int damage; int damage;
int armor; int armor;
@ -13,9 +13,9 @@ typedef struct {
bool playerAlive(int health); bool playerAlive(int health);
int playerHealth(int health, int damage, int armor); 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 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 //Getter/Setter Funktionen

31
test/c/test_encounter.c

@ -2,6 +2,8 @@
#include "unity.h" #include "unity.h"
#include "encounter.h" #include "encounter.h"
#include "playerinput.h"
#include "mock_playerinput.h"
@ -124,7 +126,7 @@ void test_PlayerAttacksEnemy_DoesDamage(void)
int expectedEnemyHealth = 44; int expectedEnemyHealth = 44;
//act //act
enemy test = {enemyHealth, 4, enemyArmor}; enemy test = {enemyHealth, 4, enemyArmor};
test.health = enemyDamaged(test, playerDamage);
enemyDamaged(&test, playerDamage);
//assert //assert
TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health);
} }
@ -139,7 +141,7 @@ void test_PlayerAttacksEnemy_DoesMinDamage(void)
int expectedEnemyHealth = 49; int expectedEnemyHealth = 49;
//act //act
enemy test = {enemyHealth, 4, enemyArmor}; enemy test = {enemyHealth, 4, enemyArmor};
test.health = enemyDamaged(test, playerDamage);
enemyDamaged(&test, playerDamage);
//assert //assert
TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health); TEST_ASSERT_EQUAL(expectedEnemyHealth, test.health);
} }
@ -178,9 +180,10 @@ void test_FightPlayerWins(void)
int playerHealth = 100, playerDamage = 10, playerArmor = 4, playerAttack = 5; 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, enemyAttack = 5;
int result; int result;
//arrange
//aCt
playerInput_ExpectAndReturn('a');
enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack};
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, test);
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test);
//assert //assert
TEST_ASSERT_EQUAL(1, result); TEST_ASSERT_EQUAL(1, result);
} }
@ -191,12 +194,28 @@ void test_FightEnemyWins(void)
int playerHealth = 1, playerDamage = 10, playerArmor = 4, playerAttack = 5; 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, enemyAttack = 5;
int result; int result;
//arrange
//act
playerInput_ExpectAndReturn('a');
enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack}; enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyAttack};
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, test);
result = fight(playerHealth, playerDamage, playerArmor, playerAttack, &test);
//assert //assert
TEST_ASSERT_EQUAL(0, result); 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 #endif // TEST
Loading…
Cancel
Save