I am working on a small console based ascii game in C++
Currently got the majority of the structure working but I am trying to improve it by using classes...
Below is a test I have done up to work through the classes concepts. I am sure someone that knows what they are doing will find this extremely messy - Can you give me pointers on how to tidy it up a bit - especially with the arrays etc.
Should I make the class have a lot more variables?
Or make seperate derived classes (in this eg, maybe an animal, humanoid and magic derived class from npc?)
Also - ideally I would like a lot of the standard info (weapons info (name, speed, atk etc.) armour, class types etc...) to be contained in a seperate file to the main program - is it worth either writing a new C++ file with the data or a text file to read the data from? Or just cop it as it is?
Anything at all will be much appreciated!
Will include the code in a seperate post.
EDIT: The npc names arent all proper names - I have divided npcs into different areas of the map (oasis, wood, desert etc.) and to get it compiled and tested I just used oasis1 etc. for the first oasis npc)
If anyone is interested/willing to look at the main game part I am happy to pass that code on too!
EDIT2: Apologies for not specifying - I am using Windows Vista, Dev-x++ and it is pure console.
/*
// Test file for working objects in - intially player and NPC
//
*/
int random(int max);
void gotoxy(int x, int y);
void delay(int x);
void clrscr();
//WPN AVAIL is for NPC types - Type 1 is humanoid, type 2 is biter, type 3 is magic?
const string wpnname[3][3]={{"Club", "Sword", "Axe"}, //Humanoid weapons - wpnname[0][x]
{"Claws", "Body", "Teeth"}, //Animal weapons - weaponname[1][x]
{"Ice magic", "Lightning magic", "Fire magic"}}; //Magic weapons - weaponname[2][x]
//
const int wpnatk[] = {1, 3, 4}; //NOT USED YET
const int wpnspeed[] = {10, 7, 10}; //NOT USED YET
const string armour[] = {"Cloth", "Leather", "Mail", "Plate"}; //NOT USED YET - Similar to weapons though (different types for different classes
const int armourdef[] = {1, 5, 10, 20};//NOT USED YET
//OBJECTS...
int globalplevel = 5; //Player level (Used in main application as global )
class npc{
public:
int level, health, atk, def, expvalue, weaponnumb;
string name;
int nametype;
npc();
npc(int numb);
int getlevel();
};
//Using delay here to ensure random number generator can generate a new rand #
npc::npc(int numb){
level = globalplevel + (random(3)-2);
cout << "Level done." << endl;
delay(1500);
health = npchealth[numb] + (random(4 * level) - (2 * level));
I have not looked deeply inside the code but here are some suggestions to start:
1. No globals should be there. All data can be part of some class. If the class doesn't exist, make one.
2. If any set of data can be combined and which is related in a class, remove that from the class and make a new class for that.
eg: a bar graph class which you will make would be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class graph //just an example
{
graph name
graph background color
bar color
bar height
bar width
max x scale
max y scale
x scale title
y scale title
x scale intervals
y scale intervals
legend properties list //something something
}; //there will be many more
Now this could be made better like this, by combining similar things and putting them in a different class
class graph
{
graph name
graph background color
};
class bar
{
bar color
bar height
bar width
};
class scale
{
max x scale
max y scale
x scale title
y scale title
x scale intervals
y scale intervals
};
class legend
{
legend properties list //something something
};
Now these all classes can have a composition or aggregation relation with the graph class. This makes it clean and easy to change.
3. than for each data, make methods so that the user can change them or get their values.
start with this and you will be able to see the next steps yourself.