more problems with code(still need help!)

Started by
13 comments, last by gohacker82 19 years, 8 months ago
Just a thought, you might want to make your code a bit easier to use. I see you try and set up a while loop for each different weapon. If you let the user choose from the beinging and set the weapon once, all you need is one game loop (while statment). Also you can reuse your battle function without making and ebattle(), battle2(), etc. Try something like this...

#include <stdio.h>#include <iostream>#include <fstream>#include <string>#include <cstdlib>#include <cmath>using namespace std;//Declare global variablesint health	= 100;  //Player healthint enemyh	= 100;  //Enemy healthint weapon;			//Current weaponint ch1		= 1;	//weapon choice 1int ch2		= 2;	//weapon choice 2int ch3		= 3;	//weapon choice 3int heal;			//heal the characterint choice;			//turn choiceint att		= 1;	//Player attacksint hel		= 2;	//healint weaponAttack = 0;  //Bonus from weaponint healBonus	 = 0;  //Bonus from weaponint enemeyAttack = 0;  //Bonus from weapon(enemey)bool loop = true;//Prototypesint attackPoints();int healPoints();//Player battle scriptvoid playerBattle() {               	cout<<"It is your turn. what do you do?(1 to attack and 2 to heal)"<< endl;	cin>> choice;		if (choice == att) 	{		cout << "You have attacked him!" << endl;		enemyh -= attackPoints() + weaponAttack;		if(enemyh <= 0)		{ cout << endl << "You have slain your enimies!" << endl; loop=false;}		cout << "He now has " << enemyh << " health left" << endl;	}else if (choice == hel){		cout << "You have healed your-self!" << endl;		health += healPoints() + healBonus;		cout <<"You now have "<< health<<" health left"<< endl;	}}//Enemey battle scriptvoid enemeyBattle() {   	cout << "It is his turn to attack! He has struck you with his mighty weapon!" << endl;	health -= attackPoints() + enemeyAttack;	if( health <= 0)	{ cout << endl << "You have been defeated!" << endl; loop=false;}	cout<<" You now have only "<< health<< " health left!" << endl;}//Random attack numberint attackPoints(){	return int(8.0 * rand()/(RAND_MAX+1.0));	}//Random heal amountint healPoints(){	return int(15.0 * rand()/(RAND_MAX+1.0));}int main(int argc, char *argv[]){	cout << "Armor core arenas" << endl;	cout << endl;	cout << "Welcome to armor core arenas! The game where you fight against your"<< endl;	cout << "opponent in an arena with a weapon of your choice."<< endl;	cout << endl;	cout << endl;	cout << endl;	cout << "Please choose your weapon"<< endl;	cout << "1.  axe(+2 to attack, +5 to heal)"<< endl;	cout << "2.  sword(+4 to attack, +0 to heal)"<< endl;	cout << "3.  staff(magic)(+0 to attack, +15 to heal)"<< endl;	cin  >> weapon;	if (weapon == ch1) 	{		weaponAttack = 2;		healBonus	 = 5;		enemeyAttack = 2;		cout << "You have chosen the axe. The battle will now comence!"<< endl;		cout << "your opponet will be the crusher. he is a large opponet who also weilds an axe"<< endl;		cout << endl;	} else if(weapon == ch2){		weaponAttack = 4;		healBonus	 = 0;		enemeyAttack = 0;		cout << "You have chosen the sword. a mighty weapon that can quickly slice through opponets. The battle will now commence!"<< endl;		cout << "Your opponet will be the hunter. A swift enemy who uses his bow and arrow to a great advantage"<< endl;		cout << endl;	} else if(weapon == ch3){		weaponAttack = 0;		healBonus	 = 15;		enemeyAttack = 2;		cout << "You have chosen the magic staff. a mystical item that has extrodinary powers. The battle will now commence!"<< endl;		cout << "Your opponet is the wizard ar naria. a powerful wizard with unknown powers."<< endl;		cout << endl;	} else {		cout << "You have selected an invalid item" <<endl;	}	while(loop)	{		if(health > 0)			playerBattle();						if(enemyh > 0)			enemeyBattle();	}	return 0;} 


it is a bit more organized and less repeted code. I threw in some random number for attacks and used your base number to get the weapon and heal bonus. Nothing perfect, but at least a start in the right direction.

dimebag
Advertisement
ok, thanks for everyones help. I dont understnad what you mean by unbalance {}, but I am just going to use parts of the code the annonymous poster supplied, to get myslef on the right track.
______________________________My website: Quest Networks
Unbalanced {}: Brackets (and parentheses etc.) need to be balanced. This means that for every { there should be a }.
So if you have
if (weapon == ch2) {

you need to have an ending }..
Brackets are used to mark blocks, { marks the beginning and } marks the end.
Sorry for the Anonymous post, forgot username and pass.

I threw that together, you could probably split the weapons into a switch statement, and make the player, enemey a little more robust, but I figured ti would give you an idea of how reuseablity is your friend. Why have 3 different functions that do the same thing with only one variable diference? Instead combine the functions, and change the variable. If you have any more questions, feel free to e-mail me. villagetech@sterlinghousing.com.

dimebag

Try to use #define

int wp1 = 1; // this is not effective!!

try this

#define wp1 1

it's the same like : int wp1 = 1; //but more efficient..

and try to give a tab space if you make a new block (scope)

example:

for (int i=0;i<10;i++){        if(i==9)        {                cout<<"Finish"<<endl;        }        else        {                cout<<"item "<< i << endl;        }}[\source]it's easy to read..

This topic is closed to new replies.

Advertisement