c++ console rpg

Pages: 12
I am somewhat decent at c++ and want to make a console rpg. I am not sure how to start though, except for the character creation. Would anyone be willing to help me get rolling?
Obligatory link: http://cplusplus.com/articles/G13hAqkS/

Now, whether the rpg is console or not, I think you should first design a map and enable the player to move on it. If you're staying console, I suggest something like
1
2
3
4
struct Room {
   std::string description;
   std::map<std::string, Room*> paths;
};
You could later add vectors of items and monsters to it.
When you have basic interface done, you can start working on combat and skills. There's a gazillion ways to do that, so be original.

By the way, it is likely that you'd want to write a simple parser too.
Last edited on
thank you, but could you please explain some things? is struct room a statement, class, or function? I use "using namespace std" so what of the stds could i take out? what is a vector and a parser. I know this sounds like I dont know alot, but this is why I want to do an rpg
struct is like a class but with everything public by default. You can take away all of the std:: if you use "using namespace std". A vector is a class, similar to an array but has many advantages like resizing, easy access to the size of the vector etc. A parser is used to analyse text, can be useful if the user will be able to enter complicated commands.
thank you this is very helpful
I guess my interpretation of "somewhat decent" was liberal. Here's a couple of alternatives:
Basic and painful. Write a function for each room.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int state = 0;

void room0() {
   cout << "description";
   string str;
   getline(cin, str);
   if (str == "go right") {
      state = 1;
   } else if (str == "open chest") {
      cout << "you found an item!";
   } else ...
}

int main() {
   ...
   while (true) {
      switch (state) {
         case 0: room0(); break;
         case 1: room1(); break;
         ...
      }
   }
   ...
}


Intermediate. Allow generalized rooms using polymorphism. (I have a feeling that this intermediate is not any simpler that advanced..).
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
28
int state = 0;

struct Room {
   virtual void room() = 0;
};

struct RoomWithADoor : Room {
   int newstate;

   RoomWithADoor(int st) : newstate(st) {}
   
   void room() {
      cout << "there is a single door";
      string str;
      getline(cin, str);
      if (str == "go") state = newstate;
   }
}

int main() {
   Room* map[] = {
      new RoomWithADoor(1), 
      new RoomWithDescriptionAndTwoDoors("a nice room", "left", 3, "elevator", 2),
      ...
   };

   while(true) map[state] -> room();
}


Advanced. What I told you. One super-generalized room struct (though you can add more as in Intermediate if you want). state variable removed (I'm not even sure that's a good thing. Now there's a lot of pointer related mess. You might want to have std::map<std::string, int> paths; instead).
Last edited on
thank you for that. What would be a good damage formula with the variables
1
2
3
int randnumb = rand()%2+1, turn;
int health = 50, attack, agility, defense, hurt;
int mhealth = 50, mattack, magility, mdefense, mhurt;


what i have is for the user hurting the monster
 
mhurt = (attack * 3) - ((magility + mdefense) * 2);


and the monster hurting the user
 
hurt = (mattack * 3) - ((agility + defense) * 2);


these don't work because I have them initialized like this
1
2
3
4
5
6
7
agility = rand()%50+40;
defense = rand()%50+40;
attack = rand()%50+40;
            
magility = rand()%30+20;
mdefense = rand()%30+20;
mattack = rand()%30+20;


I either have them doing negative damage if the variables are initialized with the same random percentages (so it adds health) or one overpowers the other if i make one lesser
actually forget about that I decided to finish the rooms first before I do the monsters. But is there anyway to get rid of the double enter with getline
For damage, leave hurt = attack for now. You'll be able to write more complex formulas at any time. Focus on the most important parts now.

By the way, you should really be using structs/classes for player and monsters.

is there anyway to get rid of the double enter with getline
A getline will read a single newline. If you want to ignore some newlines, try http://cplusplus.com/reference/iostream/manipulators/ws/
yeah I decided to make that stuff last and just use a "phantom" monster right now. Quick question: in int main why do I have to do the while loop with the switch statement? What is the purpose of that?
There are several rooms (functions). Each corresponds to a value of 'state'. The switch is needed to call the appropriate function when only 'state' is known.
Imagine that there is no loop. Now you enter room 0 and the program ends. To repeat the 'state' to function matching you need to put it in a loop. Note that normally this shouldn't be a while(true). Rather use while(state != -1) or something similar, so that you have a way to end it.
The variable state stores the current room you're in. At each iteration of the loop, the room's "scene" is played and state is updated to the room you chose to go. So, at each iteration, the switch statement calls the appropriate function according to your state (room).
could you please clarify that? Do I actually have a variable named state? How do I correspond a void function with a variable in int main?
Last edited on
State is a global variable. See line 1 of either of my examples. You correspond a function with a value using a switch.
oh okay so to make sure I understand correctly:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>

using namespace std;

int state = 1;

void help();
void room1();
void room2();

int main()
{
    while(state == -1)
    {
        switch(state)
        {
            case 1:
                room1();
                break;
            case 2:
                room2();
                
        }
    }
}

void help()
{
    //This is where it shows the list of commands that they can do in a given area\\
    return;
}

void room1()
{
    string action;
    
    //description
    
    cout<<"\nWhat do you want to do?\n>";
    
    if(action == "help")
    {
        help();
    }
    
    else
    {
        room2();
    }
}

void room2()
{
    string action;
    state = 2;
    
    //description
    
    cout<<"\nWhat do you want to do?\n>";
    
    if(action == "help")
    {
        help();
    }
    
    else
    {
        room1();
    }
}
this allows me to go to room 1 then type help then go to room 2 then back to room 1. This is a really rough piece of code.
i also realize that I didnt use getline or cin that is because this was just to show my understanding
Not quite.. Firstly, line 14 should be !=. Though I guess that's a typo..
You're not using the state mechanism at all. Recursion is not a great choice. Try adding cout << "I'll be leaving room1 now"; after line 51 and see when that gets printed. Also, recursion abuses stack, so if you're intend to walk from from to room ~ 20000 times, your game should crash. While it is not going to happen, That should illustrate why recursion shouldn't be used without a necessity.
Change calls to room1() and room2() (only in lines 49, 69) to state = 1 and state = 2. And remove line 56. It's redundant. You're only going to be in that function when state == 2.
1) Yes line 14 was a typo
2) How can I put in cout<<"I'll be leaving room1 now"; after line 51 because that is outside every function
3) Yes that is good Ill change lines 49 and 69

here is my new code:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>

using namespace std;

int state = 1;

void help();
void room1();
void room2();

int main()
{
  while(state != -1)
  {
      switch(state) 
      {
          case 1:
              room1();
              break;
          case 2:
              room2();
              break;
      }
  }
}

void help()
{
    cout<<"list of commands go here.";
    return;
}

void room1()
{
    string action;
    
    cout<<"A description of the room. What do you want to do?\n>";
    getline(cin, action);
    
    if(action == "help")
    {
        help();
    }
    
    else
    {
        state = 2;
    }
}

void room2()
{
    string action;
    
    cout<<"A description of the second room. What do you want to do?\n>";
    getline(cin, action);
    
    if(action == "help")
    {
        help();
    }
    
    else
    {
        state = 1;
    }
}


i no that there is no \n this is just the rough bit
ok
Pages: 12