Object oriented programming (I think?) - Can this be tidied up?

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.
Last edited on
#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
#include <string>

using namespace std;


/*
// 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();

//
const string npclist[] = {"Lion", "Bear", "Wolf","Skeleton", "Zombie", "Centipede","hill1","hill2","hill3","oasis1","oasis2","oasis3","desert1","desert2","desert3"};
const string npctypename[] = {"Humanoid", "Animal", "Magic wielder"};
const int npctype[] = {2, 2, 2, 1, 1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3}; //Type 1 is humanoid, 2 is animal, 3 is magic
const int npcatk[] = {1, 3, 2, 2, 3, 1, 1, 2, 3, 1, 2, 3, 1, 2, 3}; //Basic starting attack values
const int npcspeed[] = {1, 3, 2, 2, 3, 1, 1, 2, 3, 1, 2, 3, 1, 2, 3}; //Basic attack speed values
const int npcdefence[] = {1, 3, 2, 2, 3, 1, 1, 2, 3, 1, 2, 3, 1, 2, 3}; //Basic defence values
const int npchealth[] = {20, 45, 33, 15, 35, 5, 31, 32, 33, 21, 22, 23, 51, 52, 53}; //basic health values

//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();
};

npc::npc(){
level = 1;
health = 10;
atk = 1;
def = 1;
expvalue = 15;
name = "GENERIC NPC MADE";
}

//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));

cout << "Health done." << endl;
delay(1500);
atk = npcatk[numb] + (random(level)-1);

cout << "ATK done." << endl;
delay(1500);
def = npcdefence[numb] + (random(level)-1);

cout << "Defence done." << endl;
delay(1500);
expvalue = 25; //+ ((globalplevel - level) * 20);
name = npclist[numb];
nametype = npctype[numb];
delay(1500);
weaponnumb = random(3)-1;
}






int main(){
npc lib[3];
cout << "You are level " << globalplevel << endl;
for (int n = 0; n < 3; n++) {
int npcnum = random(15-1);
cout << "Initialising monster # " << n << endl;
lib[n] = npc(npcnum);
}
for (int n = 0; n < 3; n++){
cout << "Monster #" << n << " is a level : " << lib[n].level << " " << lib[n].name << "."<< endl;
cout << " This is a " << npctypename[lib[n].nametype-1] << " type creature and is using it's " << wpnname[lib[n].nametype-1][lib[n].weaponnumb] << " in combat" << endl;
cout << "Details are -- ATK:" << lib[n].atk << ", DEF:" << lib[n].def << ", Exp value : " << lib[n].expvalue << endl;
}
system("PAUSE");
return 0;
}

//
//
//
//FUNCTIONS


int random(int max){
int num;
srand ( time(NULL) );
num = rand() % max + 1;
return num;
}


void delay(int millisec)
{
Sleep(millisec);
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.
Topic archived. No new replies allowed.