Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Text based mining game using if and while

Text based mining game using if and while

Scheduled Pinned Locked Moved C / C++ / MFC
c++game-devhelpdiscussioncareer
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chleba225
    wrote on last edited by
    #1

    Quote:

    Hello guys, I want to share with you my mini "project" which I managed to do so I can understand c++ better and decided to make mining game with if and while loops. I am in programming for awhile so this is reason why it is not that good.

    There are things I would do different (I'm about to do it later). I want to hear your suggestions, opinions etc.. So let me know.

    Code:

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

    using namespace std;

    int main(){
    bool start = true;
    int startWay;
    bool marketMine = true;
    int marketWay;
    bool trymarket = true;
    static int copperGot = 2;
    int copperNow = 0;
    int balance = 0;
    int shopWay;
    int item;
    bool market = true;
    string errorInput = "Error, Again: ";
    bool gearBuy = true;
    int pickaxe;

    while(start != false){
    cout << "1. Start\n2. Credits\n>";
    cin >> startWay;
    if(startWay == 1){
    cout << "You awake in old house, you dont have any money but your job is to mine.\nYou can make some money and buy gear and upgrades.\n";
    Sleep(2000);
    cout << "1. Mine\n2. Market\n>";
    cin >> marketWay;
    while(marketMine != false){
    if(marketWay == 1){
    cout << endl;
    cout << "You are going mine..";
    Sleep(800);
    cout << "\nMining...." << endl;
    Sleep(1000);
    copperNow += copperGot;
    cout << "You have got > " << copperGot << " copper and your copper is > " << copperNow << ". \n" << endl;
    cout << "1. Mine\n2. Market\n>";
    cin >> marketWay;
    } else if(marketWay == 2){
    Sleep(1000);
    system("CLS");
    cout << "Welcome on market. Here you can sell ores you have mined and buy better items\nfor more ores or money.\n\n";
    Sleep(2000);
    while(market != false){
    cout << "1. Sell all copper.\n2. Balance\n3. Gear\n4. Pickaxe\n5. Mine\n>";
    cin >> shopWay;
    if(shopWay == 1){
    Sleep(1000);
    cout << "You have sold " << copperNow << " copper. You have gained " << copperNow * 2 << "$\n\n";
    balance = copperNow * 2;
    copperNow -= copperNow;
    Sleep(2250);
    system("CLS");
    }else if(shopWay == 2){
    cout << "Your balance is: " << balance << "$\n";
    Sleep(3000);
    system("CLS");
    }else if(shopWay

    S V D 3 Replies Last reply
    0
    • C Chleba225

      Quote:

      Hello guys, I want to share with you my mini "project" which I managed to do so I can understand c++ better and decided to make mining game with if and while loops. I am in programming for awhile so this is reason why it is not that good.

      There are things I would do different (I'm about to do it later). I want to hear your suggestions, opinions etc.. So let me know.

      Code:

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

      using namespace std;

      int main(){
      bool start = true;
      int startWay;
      bool marketMine = true;
      int marketWay;
      bool trymarket = true;
      static int copperGot = 2;
      int copperNow = 0;
      int balance = 0;
      int shopWay;
      int item;
      bool market = true;
      string errorInput = "Error, Again: ";
      bool gearBuy = true;
      int pickaxe;

      while(start != false){
      cout << "1. Start\n2. Credits\n>";
      cin >> startWay;
      if(startWay == 1){
      cout << "You awake in old house, you dont have any money but your job is to mine.\nYou can make some money and buy gear and upgrades.\n";
      Sleep(2000);
      cout << "1. Mine\n2. Market\n>";
      cin >> marketWay;
      while(marketMine != false){
      if(marketWay == 1){
      cout << endl;
      cout << "You are going mine..";
      Sleep(800);
      cout << "\nMining...." << endl;
      Sleep(1000);
      copperNow += copperGot;
      cout << "You have got > " << copperGot << " copper and your copper is > " << copperNow << ". \n" << endl;
      cout << "1. Mine\n2. Market\n>";
      cin >> marketWay;
      } else if(marketWay == 2){
      Sleep(1000);
      system("CLS");
      cout << "Welcome on market. Here you can sell ores you have mined and buy better items\nfor more ores or money.\n\n";
      Sleep(2000);
      while(market != false){
      cout << "1. Sell all copper.\n2. Balance\n3. Gear\n4. Pickaxe\n5. Mine\n>";
      cin >> shopWay;
      if(shopWay == 1){
      Sleep(1000);
      cout << "You have sold " << copperNow << " copper. You have gained " << copperNow * 2 << "$\n\n";
      balance = copperNow * 2;
      copperNow -= copperNow;
      Sleep(2250);
      system("CLS");
      }else if(shopWay == 2){
      cout << "Your balance is: " << balance << "$\n";
      Sleep(3000);
      system("CLS");
      }else if(shopWay

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #2

      The forum isn't a very good place to post large chunks of code, as the code formatting is a bit harder to get right. That said, I skimmed over the code, and without checking it in detail, there are already a few things I would advise: 1. Don't declare all variables up front. In C++, you should go by the idiom RAII (Resource acquisition is initialization - Wikipedia[^] ). That is, declare the variable when you need it, and immediately initialise it with a meaningful value. 2. Don't put everything into a single function, and most certainly not into main. main() should only contain the main control loop of the game, e. g. a simple loop that checks whether you want to continue the current game, start a new game, or exit. Continuing the game should call a function to process the next step or do whatever else your game does until the next check. Then you obviously need some kind of input and output; these should also go into separate functions. And then you need the functions that process your input and change the state of your game. 3. I see an incredible amount of 'else if' statements trying to deal with different input options, but no 'else' statements. This means that while you check for every option, you may fail to check for invalid input (more precisely: for things you didn't check in your if statements). Moreover, you're micxing checks on different variables which makes your program flow extremely hard to follow (and likely incorrect). It is much easier to structure and read your code if you use the switch() statement instead, and provide a default case to catch invalid input. 4. Games like these lend themselves very well to object-oriented programming. You have a character, a shop, a mine, and probably a few other things that can (and probably should) be implemented as a class You seem to be playing as a character who has some gear and can perforem some actions. Equipment comes in different forms, but always have a price and a name; this can be encoded in a class, or a set of classes. E. g.:

      class Equipment {
      int price_;
      string name_;
      public:
      Equipment(int price, string const& name) : price_(price), name_(name) {} // simple constructor
      virtual ~Equipment() {} // this lets you use this class as a base class
      virtual int price() const { return price_; }
      virtual

      1 Reply Last reply
      0
      • C Chleba225

        Quote:

        Hello guys, I want to share with you my mini "project" which I managed to do so I can understand c++ better and decided to make mining game with if and while loops. I am in programming for awhile so this is reason why it is not that good.

        There are things I would do different (I'm about to do it later). I want to hear your suggestions, opinions etc.. So let me know.

        Code:

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

        using namespace std;

        int main(){
        bool start = true;
        int startWay;
        bool marketMine = true;
        int marketWay;
        bool trymarket = true;
        static int copperGot = 2;
        int copperNow = 0;
        int balance = 0;
        int shopWay;
        int item;
        bool market = true;
        string errorInput = "Error, Again: ";
        bool gearBuy = true;
        int pickaxe;

        while(start != false){
        cout << "1. Start\n2. Credits\n>";
        cin >> startWay;
        if(startWay == 1){
        cout << "You awake in old house, you dont have any money but your job is to mine.\nYou can make some money and buy gear and upgrades.\n";
        Sleep(2000);
        cout << "1. Mine\n2. Market\n>";
        cin >> marketWay;
        while(marketMine != false){
        if(marketWay == 1){
        cout << endl;
        cout << "You are going mine..";
        Sleep(800);
        cout << "\nMining...." << endl;
        Sleep(1000);
        copperNow += copperGot;
        cout << "You have got > " << copperGot << " copper and your copper is > " << copperNow << ". \n" << endl;
        cout << "1. Mine\n2. Market\n>";
        cin >> marketWay;
        } else if(marketWay == 2){
        Sleep(1000);
        system("CLS");
        cout << "Welcome on market. Here you can sell ores you have mined and buy better items\nfor more ores or money.\n\n";
        Sleep(2000);
        while(market != false){
        cout << "1. Sell all copper.\n2. Balance\n3. Gear\n4. Pickaxe\n5. Mine\n>";
        cin >> shopWay;
        if(shopWay == 1){
        Sleep(1000);
        cout << "You have sold " << copperNow << " copper. You have gained " << copperNow * 2 << "$\n\n";
        balance = copperNow * 2;
        copperNow -= copperNow;
        Sleep(2250);
        system("CLS");
        }else if(shopWay == 2){
        cout << "Your balance is: " << balance << "$\n";
        Sleep(3000);
        system("CLS");
        }else if(shopWay

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        Not bad for start. It is not that uncommon to start what my boss used to called "spaghetti code". As long as your first code runs you can follow the flow and rearrange the actual code as you go. The basic concept of the application is what is important. You can identify the repetitive code and make classes or functions - as long as it continues to compile and run. Personally I look at writing code and application separately. And often I do not follow "top down " concept , but look at both application (tasks) and code as kind-off puzzle / mosaic. Do not get discouraged by "code is too big, hard to follow..." comments. I do understated that most folks just excpect to "cut and paste and run" posted code and if it does not they get frustrated. You asked for comments on your coding style and you have received very good ones, if your posted does not compile / run is immaterial. Now off the soap box and some real advise When your declare a variable get into habit to initialize it and comment (what is) its purpose. You are writing code to yourself , but do as is used to do in Chicago "vote early and often " comment and over comment what the code does - you will thank yourself later when it gets REALLY big ! Good luck

        C 1 Reply Last reply
        0
        • V Vaclav_

          Not bad for start. It is not that uncommon to start what my boss used to called "spaghetti code". As long as your first code runs you can follow the flow and rearrange the actual code as you go. The basic concept of the application is what is important. You can identify the repetitive code and make classes or functions - as long as it continues to compile and run. Personally I look at writing code and application separately. And often I do not follow "top down " concept , but look at both application (tasks) and code as kind-off puzzle / mosaic. Do not get discouraged by "code is too big, hard to follow..." comments. I do understated that most folks just excpect to "cut and paste and run" posted code and if it does not they get frustrated. You asked for comments on your coding style and you have received very good ones, if your posted does not compile / run is immaterial. Now off the soap box and some real advise When your declare a variable get into habit to initialize it and comment (what is) its purpose. You are writing code to yourself , but do as is used to do in Chicago "vote early and often " comment and over comment what the code does - you will thank yourself later when it gets REALLY big ! Good luck

          C Offline
          C Offline
          Chleba225
          wrote on last edited by
          #4

          Alright, thank you for your reply and your opinion, you motivated me little bit :D As I mentioned in my topic, I am new to programming and have to learn many things, and as I was learning through I was getting too fast on it to learn new things, I should just stop at one thing and learn it as much as I can. Anyway, thanks for reply. :)

          1 Reply Last reply
          0
          • C Chleba225

            Quote:

            Hello guys, I want to share with you my mini "project" which I managed to do so I can understand c++ better and decided to make mining game with if and while loops. I am in programming for awhile so this is reason why it is not that good.

            There are things I would do different (I'm about to do it later). I want to hear your suggestions, opinions etc.. So let me know.

            Code:

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

            using namespace std;

            int main(){
            bool start = true;
            int startWay;
            bool marketMine = true;
            int marketWay;
            bool trymarket = true;
            static int copperGot = 2;
            int copperNow = 0;
            int balance = 0;
            int shopWay;
            int item;
            bool market = true;
            string errorInput = "Error, Again: ";
            bool gearBuy = true;
            int pickaxe;

            while(start != false){
            cout << "1. Start\n2. Credits\n>";
            cin >> startWay;
            if(startWay == 1){
            cout << "You awake in old house, you dont have any money but your job is to mine.\nYou can make some money and buy gear and upgrades.\n";
            Sleep(2000);
            cout << "1. Mine\n2. Market\n>";
            cin >> marketWay;
            while(marketMine != false){
            if(marketWay == 1){
            cout << endl;
            cout << "You are going mine..";
            Sleep(800);
            cout << "\nMining...." << endl;
            Sleep(1000);
            copperNow += copperGot;
            cout << "You have got > " << copperGot << " copper and your copper is > " << copperNow << ". \n" << endl;
            cout << "1. Mine\n2. Market\n>";
            cin >> marketWay;
            } else if(marketWay == 2){
            Sleep(1000);
            system("CLS");
            cout << "Welcome on market. Here you can sell ores you have mined and buy better items\nfor more ores or money.\n\n";
            Sleep(2000);
            while(market != false){
            cout << "1. Sell all copper.\n2. Balance\n3. Gear\n4. Pickaxe\n5. Mine\n>";
            cin >> shopWay;
            if(shopWay == 1){
            Sleep(1000);
            cout << "You have sold " << copperNow << " copper. You have gained " << copperNow * 2 << "$\n\n";
            balance = copperNow * 2;
            copperNow -= copperNow;
            Sleep(2250);
            system("CLS");
            }else if(shopWay == 2){
            cout << "Your balance is: " << balance << "$\n";
            Sleep(3000);
            system("CLS");
            }else if(shopWay

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Chleba225 wrote:

            else if(startWay != 1 || 2){ cout << "\nError, try again.\n\n"; } } //while loop }else if(marketWay != 1 || 2){

            I'm sure your intent was something closer to:

            else if (startWay != 1 && startWay != 2)
            ...
            else if (marketWay != 1 && marketWay != 2)
            ...

            Otherwise the OR part of the condition would evaluate to true and display the message. Going one step further, since each of these if/else if pairs are only interested in the values 1 or 2, there's no need to check against those values at the end to determine if the message needs displaying. Just use an else by itself.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups