What I Have So Far

Alright, my first game project is making a text adventure game. I've got the rooms linked so far. I haven't seen anyone else linking their rooms like this and it might be a horrible way but I thought it was cool that I haven't seen any other code like mine. Makes it feel like it's mine. ;]


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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
using namespace std;

// This is where all the rooms are created.
// Further down is a function that connects all the rooms.

class roomcreation{
  public:

    void setName(string name);
    string getName();

    void setDesc(string desc);
    string getDesc();

  private:

    string itsName;
    string itsDesc;};

void roomcreation::setName(string name){
  itsName = name;}

string roomcreation::getName(){
  return itsName;}

void roomcreation::setDesc(string desc){
  itsDesc = desc;}

string roomcreation::getDesc(){
  return itsDesc;}

int main(){

  int playerPosition = 0;
  string name;
  string choice;

  roomcreation room1;
  room1.setName("Room 1\n");
  room1.setDesc("This is a blank room.\n\n");

  roomcreation room2;
  room2.setName("Room 2\n");
  room2.setDesc("This is a blank room.\n\n");

  roomcreation room3;
  room3.setName("Room 3\n");
  room3.setDesc("This is a blank room.\n\n");

  cout << "Please enter a name: ";
  cin >> name;
  cout << "Name has been set to " << name << ".\n\n";

  while(choice != "quit"){

    if(playerPosition == 0){
      cout << room1.getName();
      cout << room1.getDesc();
      cout << ": ";
      cin >> choice;
      if(choice == "n"){
        playerPosition = 1;}}

    if(playerPosition == 1){
      cout << room2.getName();
      cout << room2.getDesc();
      cout << ": ";
      cin >> choice;
      if(choice == "n"){
        playerPosition = 2;}
      if(choice == "s"){
        playerPosition = 0;}}

    if(playerPosition == 2){
      cout << room3.getName();
      cout << room3.getDesc();
      cout << ": ";
      cin >> choice;
      if(choice == "s"){
        playerPosition = 1;}}}

return 0;}


And now I'm going to put some monsters in. I think I'll make the respawn function do something like, every time the player moves "respawn" gets a +1 to it. Whenever it is greater than equal to the respawn number the monsters in the room attack the player and also reset the respawn rate to 0.

Anyways, enjoy.

-Ghost
* weird formatting
* get and set functions do nothing useful (like error checking). you might as well use public variables (not a bad thing).
* you should store your rooms in an array. then you wouldn't have to write the same thing for each room (lines 58-82)

good luck
Yeah, for some reason I'm not good with arrays, I don't know why. Could you give me an example of how to put these rooms in an array?
1
2
3
4
5
int roomID[3] = {0,1,2};
int roomExits[4] = {0,1,2,3}; //n,e,s,w

int room1ID = roomID[0];
int room1Exits = roomExits[1];


That's what I'm thinking right now. Any advice?
Whenever I'm planning to use an array, I usually just use a vector, it has much more power (member functions, Iterators and Algorithms) than an array. It's extremely easy to use:

1
2
3
#include <vector> //include
vector<string> rooms; //declaring your vector named rooms which is empty for now, you don't need to specifiy a starting size, but if you do want to,  just add a value in parens after.
rooms.push_back("cellar");  //adds a new room to the end of your vector, this element will equal 0 since will take first position, next push_back would equal 1 and so on 


Once you have declared your vector and added elements, you can use other member functions like:

1
2
3
4
rooms.size(); //will display the # of rooms in your vector 
rooms[i]; //allows you to target a specific room, ie i=0 would be the cellar (and allows indexing with the for statment)
rooms.pop_back(); //removes the last element of your vector
rooms.clear(); //removes all items in your vector 


More info on vectors:
http://www.cplusplus.com/reference/stl/vector/

hope that helps.
Last edited on
Hamsterman, I don't think his formatting is strange - in fact, it's almost identical to how I format my code, except that he has whitespace around operators.

I really like how you made it object oriented - that will help you extend it in the future.

As for suggestions, what omnigate said is good. Vectors would be very useful in this situation because you're dynamically adding rooms.
in fact, it's almost identical to how I format my code
That makes your formatting weird too, doesn't it?
I'm not saying it's horrible, random or non-existent as it so often on happens to be. It's just that } is quite easy to miss.

As for arrays,
you could use vectors, but there is hardly any benefit from that in this case, so I'll use normal arrays.
1
2
3
4
5
6
7
8
9
10
11
12
13
roomcreation room[3] = {
   "Room 1\n", "This is a blank room.\n\n",//you can do it like this
   "Room 2\n", "This is a blank room.\n\n",//but you can do it your old way too
   "Room 3\n", "This is a blank room.\n\n"
};
while(choice != "quit"){
   cout << room[playerPosition].getName();
   cout << room[playerPosition].getDesc();
   cout << ": ";
   cin >> choice;
   if(playerPosition < 2 && choice == "n") playerPosition ++;
   else if(playerPosition > 0 && choice == "s") playerPosition --;
}


* It would be a good idea to write a constructor for roomcreation
* http://www.cplusplus.com/forum/articles/28558/
Topic archived. No new replies allowed.