Battle Program.
Info:
The first program I've ever made in C++. Took me about 20 minutes to make and find errors. At the moment, it work's perfectly fine to my knowledge with no bugs. It's nothing special, the damage dealt and health healed aren't random, they're a set value.
Source:
Battle.cpp:
Code:
#include "stdafx.h" // Pre-processor to include precompiled standard hearer
#include "PlayerAction.h" // Pre-processor to include my custom header
#include <iostream>
using namespace std; // Make sure it's using the package inside iostream named std.
int main() // Define main function with int because it's returning 0, meaning the program ran correctly and finished.
{
while(EnemyHealth > 0 && PlayerHealth > 0) // Checks once to see if Player and Enemy have above 0 health, then repeats until they no longer are above 0.
{
char action; // Define a character
cout << "Would you like to Attack, Block, or Heal? \n (Please use A, B, or H.) \n";
cout << "Action: ";
cin >> action; // Get user input for the "action" variable.
switch (action) // Use players input and cases to find out what they want to do
{
case 'A': // Case A, when a user types in A
atk(PlayerHealth, EnemyHealth); // Do the attack function, using the variables (integers) PlayerHealth and EnemyHealth
break;
case 'B': // Case B, when a user types in B
block(PlayerHealth, EnemyHealth); // Do the block function, variables aren't really needed, not sure why I put them there for that one =\
break;
case 'H': // Case H, when a user types in H
heal(PlayerHealth); // Do the heal function, using the variable PlayerHealth.
break;
}
}
if(PlayerHealth <= 0) // If the players health went below 0, print that they lost
{
cout << "You lost the fight! \n";
} else if (EnemyHealth <= 0) { // Or if the enemies health went below 0, print that the user won
cout << "You won the fight! \n";
}
system("pause"); // Pause the program
return 0;
}
PlayerAction.h
Code:
// Preprocessor header files
#include "stdafx.h"
#include <iostream>
using namespace std;
// Define the variables PlayerHealth and Enemy health
int EnemyHealth = 100;
int PlayerHealth = 100;
void EnemyAtk(int &PlayHealth, int &EnemHealth) // Void function, using two variables, the & is before them so that the variable is returned to the main function without actually using return (w/e);.
{
PlayerHealth -= 5; // Take away 5 from the players health
cout << "The enemy successfully hits you for 5 damage. \n \n \n";
cout << "Your Health: " << PlayHealth << "\n";
cout << "Enemy Health: " << EnemHealth << "\n \n";
}
void atk(int &PlayerHealth, int &EnemyHealth)
{
system("cls");
EnemyHealth -= 10;
cout << "You hit the enemy for 10 damage! \n";
EnemyAtk(PlayerHealth, EnemyHealth);
}
void block(int &PlayerHealth, int &EnemyHealth)
{
system("cls");
cout << "You successfully blocked your enemies attack! \n";
}
void heal(int &PlayerHealth)
{
system("cls");
PlayerHealth += 10;
cout << "You heal yourself for 10 HP! \n";
EnemyAtk(PlayerHealth, EnemyHealth);
}
I commented out everything that I think I understand. I know this code is far from perfect and it's also very far from being good/properly written/useful. If you have any suggestions for something I should add, post them. You can do whatever you would like with the code, it's not very important to me, I was merely using it to test a few of the things I had learned.