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 here.
Please Feedback is Welcome :smile: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 ass 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() }



Reply With Quote

