Public Group

This topic is 1052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi guys,

I've been learning to program for about 2 months now, with C++11 from Bjarne Stroustrup's "The C++ language" book.

I've read the section covering basic facilities, variables, containers etc. I'm now reading about the abstraction mechanisms and classes and I think I understand them, however I have trouble figuring out how best to use them to write good code. Since I'm an absolute basic beginner I have just been using console apps to practice, for classes practice I decided to try and write a text based rpg like game with a few rooms just to test. I'm realising that learning the syntax is easy but the hard part of programming is design.

I was wondering if I could get some advice on the best way to use classes to make such a game, or any game/program really so that it is easily extendable without modifying the classes too much. In my program so far I've made some classes and functions to handle a waiting prompt, the setup of a new area etc, but when I come to write the room in the main cpp file, it just seems wrong and a weird way to go about it. Any advice is appreciated, below is my code example ;

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>

#define TITLE "\tTest RPG\n\t---------\n\n\n"
#define PROMPT "\n> "

void wait();
void setupareas();
void bunker();
void cafe();

void wait()
{
print("\n\n\n1. Back\n\n\n> ");
char c;
std::cin >> c;
}

class area
{
public:
short m_seen = 0;
std::string m_name;
std::string m_description;
void setname(std::string name);
void setdescription(std::string description);
void printdescription();
} ;

void area::setname(std::string name)
{
m_name = name;
}

void area::setdescription(std::string description)
{
m_description = description;
}

{
{
}
}

{
{
}
}

{
std::cout << TITLE;
print(m_name);
std::cout << std::endl << std::endl;
{
print(i);
std::cout << std::endl << std::endl;
}
}

{
std::cout << TITLE;
print(m_name);
std::cout << std::endl << std::endl;
{
print(i);
std::cout << std::endl << std::endl;
}
}

void area::printdescription()
{
m_seen = 1;
char wait;
system("cls");
print(m_description);
print("\n\n\n1. Back\n\n");
print(PROMPT);
std::cin >> wait;
}


Then the main.cpp file where I 'setup' the rooms and run the game

#include "head.h"

area bunker;
area cafe;

using namespace std;

int main(void)
{
setupareas();
bunker();
return 0;
}

void setupareas()
{
bunker.setname("Bunker\n_________");
bunker.setdescription("You are in the bunker");
vector<string> temp {"1.Look Around", "2. Go North", "3. Go South"};
temp = {"1.Look Around", "2. Go North", "3. Go South", "4. Examine poster"};

cafe.setname("Cafe\n_________");
cafe.setdescription("You are in the cafe");
temp = {"1.Look Around", "2. Go South"};
temp = {"1.Look Around", "2. Go South", "3. Speak with coffee lady", "4. Speak with worker"};
}

void bunker()
{
bool here = true;
while (here)
{
system("cls");
if (bunker.m_seen == 0)
{
}
else
{
}
print(PROMPT);
char choice;
cin >> choice;
switch(choice)
{
case '1': bunker.printdescription(); break;
case '2': cafe(); break;
case '3': break;
case '4':
system("cls");
print("Poster");
wait();
break;
}
}
}

void cafe()
{
bool here = true;
while (here)
{
system("cls");
if (cafe.m_seen == 0)
{
}
else
{
}
print(PROMPT);
char choice;
cin >> choice;
switch(choice)
{
case '1': cafe.printdescription(); break;
case '2': bunker(); break;
case '3':
system("cls");
wait(); break;
case '4':
system("cls");
print("worker");
wait(); break;
}
}
}


##### Share on other sites

The first thing that jumps out at me is that you are a lot going on in the class. You should ideally keep class members private and function public. You could delegate the object initialization upon creation to the class constructor and maybe cut out some of the code. Sadly, I am busy right this second, but I will use your code as a basis and write up code based on what I mean.

##### Share on other sites

thanks a lot, no rush. I'll be grateful for any examples you may provide!

##### Share on other sites

You should camelCase your functions and variables. i.e. 'setDescription'; it helps with readability. Hard to give too much advice without overloading you but one thing I think you should try to do is make bunker/cafe functions either part of the area class or part of another class that sits above them and deals with the transitions from one area to the next. Hopefully they would be the exact same function/method but because they have different data they will behave differently. They already look very similar so you are almost there.

Also here's something fun for you to do, see what happens if you keep going from the bunker to the cafe then to the bunker then the cafe and so on, I think you will rapidly hit an issue. It's for that reason I think you should move your area transition 'up' in the hierarchy.

Imagine you can only go North, East, South or West (your game may differ) then you might have an enum for that:

enum Direction { North, East, South West};

You already have an 'Area' class so that's good. You may then want another class that holds how they are all related to each other. I.e. the bunker may be north of the cafe, to the east there may be a dungeon. You could have another class then which manager where your player actually is, it would have a pointer/reference to the current area the player is and it would do something like:

while(true)
{
Direction moveDirection = currentArea->Do();
Area* newArea = areaMap.GetArea(currentArea, moveDirection);
if(newArea != null_ptr)
currentarea = newArea;
} 

The Do() method would print your description and options and so on that the current bunker/cafe function is doing. It would stay in there until the player chooses West or so on at which point it will then return that direction. The parent object will then check the area map in the desired direction and hopefully find a new area which will be selected. It will then end up calling Do() on the new area and the process repeats. To quit you could add 'Quit' to the direction enum and return that.

Edited by Nanoha

##### Share on other sites

Well, I feel like my advice is dwarfed by the others so I will just give a quick example using a chunk of your code for what I meant. With me I give my classes their own header and source files (I prefer to prefix class header/source with a capital C [for class] followed by class name; some prefer to drop the C and just use the name):

CArea.h:

#pragma once
#include <vector>
#include <string>

class Area {
// I always try to make members private
short m_seen = 0;
std::string m_name;
std::string m_description;
public:
Area(std::string name, std::string description); // can make the constructor replace two functions
//
// void setname(std::string name);
// void setdescription(std::string description);
// no point in adding the other functions as I left them unchanged for this example
void printDescription() const;
};



CArea.cpp

#include "CArea.h"
#include <iostream>
Area::Area(std::string name, std::string description) :m_name{ name }, m_description{ description } {}

void Area::printDescription() const
{
std::cout << m_name << '\n';
std::cout << m_description << '\n';
}



main.cpp

#include "CArea.h"

//area bunker;
//area cafe;

int main(void)
{
// This way you can initialize name/description upon object creation
// rather than using public members or setter functions
Area bunker{ "Bunker\n_________", "You are in the bunker" };
Area cafe{ "Cafe\n_________", "You are in the cafe" };

bunker.printDescription();
cafe.printDescription();
//setupareas();
//bunker();
return 0;
}

Output:

Bunker
_________
You are in the bunker
Cafe
_________
You are in the cafe
Press any key to continue . . .

Edited by BHXSpecter

##### Share on other sites

wow thanks guys, I knew I must of been going about it the wrong way. I'm going to create another class to handle where the player is and review the gamestate idea.

thanks a lot, no doubt i'll be back with another question later as I re-do the code!

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013245
×