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 ;
header file ;
#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;
std::vector<std::string> m_menu1;
std::vector<std::string> m_menu2;
void setname(std::string name);
void setdescription(std::string description);
void presetmenu(std::vector<std::string> menu);
void postsetmenu(std::vector<std::string> menu);
void printmenu1();
void printmenu2();
void printdescription();
} ;
void area::setname(std::string name)
{
m_name = name;
}
void area::setdescription(std::string description)
{
m_description = description;
}
void area::presetmenu(std::vector<std::string> menu)
{
for (auto i : menu)
{
m_menu1.push_back(i);
}
}
void area::postsetmenu(std::vector<std::string> menu)
{
for (auto i : menu)
{
m_menu2.push_back(i);
}
}
void area::printmenu1()
{
std::cout << TITLE;
print(m_name);
std::cout << std::endl << std::endl;
for (auto i : m_menu1)
{
print(i);
std::cout << std::endl << std::endl;
}
}
void area::printmenu2()
{
std::cout << TITLE;
print(m_name);
std::cout << std::endl << std::endl;
for (auto i : m_menu2)
{
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"};
bunker.presetmenu(temp);
temp = {"1.Look Around", "2. Go North", "3. Go South", "4. Examine poster"};
bunker.postsetmenu(temp);
cafe.setname("Cafe\n_________");
cafe.setdescription("You are in the cafe");
temp = {"1.Look Around", "2. Go South"};
cafe.presetmenu(temp);
temp = {"1.Look Around", "2. Go South", "3. Speak with coffee lady", "4. Speak with worker"};
cafe.postsetmenu(temp);
}
void bunker()
{
bool here = true;
while (here)
{
system("cls");
if (bunker.m_seen == 0)
{
bunker.printmenu1();
}
else
{
bunker.printmenu2();
}
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)
{
cafe.printmenu1();
}
else
{
cafe.printmenu2();
}
print(PROMPT);
char choice;
cin >> choice;
switch(choice)
{
case '1': cafe.printdescription(); break;
case '2': bunker(); break;
case '3':
system("cls");
print("lady");
wait(); break;
case '4':
system("cls");
print("worker");
wait(); break;
}
}
}