HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

C++ text-based RPG

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textbasedrpg

Problem

I have made this small text-based RPG in C++ which is based around one quest. I did this to practice what I have learnt so far. How could I improve it? Be as picky as you'd like.

```
#include
#include
#include

using namespace std;

void riverstead();
void aragornHouse();
void stage1();
void stage2();
void stage3();
void stage4(int &sword, int &gold);
void ratCave();
void attackThief(int &pHealth, int &tHealth);
void thiefDead();
void searchBody();
void questUpdate();
void end();

int input;
int stages[5] = {1, 0, 0, 0, 0};
string qUpdates;
string qStages;

int main() {

srand (time(NULL));

system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
qUpdates = "Quest begun";
qStages = "Talk to Aragorn in his house";
questUpdate();

riverstead();

case 2:
exit(0);

}
}

void riverstead() {

system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
aragornHouse();

case 2:
ratCave();

}
}

void aragornHouse() {

if (stages[0] == 1) {
system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
stage1();

case 2:
system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
stages[0] = 0;
stages[2] = 1;
system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
stages[0] = 0;
stages[1] = 0;
stages[2] = 1;
system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
system("cls");
cout ";
cin >> input;

system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
system("cls");
cout ";
cin >> input;
switch (input) {

case 1:
tHealth = tHealth - pDamage;
system("cls");
cout ";
cin >> input;

Solution

Don't do using namespace std in global scope, instead use it inside the functions or even better only on the things you are using:

int main()
{
  using std::cout;
  ...
}


In your main, you have exit(0), instead you should do a return statement.

return EXIT_SUCCESS;


You don't have a default case in your switch statements, so if a user enters the wrong number nothing happens and the program ends without notice. It is better to have some kind of error handling. You should also do a function that shows a number of options and lets the user enter a value that is returned if correct, that way you save some typing.

e.g.

/**
 * show a number choices and lets the user choose one
 * @returns 1-n
 */
int promptUser( const std::vector& options );


Your program has the structure of a C program, you should use classes to encapsulate functionality. Identify the objects in the story and create appropriate classes.

e.g. Aragorn, Thief, Player have some common traits

system("pause");


Calling external programs like that is not a good thing, it opens up a security hole in your application instead use std::getline or similar.

You forgot to initialize some variables e.g.

int gold;   <---
int sword;

gold = gold + 20;


Always make it a habit to initialize variables when you declare them.

Don't call main(). It makes the program flow difficult to follow, instead have a loop in main() if you want to allow restart of the game.

Code Snippets

int main()
{
  using std::cout;
  ...
}
return EXIT_SUCCESS;
/**
 * show a number choices and lets the user choose one
 * @returns 1-n
 */
int promptUser( const std::vector<std::string>& options );
system("pause");
int gold;   <---
int sword;

gold = gold + 20;

Context

StackExchange Code Review Q#80531, answer score: 9

Revisions (0)

No revisions yet.