Battle Program.

Posts 1–3 of 3 · Page 1 of 1
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.
Quote Originally Posted by That0n3Guy View Post
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.

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.
Lol. Nice little game there. And more importantly good organization of the function. It turn my stomach inside out when games like these are poorly thought. It's pretty good for you first program, I'm the kind of person who likes to test every little thing step by step as I learn, but putting it all together is good too. +1 for use of references.

The only bad part is... haha It's impossible to lose xD!

Maybe you should pic up this book that teaches you C++ basics via game programming. Personally I rather prefer something dry and to the point, but if that's your thing go for it
Quote Originally Posted by why06 View Post
Lol. Nice little game there. And more importantly good organization of the function. It turn my stomach inside out when games like these are poorly thought. It's pretty good for you first program, I'm the kind of person who likes to test every little thing step by step as I learn, but putting it all together is good too. +1 for use of references.

The only bad part is... haha It's impossible to lose xD!

Maybe you should pic up this book that teaches you C++ basics via game programming. Personally I rather prefer something dry and to the point, but if that's your thing go for it
I was thinking about testing everything as I learned it, and that's kind of how it got started. The tutorial I was reading had to do with manipulating variables and I wrote,

Code:
#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
int PlayerHealth = 100;
PlayerHealth -= 10;
cout << PlayerHealth;
}
and I decided to try and make a crappy game using what I had learned and this is what happened. x]

Also, I'm aware it's impossible to lose :P

I haven't learned randomizing variables yet, that's a while later in the book I'm reading.

Edit: I just finished an update, you can now choose between one of 3 preset weapons. I'll expand on that later. Just finished reading about classes.
Posts 1–3 of 3 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us