question on best approach, class or function

I thought I would try and do a McMurphy's Mansion type thing to help me learn the basics of programming. I think I really got the concept of objects backwards. Instead of using the class to manipulate the room information, I have created a function for every room. There is no reason to have a Room class as it is never even used in the current program. But, I had hoped to use a Room class to be able to store things like, is there a dresser? What's in it? for each room. Is the current format of functions for each room ok, or should each room be an object created by a Room class? If the latter, does anyone have any ideas on how to do that?

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;

void room1();
void room2();
void room3();

class Room
{
public:
	int North, South, East, West;
	int roomNum;
};


void Direction(int a, int b, int c, int d)
{
	int north = a;
	int south = b;
	int east = c;
	int west = d;
	string go;
	cout << "\nWhere would you like to go?";
	cin >> go;
	if (go == "n" || go == "N" || go == "north" || go == "North")
	{
			switch (north)
			{
			case 2:
				room2();
				break;
			case 3:
				room3();
				break;
			default:
			    cout << "I'm sorry, you cannot go that way at this time.\n";
				Direction(a, b, c, d);
				break;
			}
	}
	else if (go == "s" || go == "S" || go == "south" || go == "South")
	{
			switch (south)
			{
			case 1:
				room1();
				break;
			case 2:
				room2();
				break;
			case 3:
				room3();
				break;
			default:
			    cout << "I'm sorry, you cannot go that way at this time.\n";
				Direction(a, b, c, d);
				break;
			}
	}
	else if (go == "e" || go == "E" || go == "east" || go == "East")
	{
			switch (east)
			{
			case 1:
				room1();
				break;
			case 2:
				room2();
				break;
			case 3:
				room3();
				break;
			default:
			    cout << "I'm sorry, you cannot go that way at this time.\n";
				Direction(a, b, c, d);
				break;
			}
	}
	else if (go == "w" || go == "W" || go == "west" || go == "West")
	{
			switch (west)
			{
			case 1:
				room1();
				break;
			case 2:
				room2();
				break;
			case 3:
				room3();
				break;
			default:
			    cout << "I'm sorry, you cannot go that way at this time.\n";
				Direction(a, b, c, d);
				break;
			}
	}
}


void room1()
{
	Room room1;
	room1.roomNum = 1;
	room1.North = 2;
	cout << "You are in your bedroom.\n";
	Direction(2, 0, 0, 0); //Determines which rooms are n, s, e, and w from the one you are in.
	
}

void room2()
{
	Room room2;
	room2.roomNum = 2;
	room2.North = 3;
	room2.South = 1;
	cout << "You are now in the hallway. Congrats.\n";
	Direction(3, 1, 0, 0);
}

void room3()
{
	Room room3;
	room3.roomNum = 3;
	room3.North = 0;
	room3.South = 2;
	cout << "You are now in the bathroom. Peepeepeeepeeeeeeeee.  Poop poop PEEPEEEeeee!!!!!\n";
	Direction(0, 2, 0, 0);
}

int main()
{
  room1();
}
Last edited on
Start on paper. Write down what your program needs to do. Then think about how to do it. I'd do room a class and just create objects for each room you need. Think about what all a room needs to hold, you can also create a class for objects in rooms if you wish.
You need to take a step back and think of it logically. You might want a class "House", and that class has 3 rooms and 2 bathrooms. You could store the functions inside of the class House. You might also want a class "Enemy" that stores a "ninja" , or a "ghost". Because a house HAS a Enemy, you can include an Enemy object inside of the class House. A lot of programming is logic and thinking.
hmm...

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

class someObject
{
protected:
      std string strName;  // name of the object
      std string Description; // description of the object
public:
      someObject(const std::string &Name, const std::string &Desc) 
      {
             strName = Name;
             strDescription = Desc;
       }

      virtual bool Use(int roomNumber) = 0;  // a virtual functions of someObject
};

class bookObject : public someObject
{
public:
       BookObject(const std::string &Name, const std::string &Desc)
       {
             strName = Name;
             strDescription = Desc;
       }            
       virtual bool Use(int roomNumber)
       {
              if(roomNumber == 231)
              {
                     cout << "You use the book" << endl;
                     cout << "You fall faint."  << endl;
              }
              else
              {
                     cout << "You Can't use the book here" << endl;
              }
       }
};

class room
{
private:
      int roomNumber;
      std::string strName; // the name of the room...
      std::string Description;  // a short discription.
      std::list<someObject> listofObjectsOnFloor;
      std::list<someActions> listofActionsCanDo;
      std::list<std::string> listofExits;  // plan a direction code plus room Number
      // if it doesn't exist I bump into a wall

public:
      room(int nRoomNumb, const std::string &Name; const std::string &Desc)
      {
             roomNumb = nRoomNumb;
             strName = Name;
             Description = Desc;
      }
      int Command(const std::string strCommand, CPlayer &player)
      {
             // parse the command....
             // break into parsed elements 
             // if we return a number we move to that room.
             // if we return a zero we don't move.
             // for example I would ad normalizations too..
             if(strCommand == "north")
             {
                   //scan the listofExits for the N### code
                   // extract the ### and return it.
                   // if not found display you bump into a wall.
             }
             // rince and repeat for other commands..
      
             if(strCommand == "get")
             { 
                  //see if we have the object in the room...
                  // transfer to player
             }
  
             if(strCommand == "Use")
             {
                   // is the object in possession or not..
                  // if it is use the object.
                  player->SearchObjects();
                  player->Use(strObject);
             }
             // search the actions for things I can do with specific commands...
      } // end of command

      void addObjects(someObject &sObject);
      void addActions(someAction &sAction);
      void addExit(const std::string &exit);
      int GetRoomNumber() {return roomNumb;}
}; end of room

int Main()
{
        std::vector<room> listOfRooms;

        room room1(1, "Entry Way", "This is the entry way");
        room1.AddExit("N2");
        listOfRooms.push_back(room1);
        
        room room2(2, "Foyer", "This is the Foyer");
        room2.AddExit("N3");  // north go to that room
        room2.AddExit("E4");
        room2.AddExit("W5");
        room2.AddExit("S1");
        listofRooms.push_back(room2);

        room room3(3, "Kitchen", "This is a Kitchen");
        room3.AddExit("N6");
        room3.AddExit("S2");
        room3.AddExit("D10");
        room3.AddObject(Broom);  // broom is another thing inherited from someObject
        listofRooms.push_back(room2);
        // so on on the rooms.

        // set up the first room
        room *pCurrentRoom;
        // scan the room list for room 1.
        pCurrentRoom = &listofRooms.get(// some defined iterator);
        
        std::string command;
        int nCurrentRoom= 1;
        do
        {
              if(pCurrentRoom)
              {
                   pCurrentRoom->DisplayName();
                   pCurrentRoom->DisplayDesc();
                   pCurrentRoom->DisplayOjbects();

                   cout << "What Would you like to do? (\"Quit\" will end game) " << endl;            
                   getline(cin, strCommand);
                   
                   nCurrentRoom = pCurrentRoom->Command(strCommand);
                   if(nCurrentRoom)
                   {
                          // with a new room number scan the list of objects again.
                          pCurrentRoom = &listofRooms.get(// some defined iterator);
                   }
              } // end of valid pointer
         } while ( (strCommand != "Quit")&& (strCommand != "quit"))
       
         cout << "Thank you for playing" << endl;
         return 0;
}


this is a mix between suedo code and actual stuff. I don't know if this was an homework assignment or something else. I hope I made some things understandable but it is a basic framework.


Yeah, I am still just jumping in and changing things on the fly as I think of them. ADD at work, I guess. I really like the idea of a "House" class with the room functions contained in it. I think to make an enemy I'd want it to move around the house randomly. To do that I'd have to come up with a different directional system than the one I have. I was so proud of that too. Ok, thanks for the input. Good stuff.

EDIT: Azagaros, I posted this before I saw your post. Don't know how it came up after yours without me seeing your post, but I appreciate the code. Not an assignment, just for fun to try and get the basics down. Again, thanks for the time and effort.
Last edited on
I understand. I just whipped this out. In a previous semester of school I had someone ask me for something similar and I never got around to it because of my class load. I didn't realize it would be this simple to whip out in 1 hour. Its got a lot of work to get to the state I would take it too. Having mobiles(or wandering monsters) wouldn't be hard to extrapolate into this code.

I wrote something similar in pascal years ago before I knew object orientation.

In this form I can put it in files and the whole thing and load at will too.
Last edited on
Topic archived. No new replies allowed.