Original Post
Well, I''ve been posting here quite a bit recently concerning all the problems I''ve been having with my game (hell, at this point, it seems as though YOU ALL have been writing the game and not me...I''ve been getting so much help). But anyway, thank God all my previous problems have been solved (so far). Now, the reason I''m writing this post is not so much because of a problem, but because I know there are ways to better optimize the code I''m currently working with. Ok, the idea is that the user is at the weapons menu screen and to choose a weapon they simply enter the number corresponding to the weapon they want - simple enough, AND I can do it the long, hard way.
switch(choice)
case ''1'':
weapon * TheWeapon = new weapon("NAME OF WEAPON 1",5,100);
break;
case ''2'':
weapon * TheWeapon = new weapon("NAME OF WEAPON 2",8,200);
break;
// etc etc.
Talk about redundancy! So, at this point I''ve gotten the code a little more simplified:
char menu_choice;
char convert[1];
int weapon_select;
int done = 0;
switch(toupper(menu_choice))
{
case ''1'':
case ''2'':
case ''3'':
case ''4'':
case ''5'':
case ''6'':
case ''7'':
case ''8'':
{
done = 1;
*convert = menu_choice; // if 1-8...weapon wanted
weapon_select = atoi(convert); // see notes at bottom
weapon * pWeapon = new weapon(NAME,DAMAGE,50);
break;
}
case ''S'':
{
ThePlayer->show_player_stats();
TheWeapon->show_weapon_stats();
break;
}
case ''Q'':
{
done = 1;
break;
}
default:
{
cout << endl << invalid_option;
getch();
break;
}
}
As you can see, it has less redundant code, but now I don''t know how to get it to recognize exactly which weapon they want. It only knows "Hey, they entered a number, they must want a weapon." Unfortunately, that''s all it knows. The reason I converted the menu_choice to an integer (weapon_select) was so I could test it to see which weapon they wanted, but then that defeats the whole purpose - I would have to write another long, redundant switch statement for that. The only reason I really care about doing this is because some guy chewed my butt out one time when I used a switch statement like the one at the top (too redundant). So, to make that jerkoff happy I decided I''d better learn how to do this the right way.
PS: I''ve tried some stuff already, but it always felt like I was heading right for a brick wall. For example, I tried making a weapon codename and damage codename generator. It would do something like append their menu_choice to ''WC'', creating the string ''WC1'' if they wanted weapon #1, ''WC2'' for weapon #2, etc. Then, I had a list of defines like:
#define WC1 "Colt .45 Pistol"
#define WC2 "Shotgun"
However, I realized that it wouldn''t go and substitute "Colt" for WC1 because WC1 was ALREADY a string. Talk about disappointment. Anyway, that''s about it. Below is some of the more relevant code to my program if you need it to help solve the problem. Enjoy.
class weapon
{
public:
weapon(string, int, int);
~weapon(void);
void show_weapon_stats(void);
void fire_weapon(void);
void change_fire_method(int);
void reload_weapon(int);
void buy_ammo(int);
friend player;
private:
string weapon_name;
int damage;
int ammo;
};
weapon::weapon(string w_name, int d, int a)
{
this->weapon_name = w_name;
this->damage = d;
this->ammo = a;
}