anyone want to analyze this

Started by
2 comments, last by jgrudier 14 years, 5 months ago
I am trying to learn c++ and have gotten pretty far I think. I was wondering if anyone would analyze this for potential errors I have missed or easier ways to do things that I should look into before I go any farther with this. It is a lot of code so sorry if there is a better way to post it. library.h

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "item.h"
#include "weapon.h"
#include "armor.h"
#include "entity.h"
#include "monster.h"
#include "player.h"
#include "globals.h"
#include <cstdlib>
using namespace std;

globals.h

#ifndef GLOBAL_DEFINE
#define GLOBAL_DEFINE

#define NORTH (answer[0] == 'n') 
#define SOUTH (answer[0] == 's' && answer[1] != 't')
#define EAST (answer[0] == 'e')
#define WEST (answer[0] == 'w')
#define STATS (answer[0] == 's' && answer[1] == 't') 
#define QUIT  (answer[0] == 'q' && answer[1] == 'u' && answer[2] == 'i' && answer[3] == 't') 
#define ATTACK (answer[0] == 'a')
#define CAST (answer[0] == 'c')
#define POTION (answer[0] == 'p')
bool fountain(PLAYER*);
bool quitgame();
bool nof(PLAYER*);
bool eof(PLAYER*);
bool sof(PLAYER*);
bool wof(PLAYER*);
void whoru();
bool stats();
bool Summonportal();
bool battle(PLAYER* player,MONSTER* list,int count);
#endif

item.h

class ITEM
{private:
    char name[32];
    int price;
public:
    ITEM()
    {
        price = 0;
    }
    void Setname(char *newname)
       {
            strcpy(name,newname);
       }
    char* Getname()
       {
            return name;
       }
    int Getprice()
       {
            return price; 
       }
     void Setprice(int amount)
       {
            price = amount;       
       }
};

player.h


class PLAYER:   public ENTITY
{private:
    int strength;
    int agility;
    int vitality;
    int intelligence;
    int dexterity;
    int luck;
    int level;
    WEAPON weapon;
    ARMOR armor;
public:
    PLAYER();
    void Setstr(int newstr);
    int Getstr();
    void Setagi(int newagi);
    int Getagi();
    void Setvit(int newvit);
    int Getvit();
    void Setint(int newint);
    int Getint();
    void Setdex(int newdex);
    int Getdex();
    void Setluck(int newluck);
    int Getluck();
    void Setlevel(int newlevel);
    int Getlevel();
    WEAPON* Getweapon();
    void Setweapon(WEAPON weapon);
    ARMOR* Getarmor();
    void Setarmor(ARMOR armor);
};

monster.h

class MONSTER:   public ENTITY
{private:
    int attack;
    int armor;
      
public:  
    MONSTER();
    MONSTER(MONSTER*);
    void Setattack(int amount);
    int Getattack();
    void Setarmor(int amount);
    int Getarmor();    
};

weapon.h

class WEAPON: public ITEM
{private:
    char name[25];
    int damage;
public:
       
    WEAPON()
        {
            damage = 0;
        }
    bool WEAPON::Setname(char *newname)
        {
            if (strlen(newname) == 0)
            return false;
            if (strlen(newname) > 25)
            return false;
            else
            strcpy(name,newname);
            return true;
       }
    char* Getname()
       {
            return name;
       }
       
    int Getdamage()
       {
            return damage;
       }
     void Setdamage(int amount)
       {
            damage = amount;
       }
};

armor.h

class ARMOR: public ITEM
{private:
    char name[25];
    int ap;
        
public:
    ARMOR()
        {
            ap = 0;
        }     
    bool ARMOR::Setname(char *newname)
        {
            if (strlen(newname) == 0)
            return false;
            if (strlen(newname) > 25)
            return false;
            else
            strcpy(name,newname);
            return true;
       }
    char* Getname()
       {
            return name;
       }
    int Getap()
        {
            return ap;
        }
    void Setap(int amount)
        {
            ap = amount;
        }
};

entity.h

class ENTITY
{private:
    char name[25];
    int gold;
    int experience;
    int health;
    int maxhealth;
    int maxmana;
    int mana;
    
public:
    bool Setname(char *newname);
    char* Getname();
    void Setgold(int newgold);
    void Addgold(int amount);
    void Spendgold(int amount);
    int Getgold();
    void Setexp(int newexp);
    void Addexp(int amount);
    int Getexp();
    void Sethealth(int newhealth);
    void Addhealth(int amount);
    void Losehealth(int amount);
    int Gethealth();
    void Setmaxhealth(int newmaxhealth);
    int Getmaxhealth();
    void Setmana(int amount);
    void Addmana(int amount);
    void Losemana(int amount);
    int Getmana();
    void Setmaxmana(int newmaxmana);
    int Getmaxmana();   
};

monster.cpp

#include "library.h"
MONSTER::MONSTER()
{}
MONSTER::MONSTER(MONSTER* monster)
    {
        Setname(monster->Getname());
        Setmaxhealth(monster->Getmaxhealth());
        Sethealth(monster->Gethealth());
        Setarmor(monster->Getarmor());
        Setgold(monster->Getgold());
        Setexp(monster->Getexp());
        Setattack(monster->Getattack());    
    }

    void MONSTER::Setattack(int amount)
    {
        attack = amount;    
    }
    int MONSTER::Getattack()
    {
        return attack;    
    }
    
    void MONSTER::Setarmor(int amount)
    {
        armor = amount;    
    }
    int MONSTER::Getarmor()
    {
        return armor;    
    }

player.cpp

#include "library.h"

PLAYER::PLAYER()
{      
    char name[25];
    strength = 15;
    agility = 15;
    vitality = 15;
    intelligence = 15;
    dexterity = 15;
    luck = 15;
    Setgold(0);
    Setexp(0);
    level = 1;
    Sethealth(50);
    Setmaxhealth(50);
    Setmana(50);
    Setmaxmana(50);
       
    weapon.Setname("rusty dagger");
    weapon.Getname();
    weapon.Setdamage(2);
    weapon.Setprice(0);
    
    armor.Setname("tathered cotton shirt");
    armor.Getname();
    armor.Setap(0);
    armor.Setprice(0);
}

int PLAYER::Getagi()
{   
    return agility;     
}
int PLAYER::Getdex()
{
    return dexterity;
}
int PLAYER::Getint()
{
    return intelligence;
}
int PLAYER::Getluck()
{
    return luck;
}
int PLAYER::Getstr()
{
    return strength;
}
int PLAYER::Getvit()
{   
    return vitality;
}
void PLAYER::Setagi(int newagi)
{
    agility = newagi;
}
void PLAYER::Setdex(int newdex)
{
    dexterity = newdex;
}
void PLAYER::Setint(int newint)
{
    intelligence = newint;
}
void PLAYER::Setluck(int newluck)
{
    luck = newluck;     
}
void PLAYER::Setstr(int newstr)
{
    strength = newstr;
}
void PLAYER::Setvit(int newvit)
{
    vitality = newvit;
}
void PLAYER::Setlevel(int newlevel)
{
    level = newlevel;
}
int PLAYER::Getlevel()
{
    return level;
}
WEAPON* PLAYER::Getweapon()
{
    return &weapon;
}
void PLAYER::Setweapon(WEAPON newweapon)
{
    weapon = newweapon;
}
ARMOR* PLAYER::Getarmor()
{
    return &armor;     
}
void PLAYER::Setarmor(ARMOR newarmor)
{
    armor = newarmor;
}

entity.cpp

#include "library.h"

void ENTITY::Addexp(int amount)
{
    Setexp(experience + amount);     
}
void ENTITY::Addgold(int amount)
{
    Setgold(gold + amount);
}
void ENTITY::Addhealth(int amount)
{
    Sethealth(health + amount);   
} 
void ENTITY::Addmana(int amount)
{
    Setmana(mana + amount);
}
int ENTITY::Getexp()
{
    return experience;
}
int ENTITY::Getgold()
{
    return gold;
}
int ENTITY::Gethealth()
{
    return health;
}
int ENTITY::Getmana()
{
    return mana;
}
int ENTITY::Getmaxhealth()
{
    return maxhealth;
}
int ENTITY::Getmaxmana()
{
    return maxmana;
}
void ENTITY::Losehealth(int amount)
{
    Sethealth(health - amount); 
}
void ENTITY::Losemana(int amount)
{
    Setmana(mana - amount);
}
void ENTITY::Setexp(int newexp)
{
    experience = newexp;
}
void ENTITY::Setgold(int newgold)
{
    gold = newgold;
}
void ENTITY::Spendgold(int amount)
{
    Setgold (gold - amount);
}
void ENTITY::Sethealth(int newhealth)
{
    health = newhealth;
     
    if (health > maxhealth)
    health = maxhealth;
     
    if (health < 0)
    health = 0;
}
void ENTITY::Setmana(int newmana)
{
    mana = newmana;
     
    if (mana > maxmana)
    mana = maxmana;
     
    if (mana < 0)
    mana = 0;
}
void ENTITY::Setmaxhealth(int newmaxhealth)
{
    maxhealth = newmaxhealth;
}
void ENTITY::Setmaxmana(int newmaxmana)
{
    maxmana = newmaxmana;
}
bool ENTITY::Setname(char *newname)
{   if (strlen(newname) == 0)
    return false;
    if (strlen(newname) > 25)
    return false;
    else
    strcpy(name,newname);
    return true;
}
char* ENTITY::Getname()
{
    return name;
}

main.cpp

#include "library.h"
PLAYER* player;
int main()

    {cout << "\n\n\nWelcome to my game!\n\n";
        whoru();  
        fountain(player);
   
    cout << "Thank you for playing\n\n";    
    system("PAUSE");
    return EXIT_SUCCESS;
}


void whoru()
{
    PLAYER player;
    char temp[25];    
    cout<<"\nwhat is your name?\n\n";
    cin>> temp;
    cout<<"\nHello ";
    player.Setname(temp);
    cout << player.Getname() <<"!" << endl; 
}   


bool stats()

{   
    PLAYER player;
    WEAPON weapon;
    ARMOR armor;
    cout << "\n\nHere are your current stats:\n\n";
    cout << "Strength: " << player.Getstr() << endl;
    cout << "Agility: " << player.Getagi() << endl; 
    cout << "Vitality: " << player.Getvit() << endl;
    cout << "Intelligence: " << player.Getint() << endl;
    cout << "Dexerity: " << player.Getdex() << endl;
    cout << "Luck: " << player.Getluck() << endl;
    cout << "Experience: " << player.Getexp() << endl;
    cout << "Level: " << player.Getlevel() << endl;
    cout << "HP: " << player.Gethealth() << "/" << player.Getmaxhealth() << endl;
    cout << "MP: " << player.Getmana() << "/" << player.Getmaxmana()  << endl;
    weapon.Setname("rusty dagger");
    cout << "You are wielding a " << weapon.Getname() << endl;
    armor.Setname("tathered cotton shirt");
    cout << "You are wearing a " << armor.Getname() << endl << endl;
    return true;
}

fountain.cpp

#include "library.h"

bool fountain(PLAYER* player)
{
    char answer[5];
    while (true)
        {cout << "\nFountain\n";
        cout << "You are at the town fountain.  This is the central hub where all players gather.";
        cout << "Travelers weary from their journeys take long drinks from the fountain and then ";
        cout << "fill their canteens.\n";
        cout << "There are exits are North, East, South, and West." << endl;
        cout << "What would you like to do?\n\n";
        cin >> answer;
        if (NORTH || EAST || SOUTH || WEST || STATS)
        break;
        else if  (QUIT) 
            {char quitans[4];
                cout << "\nAre you sure?\n\n";
                cin >> quitans;
                    if (quitans[0] == 'y') 
                    {cout << "\nGoodbye!\n\n";
                    break;}
                    else if (quitans[0] == 'n') cout << "\nokay\n\n";
            }
        }
     
    {if (NORTH) nof(player);
     if (EAST) eof(player);
     if (SOUTH) sof(player);
     if (WEST) wof(player);
     if (STATS)
        {
        stats();
        fountain(player);
        }
    }  
return true;}

// functions

bool nof(PLAYER* player)
    {char answer[5];
    while (true)
        {cout<< "\nNorth of Fountain"<<endl;
        cout << "You are standing just north of the town fountain.";
        cout << "  To the south you can hear\nthe noise of a busy town center.\n";
        cout << "Exits are North, East, South, and West..\nWhat would you like to do?\n\n";     
        cin >> answer;
        cout << endl;
        if (NORTH || EAST || SOUTH || WEST || STATS)
        break;
        else if  (QUIT) 
            {char quitans[4];
            cout << "\nAre you sure?\n\n";
            cin >> quitans;
                if (quitans[0] == 'y') 
                {cout << "\nGoodbye!\n\n";
                break;}
                else if (quitans[0] == 'n') cout << "\nokay\n";
                }
            }
          
    {//if (NORTH) ang(player); 
     //if (EAST) neof(player);
     if (SOUTH) fountain(player);
     //if (WEST) nwof(player);
     if (STATS) {stats();
                nof(player);
                }
            
    }
return true;
}

bool eof(PLAYER* player)
    {char answer[5];
    while(true)
        {cout<<"\nEast of Fountain" << endl;
        cout << "You are standing just east of the town fountain.";
        cout << "  To the west you can hear\nthe noise of a busy town center.\n";
        cout << "Exits are Exits are North, East, South, and West.\nWhat would you like to do?\n\n";
        cin>>answer;
        if (NORTH || EAST || SOUTH || WEST || STATS)
        break;
        else if (QUIT)
            {char quitans[4];
            cout << "\nAre you sure?\n\n";
            cin >> quitans;
                if (quitans[0] == 'y') 
                {cout << "\nGoodbye!\n\n";
                break;}
                else if (quitans[0] == 'n') cout << "\nokay\n";
                }  
            }  
       
    {//if (NORTH) neof(player);
     if (WEST) fountain(player);
     //if (SOUTH) seof(player);
     //if (EAST) slumsw(player);
     if (STATS) {stats();
                 eof(player);
                }
     if (SOUTH) sof(player);                                    
    }
return true; 
}
   
bool sof(PLAYER* player)
    {char answer[5];
    while(true)
        {cout<<"\nSouth of Fountain" << endl;
        cout << "You are standing just south of the town fountain.";
        cout << "  To the north you can hear thenoise of weary travelers.";
        cout << "  To the south you can hear the noise of a busy town\ncenter.\n";
        cout << "Exits are North, East, South, and West.\nWhat would you like to do?\n\n";
        cin>>answer;
        if (NORTH || SOUTH || STATS)
        break;
        else if (QUIT)
            {char quitans[4];
            cout << "\nAre you sure?\n\n";
            cin >> quitans;
                if (quitans[0] == 'y') 
                {cout << "\nGoodbye!\n\n";
                break;}
                else if (quitans[0] == 'n') cout << "\nokay\n";
                }  
            }  
       
    {if (NORTH) fountain(player);
     //if (EAST) seof(player);
     //if (SOUTH) rsg(player);
     //if (WEST) swof(player):
     if (STATS) {stats();
                 sof(player);
                }
                                         
    }
return true; 
}

bool wof(PLAYER* player)
    {char answer[5];
    while(true)
        {cout<<"\nWest of Fountain" << endl;
        cout << "You are standing just west of the town fountain.";
        cout << "  To the east you can hear\nthe noise of a busy town center.\n";
        cout << "Exits are North, East, South, and West.\nWhat would you like to do?\n\n";
        cin>>answer;
        if (NORTH || EAST || SOUTH || WEST || STATS)
        break;
        else if (QUIT)
            {char quitans[4];
            cout << "\nAre you sure?\n\n";
            cin >> quitans;
                if (quitans[0] == 'y') 
                {cout << "\nGoodbye!\n\n";
                break;}
                else if (quitans[0] == 'n') cout << "\nokay\n";
                }  
            }  
       
            {//if (NORTH) nwof(player);
             if (EAST) fountain(player);
             //if (SOUTH) swof(player);
             if (WEST) Summonportal();
             if (STATS) {stats();
                         wof( player);
                        }
                                                 
            }
return true; 
}

summonportal.cpp

#include "library.h"

bool Summonportal()
{PLAYER player;
    MONSTER wolf;
    wolf.Setname("wolf");
    wolf.Setmaxhealth(50);
    wolf.Sethealth(50);
    wolf.Setarmor(0);
    wolf.Setgold(2);
    wolf.Setexp(1);
    wolf.Setattack(5);
   
   
    player.Getname();
    player.Setstr(15);
    player.Setgold(0);
    player.Setexp(0);
    player.Setmaxhealth(50);
    player.Sethealth(50);
    
    battle(&player,&wolf,1);
    
    
    //MONSTER *monsters;
    //monsters = new MONSTER[2];
    //monsters[0] = wolf;
    //monsters[1] = wolf;
    //if(!battle(player,monsters,2))
//delete monsters;
return true;
}

battle.cpp

#include "library.h"

void Listmonster(MONSTER* list, int count);
bool Attackmonster(PLAYER* player, MONSTER* monster);
bool Monsterattack(MONSTER* monster, PLAYER* player);

bool battle(PLAYER* player,MONSTER* list,int count)
{cout << "you grab your dagger\n";
    while (player->Gethealth() > 0)
    {
        if (count == 1) 
        {cout << "The " << list->Getname() << " stares at you!\n";
        break;}
        else  Listmonster(list,count);
        break;
    }    
    cout <<  player->Getname() << endl;
    cout << "HP: " << player->Gethealth() << "/" << player->Getmaxhealth() << endl;
    cout << "MP: " << player->Getmana() << "/" << player->Getmaxmana()  << endl;
    char answer[6];
    do 
    {if (player->Gethealth() < 0)
    {cout << "you died\n";
    break;
    }
    else if (list->Gethealth() <= 0)
    {cout << "you win!\n";
    break;}
    
     
    
    {cin >> answer;
        {if (ATTACK)
            {if (count == 1)
                {Attackmonster(player,list);
                Monsterattack(list,player);
                
                }
            else 
                {int target = 0;
                while(true)
                    {
                    Listmonster(list,count);
                    cout << "which monster\n";
                    cin>> target;
                    }
                }
            }
         if (CAST)
            {
            }
        if (POTION)
            {
            }
       
        }
}   
}
while (true);
fountain(player);
}
void Listmonster(MONSTER* list, int count)
{
    cout << "Enemies\n";
        for (int loopname = 0; loopname < count; loopname++)
        {
            if (list[loopname].Gethealth() > 0)
            {
                cout << loopname+1 << ": " << list[loopname].Getname()<<endl;
            }
        }
}
bool Attackmonster(PLAYER* player, MONSTER* monster)
{
    int pstr = player->Getstr();
    if (pstr < 25)
    {
        int random = (rand()%(25 - pstr)) +1;
        if (random == 1)
        {
            cout << "you miss\n";    
            return false;
        }    
    }
    int damagelow = ((player->Getweapon()->Getdamage() + pstr) /2);
    int damagehigh = (player->Getweapon()->Getdamage() + pstr);
    int damagegiven = (rand()%(damagehigh-damagelow)) + damagelow;
    int totaldamage = damagegiven - monster->Getarmor();
    cout << "you do " << totaldamage << " damage.\n";
    monster->Losehealth(totaldamage);  
    
    return true;
}
bool Monsterattack(MONSTER* monster, PLAYER* player)
{
    int damagelow = (monster->Getattack()-(monster->Getattack()/2));
    int damagehigh = monster->Getattack();
    int damagegiven = (rand()%(damagehigh-damagelow)) + damagelow;
    int totaldamage = damagegiven - player->Getarmor()->Getap();
    cout << monster->Getname() << " does " << totaldamage << "damage.\n";
    player->Losehealth(totaldamage);
    return true;  
}

Advertisement
If I may, here's a list of classic mistakes you made:

1) The Object Oriented syndrome. Go to :

http://www.insomniacgames.com/blogcast/blog/mike_acton/1500756

and read number 2.

2) you use char* for strings. Worst still, you use a fixed size char array! Use a string object.

3) Where's the const-correctness?

4) Remember that Object-oriented isnt about hiding data behind gets/sets everywhere. Its about *behavior*. Encapsulating class behavior, inheritance of class behavior, etc.
I haven't read all the code, but here are a few pieces of advices from what I've seen:
* All your header files should have include guards.
* Having a master header that includes every other header obscures the true dependencies in your code and forces recompilation of everything if any header file changes, so I would not recommend it.
* Don't use macros. For simple tests like those in globals.h, use functions. Relying in the local variable's name being `answer' is particularly awful.
* Use `std::string' instead of things like `char[32]'. For instance, your program has potential buffer overflows.
* You should learn to use `const'.
* Defining trivial setters and getters for all the private members in a class is not very different from making them public. Perhaps your classes should have more meaningful interfaces. Inside the class itself, there is no need to use getters and setters (see `MONSTER::MONSTER(MONSTER *)').
* You are using pointers in many places where a reference would be more idiomatic (again, see `MONSTER::MONSTER(MONSTER *)').
* Your function `stats()' makes no sense. If gives you the stats of a default-constructed `Player'. What's the point?
Thank you both for the advice. I will look into all of this. The Stat() function was ideally trying to get the stats of the current player not just a default-constructed `Player'. Any good references for pointers and references I admit I am still really confused by them.

This topic is closed to new replies.

Advertisement