Console RPG

Hello! I'm pretty new to C++ and I'm trying to create a text-based RPG. I want to create a player, item, and enemy class. Also how to make the map of the game in a beginner way.

For the player class I already have the skills planned out (int playerHealth, int playerMana, ...) but I'm wondering how to make an inventory class that works with the item class. My idea for the item class is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Item {
private:
   enum itemType{MELEE, RANGED, MAGIC};
   string name;
   string modifier; //Like rusty, jagged, etc.
   int itemID; //To be used in the inventory... somehow...
   int damage;
   int range;
   int magicDamage;
   int numArrows; 
   int manaCost;
   enum magicType{FIRE, WATER, AIR, EARTH};
public:
   Item(itemType item_Type, string name, string modifier, int damage, int range, magicDamage, int numArrows, int manaCost, magicType magic_Type);
   //If it's a melee item then range and magic related variables are ignored.
   ~Item(); //Never got the point of destructors
};


In the constructor in the main.cpp file I will create all the items in the game. Please tell me if that's the correct way of doing it or do I structure my Item class in another way? How would I make the inventory feature? Do I make a new class for the Inventory that connects to the player class? This where I have the inventory class right now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Player {
private:
   string playerName;
   int playerClass; //Using an if class it will change the stats and armor.
   int playerHealth;
   int playerMana;
   int playerStamina;
   int playerDefense; //I divide the incoming attack damage by the defense
   int playerMagicStrength; //Magic
   int playerStrength; //Melee
   int playerDexteriry; //Bow
   bool isMagicArmorAllowed; //Use this for weapons too
   bool isRangedArmorAllowed; //Use this for weapons too
   bool isMeleeArmorAllowed; //Use this for weapons too
   int inventory[10];
   int inventoryCapacity;
public:
   Player(string playerName, int playerClass);
   ~Player();
};


So in the code I would do this after asking for the class and name and all that:

 
Player player(mainName, mainClass);


Too expand the inventory limit then I would do this:

 
inventory[10] = new inventory[inventoryLimit];


Am I getting this completely wrong or what? How would I structure player, inventory, and inventory in a way where they work?

Also how should I do the enemy class? Do I just use a class which has the general stats and then use inheritance or create a separate class for every enemy or what?

My final question is how would I make the map? Do I use while loops reading the location number and then print out a string that looks like the map? Please do not bombard me with really complicated things if you can and sorry if my code looks really newbish.
Also sorry if this wall of text is sloppy. :(
Why not just use a std::vector for your inventory class?
A simple inventory is just a collection of items.
1
2
3
4
5
6
7
8
typedef vecotr<Item> Inventory;

class Player
{
...
  Inventory inventory;
...
};


If at some point you want your inventory to have properties other than a simple collection class, you can replace the typedef with an Inventory class which inherits a vector:
1
2
3
4
class Inventory : public std::vector<Item>
{
...
};


BTW, this line is faulty:
 
  inventory[10] = new inventory[inventoryLimit];

new returns a pointer to an array of 10 inventories (assuming inventoryLimit is 10). inventory[10] is an array of inventories, not an array of pointers. This line won't compile.
Also, inventory[10] is not a valid reference. subscripts in C++ are 0 based, therefore the valid elements are inventory[0] to inventory[9].

Also how should I do the enemy class?

Any way you want. You're limited only by your imagination.

If your Player and Enemy have common attributes, you might want to make a Character class with contains the common attributes such as stamina and health, then have both Player and Enemy inherit the Character class.

how would I make the map?

Again, you're limited only by your imagination. What to you want the map to represent? A simple 10x10 character grid? Rooms in a dungeon? Tiles in a world (ala Minecraft)?
Last edited on
I do not know what vectors are so could you send me a link to a good tutorial? I'm going to do what you said about one Character class but could you tell me how to make the different enemies from the inherited Enemy class? Do I only do something like this in the constructor:

1
2
Enemy slime(health, mana, ...);
Enemy ogre(health, mana, boss, ...);


On the map topic, is it too complicated for me to do it so that when you type 'W' you move in the map? This is basically my knowledge: variables, arrays, structs, classes, functions, and that sort of beginner stuff.

Also additional question: how would I do a save game? Where can I find an good I/O tutorial for beginners?
Tutorials for std::vector and I/O are on this site:
http://www.cplusplus.com/reference/vector/vector/
http://www.cplusplus.com/doc/tutorial/files/

Do I only do something like this in the constructor:
1
2
Enemy slime(health, mana, ...);
Enemy ogre(health, mana, boss, ...);

That's one way.

is it too complicated for me to do it so that when you type 'W' you move in the map?

No. That is pretty straight-forward. I suggest you create a move function that accepts a character (W,A,S,D), ensures the character is valid, calculates the new coordinates, checks that the new coordinates are in bounds, moves the player to that position, then takes action based on what's at that position (a new room, a wall, a monster, a treasure, an item to acquire, etc).





How would that type of map work? Do I create an array like this:

1
2
3
map1[5][5][5] = {"#", "#", "#", "#", "#",
"#", ".", "@", ".", "#",
}


And so on... And then how would I make it so that it received what key I pressed instead of storing it in a variable and then accessing it? Do I use something like getch() or something? I'm new to these kind of stuff I'm used to the type command, store in variable, run if or switch statement and continue.

Also thanks for the links. :D
Last edited on
Again, that's one way. Although I would suggest a 2D array rather than a 3D array for a console game.

Use cin to read a character from the console:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  char  dir;

  cout << "Direction?";
  cin >> dir;
  switch (dir)
  {
  case 'w':  //  Move north
                 player.move (curr_row-1, curr_col);
                 break;

  case 'a'   // Move west 
                 player.move (curr_row, curr_col-1);
                 break;

  case 's':  //  Move south
                 player.move (curr_row+1, curr_col);
                 break;

 case 'd':  //  Move east
                 player.move (curr_row, curr_col+1);
                 break;

  default:  cout << "Not a valid direction" << endl;
  }

No offense but if you're a C++ beginner you ARE going to get mired down dealing with all the details you're trying to implement. Drop all the complexity for now and try to write a simple RPG. For instance: A four room house with a few basic objects in each room such as hidden gold and keys to the other rooms. Have a basic inventory of gold, keys and maybe useful objects you need to complete the game (crowbar, flashlight, etc.) and keep everything simple. Once you have the basics of OOP and how to handle maps and data structures as second nature, THEN it's time to worry about magic spells, melee vs ranged, armor types, NPCs and all that fun stuff 8^D. Trying to incorporate it now will just end up confusing you, distracting you, and ultimately frustrating you.
Cnoeval:
None taken, thanks for the advice.

Abstraction:
Once I understand vectors and all that I will use your method.

Thanks for your help. :D
Topic archived. No new replies allowed.