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

RPG game shop system

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

Problem

I'm interested in the organization, layout and style of how I program. How does organizing code using whitespace work? I've had a go in the code below. I mean like when you press tab and start the code further right into the page.
Was using the goto command the best option for this code? Before learning C++, I learned batch, so I am used to using goto and labels.

How do I organize this code better? What could I have done better in general?

I don't think there's any bugs in here, but I'd appreciate it if you could tell me if there are.

```
#include
#include
#include

using namespace std;

int main() {
int choice;
int gold = 0;
int sword = 0;
int armour = 0;
int potion = 0;
int meat = 0;
int skin = 0;

system("cls");
cout ";
cin >> choice;
switch (choice) {
shop:
case 1:
system("cls");
cout ";
cin >> choice;
switch (choice) {
buy:
case 1:
system("cls");
cout ";
cin >> choice;
switch (choice) {
case 1:
if (gold ";
cin >> choice;
switch (choice) {
meat:
case 1:
if (meat < 1) {
system("cls");
cout << "\n You don't have any meat to sell!"
<< endl;
cout << "\n ";
system("pause");
goto sell;
} else {
system("cls");
gold = gold + 5;
meat = meat - 1;
cout << "\n Item added - 5 Gold" << endl;
cout << "\n ";
system("paus

Solution

You can divide main() into lots of smaller functions and use appropriate enums to deal with choices.

I have used enum classes in this answer, which is supported in C++11. If you are not able to use a C++11 compiler, you have use modify the enums a bit.

Here's how I would advise starting with main():

#include 
#include 
#include 

using namespace std;

enum class TopLevelChoice
{
   SHOP = 1,
   WOODS,
   EXIT
};

TopLevelChoice getTopLevelChoice()
{
   int choice;
   system("cls");
   cout  ";
   cin >> choice;
   return static_cast(choice);
}

void goToWoods()
{
   // Do the needful to go to the woods.
}

void goToShop()
{
   // Do the needful to go shopping.
}

int main() {

   system("cls");
   cout << "\n Item added - 50 gold" << endl;
   cout << "\n ";
   system("pause");

   TopLevelChoice choice;
   while ( (choice = getTopLevelChoice()) != TopLevelChoice::EXIT )
   {
      switch (choice)
      {
         case TopLevelChoice::SHOP:
            goToShop();
            break;

         case TopLevelChoice::WOODS:
            goToWoods();
            break;

         default:
            cout << "Invalid choice in main(): " << (int)choice << endl;
      }
   }
}


You can move the code from main() for dealing with various options into their own functions. It'll make your code easier to understand and maintain.

The rest of the code from main() can be refactored to:

int gold = 0;
int sword = 0;
int armour = 0;
int potion = 0;
int meat = 0;
int skin = 0;

enum class ShopChoice
{
   BUY = 1,
   SELL,
   GOBACK
};

ShopChoice getShopChoice()
{
   int choice;
   system("cls");
   cout  ";
   cin >> choice;
   return static_cast(choice);
}

enum class BuyChoice
{
   SWORD = 1,
   ARMOUR,
   POTION,
   GOBACK
};

BuyChoice getBuyChoice()
{
   int choice;
   system("cls");
   cout  ";
   cin >> choice;
   return static_cast(choice);
}

void buySword()
{
   if (gold  ";
   cin >> choice;
   return static_cast(choice);
}

void sellMeat()
{
   if (meat < 1) {
      system("cls");
      cout << "\n You don't have any meat to sell!"
         << endl;
      cout << "\n ";
      system("pause");
   } else {
      system("cls");
      gold = gold + 5;
      meat = meat - 1;
      cout << "\n Item added - 5 Gold" << endl;
      cout << "\n ";
   }
}

void sellSkin()
{
   if (skin < 1) {
      system("cls");
      cout << "\n You don't have any skin to "
         "sell!" << endl;
      cout << "\n ";
      system("pause");
   } else {
      system("cls");
      gold = gold + 10;
      skin = skin - 1;
      cout << "\n Item added - 10 Gold"
         << endl;
      cout << "\n ";
      system("pause");
   }
}

void sell()
{
   SellChoice choice;
   while ( (choice = getSellChoice()) != SellChoice::GOBACK )
   {
      switch (choice)
      {
         case SellChoice::MEAT:
            buySword();
            break;

         case SellChoice::SKIN:
            buyArmour();
            break;

         default:
            cout << "Invalid choice in sell(): " << (int)choice << endl;
      }
   }
}

void goToShop()
{
   ShopChoice choice;
   while ( (choice = getShopChoice()) != ShopChoice::GOBACK )
   {
      switch (choice)
      {
         case ShopChoice::BUY:
            buy();
            break;

         case ShopChoice::SELL:
            sell();
            break;

         default:
            cout << "Invalid choice in shop(): " << (int)choice << endl;
      }
   }
}

Code Snippets

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

enum class TopLevelChoice
{
   SHOP = 1,
   WOODS,
   EXIT
};

TopLevelChoice getTopLevelChoice()
{
   int choice;
   system("cls");
   cout << "\n Where do you go?" << endl;
   cout << "\n " << (int)TopLevelChoice::SHOP  << ". Shop" << endl;
   cout << "\n " << (int)TopLevelChoice::WOODS << ". Woods" << endl;
   cout << "\n " << (int)TopLevelChoice::EXIT  << ". Exit" << endl;
   cout << "\n> ";
   cin >> choice;
   return static_cast<TopLevelChoice>(choice);
}

void goToWoods()
{
   // Do the needful to go to the woods.
}

void goToShop()
{
   // Do the needful to go shopping.
}

int main() {

   system("cls");
   cout << "\n Item added - 50 gold" << endl;
   cout << "\n ";
   system("pause");

   TopLevelChoice choice;
   while ( (choice = getTopLevelChoice()) != TopLevelChoice::EXIT )
   {
      switch (choice)
      {
         case TopLevelChoice::SHOP:
            goToShop();
            break;

         case TopLevelChoice::WOODS:
            goToWoods();
            break;

         default:
            cout << "Invalid choice in main(): " << (int)choice << endl;
      }
   }
}
int gold = 0;
int sword = 0;
int armour = 0;
int potion = 0;
int meat = 0;
int skin = 0;

enum class ShopChoice
{
   BUY = 1,
   SELL,
   GOBACK
};

ShopChoice getShopChoice()
{
   int choice;
   system("cls");
   cout << "\n Shop" << endl;
   cout << "\n Buy or sell?" << endl;
   cout << "\n " << (int)ShopChoice::BUY         << ". Buy" << endl;
   cout << "\n " << (int)ShopChoice::SELL        << ". Sell" << endl;
   cout << "\n " << (int)ShopChoice::GOBACK << ". Go back" << endl;
   cout << "\n> ";
   cin >> choice;
   return static_cast<ShopChoice>(choice);
}

enum class BuyChoice
{
   SWORD = 1,
   ARMOUR,
   POTION,
   GOBACK
};

BuyChoice getBuyChoice()
{
   int choice;
   system("cls");
   cout << "\n Shop, Buy" << endl;
   cout << "\n What do you buy?" << endl;
   cout << "\n " << (int)BuyChoice::SWORD  << ". Sword    50 gold" << endl;
   cout << "\n " << (int)BuyChoice::ARMOUR << ". Armour   100 gold" << endl;
   cout << "\n " << (int)BuyChoice::POTION <<". Potion   10 gold" << endl;
   cout << "\n " << (int)BuyChoice::GOBACK <<". Go back" << endl;
   cout << "\n> ";
   cin >> choice;
   return static_cast<BuyChoice>(choice);
}

void buySword()
{
   if (gold < 50) {
      system("cls");
      cout << "\n You don't have enough gold!"
         << endl;
      cout << "\n ";
      system("pause");
   } else {
      system("cls");
      gold = gold - 50;
      sword = sword + 1;
      cout << "\n Item added - Sword" << endl;
      cout << "\n ";
      system("pause");
   }
}

void buyArmour()
{
   if (gold < 100) {
      system("cls");
      cout << "\n You don't have enough gold!"
         << endl;
      cout << "\n ";
      system("pause");
   } else {
      system("cls");
      gold = gold - 100;
      armour = armour + 1;
      cout << "\n Item added - Armour" << endl;
      cout << "\n ";
      system("pause");
   }
}

void buyPotion()
{
   if (gold < 10) {
      system("cls");
      cout << "\n You don't have enough gold!"
         << endl;
      cout << "\n ";
      system("pause");
   } else {
      system("cls");
      gold = gold - 10;
      potion = potion + 1;
      cout << "\n Item added - Potion"
         << endl;
      cout << "\n ";
      system("pause");
   }
}

void buy()
{
   BuyChoice choice;
   while ( (choice = getBuyChoice()) != BuyChoice::GOBACK )
   {
      switch (choice)
      {
         case BuyChoice::SWORD:
            buySword();
            break;

         case BuyChoice::ARMOUR:
            buyArmour();
            break;

         case BuyChoice::POTION:
            buyPotion();
            break;

         default:
            cout << "Invalid choice in buy(): " << (int)choice << endl;
      }
   }
}

enum class SellChoice
{
   MEAT = 1,
   SKIN,
   GOBACK
};

SellChoice getSellChoice()
{
   int choice;
   system("cls");
   cout << "\n Shop, Sell" << endl;
   cout << "\n What do you sell?" << endl;
   cout << "\n " << (int)SellChoice::MEAT   << ". Meat   5 gold    Number: " << meat << endl;
   cout << "\n " 

Context

StackExchange Code Review Q#62100, answer score: 12

Revisions (0)

No revisions yet.