Tell me more ×
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 what is the best way to save on RAM/Memory when there are many required arrays (I plan to have many more NPC's and various other arrays with text and items). And I am wondering what I can improve on so far. I'm new with C++ (as you can probably tell).

  #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
 
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
 
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

1 Answer

  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 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 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

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.