Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am wondering about the best way to save on RAM/memory when there are many required arrays (I plan to have many more NPCs and various other arrays with text and items). And I am wondering what I can improve on so far.

  #include <iostream>
  #include <cstring>

  #define NUM_NPC 10       // Number of NPC players

  #define DETAILS_HEARTS 0 // Heart Position
  #define DETAILS_BDMON  1 // Birthday Month Position
  #define DETAILS_BDDAY  2 // Birthday Day Position
  #define DETAILS_GENDER 3 // NPC Gender

  using namespace std;

  class npcPlayers {
     public:
        int getHearts(int playerID);
        int getItem(int playerID, int itemID);
        int getBirthdayMonth(int playerID);
        int getBirthdayDay(int playerID);
        char *getName(int playerID);
        npcPlayers();
     private:
        int npcDetails[NUM_NPC][10];
        char npcNames[NUM_NPC][20];
        int updateHearts(int PlayerID,int value);
  };

  npcPlayers::npcPlayers() {
     npcNames = {"Ace","Amber","Benny","Ethan","Michael","Jay","William","Mia","Chloe","Emily"};
     npcDetails = {
        {0,3,5,0},    // Ace
        {0,9,20,1},   // Amber
        {0,1,10,0},   // Bennny
        {0,12,30,0},  // Ethan
        {0,2,17,0},   // Michael
        {0,6,10,0},   // Jay
        {0,8,9,0},    // William
        {0,4,1,1},    // Mia
        {0,5,13,1},   // Chloe
        {0,8,5,1}     // Emily
     };
  }

  int npcPlayers::getHearts(int playerID) {
     return npcDetails[playerID][DETAILS_HEARTS];
  }
  int npcPlayers::getItem(int playerID, int itemID) {

  }
  int npcPlayers::getBirthdayDay(int playerID) {
     return npcDetails[playerID][DETAILS_BDDAY];
  }
  int npcPlayers::getBirthdayMonth(int playerID) {
     return npcDetails[playerID][DETAILS_BDMON];
  }
  char *npcPlayers::getName(int playerID) {
     return npcNames[playerID];
  }

  int main() {
     npcPlayers npc;
     cout << "Player 1 Details:" << endl;
     cout << "Name: " << npc.getName(1) << endl;
     cout << "Hearts: " << npc.getHearts(1) << endl;
     cout << "Birthday: " << npc.getBirthdayMonth(1) << "/" << npc.getBirthdayDay(1) << endl;
     return 0;
  }
share|improve this question
1  
Unfortunately we could spend a whole book on what you need to know. But a good starting point is to understand OO programming (rather than worry about memory). If you want to write idiomatic C++ you need to start by designing objects (Like A Player) and the actions that can be performed on that player (the interface). –  Loki Astari Mar 29 '12 at 22:12
1  
I agree. It is impossible to answer, so many things to say... Read books, there are a plenty of free C++ reading. –  Michael Mar 30 '12 at 11:57

2 Answers 2

  1. C++ is statically typed programming language. Due to this overhead for allocation of class members is very small.

  2. Using of global variables is not a good idea. Such vars like NUM_NPC makes your application non-scalable.

  3. Each domain object (like a player or an item) must be described by a same-named class.

#include <iostream>
#include <string>
#include <vector>

typedef int Hearts;
typedef bool Gender;

struct Date
{
      Date(int month,int day) : month(month), day(day) {}
      int month;
      int day;
};

class Item
{
    // TODO: implement
};

class Player
{
public:
      Player(std::string name, Hearts hearts, Date birthday, Gender gender)
            : name(name), hearts(hearts), birthday(birthday), gender(gender) {}

      Hearts getHearts() const
      {
            return hearts;
      }

      const std::vector<Item>& getItems()
      {
            return items;
      }

      Date getBirthday() const
      {
            return birthday;
      }

      std::string getName() const
      {
            return name;
      }
private:
      std::vector<Item> items;
      Hearts hearts;
      Date birthday;
      std::string name;
      Gender gender;
};

int main()
{
      using namespace std;

      vector<Player> players;
      players.push_back(Player("Ace", 0, Date(3,5), 0));
      players.push_back(Player("Amber", 0, Date(9,20), 1));
      players.push_back(Player("Benny", 0, Date(1,10), 0));
      players.push_back(Player("Ethan", 0, Date(12,30), 0));

      Player& p = players[1];
      cout << "Player 1 Details:" << endl;
      cout << "Name: " << p.getName() << endl;
      cout << "Hearts: " << p.getHearts() << endl;
      cout << "Birthday: " << p.getBirthday().month << "/" << p.getBirthday().day << endl;
      cout << "Item count: " << p.getItems().size() << endl;
}
share|improve this answer
    
Thank You. I will definitely look into this. –  David Apr 14 '12 at 20:03

There's little need for macros (#defines) in C++, unlike in C:

#define NUM_NPC 10       // Number of NPC players

#define DETAILS_HEARTS 0 // Heart Position
#define DETAILS_BDMON  1 // Birthday Month Position
#define DETAILS_BDDAY  2 // Birthday Day Position
#define DETAILS_GENDER 3 // NPC Gender

The first macro should instead be a constant, using the const keyword:

const int NUM_NPC = 10;

The rest of them can be put into an enum:

enum Detail { HEARTS, BDMON, BDDAY, GENDER };

Each respective value inside still ranges from 0 to 3.

Inside the class, you're using accessors ("getters"), which is bad for encapsulation. Plus, you're only displaying them in the calling code, so you don't even need the values themselves.

Not only that, but you're not constructing the npcPlayers object correctly. You're invoking its default constructor, then passing something to each accessor (which appears to be playerID).

Here's now the object should be constructed:

npcPlayers nps(1);

In your class definition, the constructor should be overloaded as such:

npcPlayers(int playerID) : playerID(playerID) {}

In order for that to work, you must first add playerID as a new private member.

Finally, create a new public display() member function:

void npcPlayers::display() const
{
    std::cout << "Player 1 Details:\n";
    std::cout << "Name: " << npcNames[playerID] << "\n";
    std::cout << "Hearts: " << npcDetails[playerID][DETAILS_HEARTS] << "\n";
    std::cout << "Birthday: " << npcDetails[playerID][DETAILS_BDMON]
        << "/" << npcDetails[playerID][DETAILS_BDDAY] << "\n";
}

With this, you can simply display all of this in the calling code as such:

npc.display();

Misc.:

  • Try not to use using namespace std.
  • There's no need to use C-strings in C++. Instead, use std::string objects.
  • You don't need return 0 at the end of main(). As successful termination is always implied at this point, the compiler will insert this return for you automatically.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.