• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Critique my Game

Junior Spellweaver
Joined
Oct 22, 2005
Messages
168
Reaction score
1
I haven't done any programming in over 10 months so this code is probably very sloppy and messy. If you have some spare time I was hoping someone could show me some ways to simplify my code. It works for the most part....

Description: A small text based battle system, made with C++

If you just want to play the game you can download it .
Code:
#include <iostream>
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
using namespace std;

int pHealth(20); //Player Health
int mHealth(20); //Monster Health

void win()
{
	cout << "You live to see another day.\n WIN\n";
}
void lose()
{
	cout << "You got your butt handed to you.\n LOSE\n";
}
void draw()
{
	cout << "You and the monster kill each other at the same time.\n DRAW\n";
}
void battle()
{
	int pAction, pDamage(0), pCritical, pAttack(7), pDefense(5), pDodge; //Player Values
	int mAction, mDamage(0), mCritical, mAttack(7), mDefense(5), mDodge; //Monster Values

	cout << "A monster approaches you in battle.\n";
	cout << "Your HP is: " << pHealth << "\nThe Monster's HP is: " << mHealth << "\n" ;
	cout << "What do you wish to do?\n\n1)Attack\n2)Defend\n3)Dodge\n";
	cout << "> ";
	cin >> pAction; //Get Players Action
	cout << "\n\n\n";
	srand(time(0));  // Initialize random number generator.
	mAction = (rand() % 3) + 1; //Generates random number 1 to 3


// CHECK FOR CRITICAL ATTACKS
	srand(time(0));  // Initialize random number generator.
	pCritical = (rand() % 5) + 1; //Generates random number 1 to 5
	if(pCritical == 5 && pAction == 1)
	{
		pAttack = pAttack*2;
		cout << "Critical Hit\n";
	}
	srand(time(0));  // Initialize random number generator.
	mCritical = (rand() % 5) + 1; //Generates random number 1 to 5
	if(mCritical == 5 && mAction == 1)
	{
		mAttack = mAttack*2;
		cout << "The Monster Critically Hit You\n";
	}
// CHECK FOR DODGE SUCCESS
	srand(time(0));  // Initialize random number generator.
	pDodge = (rand() % 2) + 1; //Generates random number 1 to 2
	if(pDodge == 2 && pAction == 3 && mAction == 1)
	{
		mAttack = 0;
		cout << "You Successfully Dodge The Incoming Attack\n";
	}
	else if(pDodge != 2 && pAction == 3)
	{
		cout << "You Failed To Dodge The Monster\n";
	}
	srand(time(0));  // Initialize random number generator.
	mDodge = (rand() % 2) + 1; //Generates random number 1 to 2
	if(mDodge == 2 && mAction == 3 && pAction == 1)
	{
		pAttack = 0;
		cout << "The Monster Dodged Your Attack\n";
	}
	else if(mDodge != 2 && mAction == 3)
	{
		cout << "The Monster Failed To Dodge Your Attack\n";
	}

	switch(pAction)
	{
	case 1:
		switch(mAction)
		{
		case 1:
			// If both the monster and Player attack.
			pDamage = pAttack - mDefense;
			mDamage = mAttack - pDefense;
			cout << "You Attack\n";
			cout << "The Monster Attacks\n";
			break;
		case 2:
			// If the Player Attacks and the Monster Defends
			pDamage = pAttack - mDefense*2;
			mDamage = 0;
			cout << "You Attack\n";
			cout << "The Monster Defends\n";
			break;
		case 3:
			// If the Player Attacks and the Monster Dodges
			pDamage = pAttack - mDefense;
			mDamage = 0;
			cout << "You Attack\n";
			break;
		}
		break;
	case 2:
		switch(mAction)
		{
		case 1:
			//If the player defends and the Monster Attacks
			pDamage = 0;
			mDamage = mAttack - pDefense*2;
			cout << "You Defend Yourself\n";
			cout << "The Monster Attacks\n";
			break;
		case 2:
			//If Both defend
			pDamage = 0;
			mDamage = 0;
			cout << "You Defend Yourself\n";
			cout << "The Monster Defends\n";
			break;
		case 3:
			//If player Defends and Monster dodges
			pDamage = 0;
			mDamage = 0;
			cout << "You Defend Yourself\n";
			break;
		}
		break;
	case 3:
		switch(mAction)
		{
		case 1:
			//If player Dodges and the Monster Attacks
			pDamage = 0;
			mDamage = mAttack - pDefense;
			cout << "The Monster Attacks\n";
			break;
		case 2:
			//If the player Dodges and the Monster Defends
			pDamage = 0;
			mDamage = 0;
			cout << "The Monster Defends\n";
			break;
		case 3:
			//If both dodge
			pDamage = 0;
			mDamage = 0;
			break;
		}
		break;
	default:
		battle();
	}
	if(pDamage < 0)
	{
		pDamage = 0; //Prevents negative damage
	}
	if(mDamage < 0)
	{
		mDamage = 0; //Prevents negative damage
	}
	pHealth = pHealth - mDamage; //Get new players Health
	mHealth = mHealth - pDamage; //Get new Monsters Health

	cout << "\n\n\n";

	if(pHealth <= 0 && mHealth > 0)
	{
		lose(); //Goto void lose()
	}
	else if(mHealth <= 0 && pHealth > 0)
	{
		win(); //Goto void win()
	}
	else if(mHealth <= 0 && pHealth <= 0)
	{
		draw(); //Goto void draw()
	}
	else
	{
	battle(); //Goto void battle()
	}
	
}
int main ()
{
	battle(); //Goto void battle()
}

Please Feedback is Welcome :smile:
 
Skilled Illusionist
Joined
Jun 2, 2006
Messages
344
Reaction score
123
amazing, really cool man

im making something simillar in AS2.0 but ill make an interface

anyway.. really good :p
 
Skilled Illusionist
Joined
Apr 6, 2007
Messages
384
Reaction score
2
Suggestion: make the win/lose/draw functions either a macro (if your compiler supports it) or make them inline to avoid the function overhead.
 
Junior Spellweaver
Joined
Oct 22, 2005
Messages
168
Reaction score
1
Thank you for the comments...

@bone-you The reason I made them have their own functions is because if I ever expand the game it will be easier to add a exp/leveling system...
Example:
Code:
void win()
{
	cout << "You live to see another day.\n WIN\n";
	pExperience = mAttack + mDefense; //Just an example
}
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
Looks like the beginning of a nice funny game :p, nice idea :)
 
Newbie Spellweaver
Joined
Jan 25, 2007
Messages
44
Reaction score
0
i think its bad programed beacuse its better if you made it using oop and global variables it would be better or u can make it using
class::inheratance if you dont want to use global variables
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
Classes alright, but global variables? Are you sure?
 
Back
Top