Jump to content
  • Advertisement
Sign in to follow this  
KiNgSGB

My First Text Based RPG help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I say this it is not a very good game at all. I actually have a problem that I need some input on. I know the error but I just don't know how to fix it. Here is the entire code

 

 

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
    char name[100];
    cout<<"\t\tGreenwood"<< endl;
    cout<<"Welcome stranger, what is your name?:"<< endl;
    cin>>name;
    cout<<"Welcome "<<name<<"\n";
 
    cout<<"Please select a race: \n";
    cout<<"1. Human \n";
    cout<<"2. Orc \n";
    int pickRace;
    cout<<" Pick your race: ";
    cin>>pickRace;
 
    switch(pickRace)
    {
    case 1:
        cout<<"You have chosen to be a human.\n";
        break;
    case 2:
        cout<<"You have chosen to be an orc.\n";
        break;
    default:
        cout<<"Invalid input, you many only select 1 or 2/.\n";
    }
 
    int difficulty;
    cout<<"Select a difficulty: "<< endl;
    cout<<"1.Easy"<< endl;
    cout<<"2.Medium"<< endl;
    cout<<"3.Hard"<< endl;
 
    cout<<"Pick your difficulty: "<< endl;
    cin>>difficulty;
 
    switch(difficulty)
    {
    case 1:
        cout<<"You have picked Easy."<< endl;
        break;
    case 2:
        cout<<"You have picked Medium."<< endl;
        break;
    case 3:
        cout<<"You have picked Hard."<< endl;
        break;
    default:
        cout<<"Invalid input."<< endl;
        break;
    }
 
    int choice;
    int attack = 2;
    int cHealth;
    int eHealth = 8;
    int yn;
    int stats;
    int looper = 7;
 
    while (looper > 1)
    {
    cout<<"Select a command: "<< endl;
    cout<<"1.Fight"<< endl;
    cout<<"2.Stats"<< endl;
    cout<<"Select: ";
    cin>>choice;
 
    switch (choice)
    {
    case 1:
        cout<<"Attacked by a Goblin! Attack back?"<< endl;
        cout<<"1.Yes"<< endl;
        cout<<"2.No"<< endl;
        cin>>yn;
        switch (yn)
        {
        case 1:
            while (eHealth > 0)
            {
            eHealth - attack;
            if (eHealth <= 0)
            {
                 cout<<"You have killed the goblin."<< endl;
            cout<< endl;
            ++stats;
            break;
            }
            }
 
        case 2:
            cout<<"You have died."<< endl;
            break;
        }
        break;
 
    case 2:
        cout<<"Level "<<stats<< endl;
        cout<<" "<< endl;
        cout<<" "<< endl;
        break;
 
 
 
 
 
    }
 
 
 
 
 
 
 
 
 
 
}
}
 
 
 
 
 
The problem is at:
 
 
 cout<<"Select a command: "<< endl;
    cout<<"1.Fight"<< endl;
    cout<<"2.Stats"<< endl;
    cout<<"Select: ";
    cin>>choice;
 
    switch (choice)
    {
    case 1:
        cout<<"Attacked by a Goblin! Attack back?"<< endl;
        cout<<"1.Yes"<< endl;
        cout<<"2.No"<< endl;
        cin>>yn;
        switch (yn)
        {
        case 1:
            while (eHealth > 0)
            {
            eHealth - attack;
            if (eHealth <= 0)
            {
                 cout<<"You have killed the goblin."<< endl;
            cout<< endl;
            ++stats;
            break;
            }
            }
 
        case 2:
            cout<<"You have died."<< endl;
            break;
        }
        break;
 
    case 2:
        cout<<"Level "<<stats<< endl;
        cout<<" "<< endl;
        cout<<" "<< endl;
        break;
 
 
 
 
 
    }
 
 
If I take out the part with while (eHealth > 0). I can get it to work fine, but I would like a health system. I need some help with the health system. By the way I'm also pretty new to programming so I may not understand some terms.
Edited by KiNgSGB

Share this post


Link to post
Share on other sites
Advertisement

I am not sure on your problem but one thing that caught my attention was the line

 eHealth - attack;

Did you mean...

 

eHealth -= attack;

 

I believe this is your problem.

Share this post


Link to post
Share on other sites

Ok thanks but now the problem is the the switch goes through case 1 and 2. So instead of just killing the goblin and returning to the beginning, it also says you have died. How would I fix that.

Share this post


Link to post
Share on other sites

What happens when your code reaches this block of code?

 

while (eHealth > 0)
{
    eHealth - attack;
    if (eHealth <= 0)
    {
         cout<<"You have killed the goblin."<< endl;
         cout<< endl;
         ++stats;
         break;
     }
}

 

Think of it in isolation a second. Once your execution enters into the while() statement, it is stuck there until you let it leave.

As far as the flow of logic goes, just have one main loop, and branch the flow of logic by having different "states" your game is in.

 

 

while(gameIsRunning)
{
     if(gameState == IN_COMBAT)
     {
           //Do something...
     }
     else if(gameState == EXPLORING)
     {
           //Do something else...
     }
     else if(gameState == IN_SHOP)
     {
           //Do something different...
     }
}

 

I'm not saying only have one loop period, but that conceptually you should have one main loop of logic, and not let execution get "stuck" in different loops elsewhere (there are reasons to have separate logic loops, but rarely).

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

Ok so I understand what your saying there, but how do i declare each thing (gameIsRunning, IN_SHOP, EXPLORING, IN_SHOP). Sorry if it's a very noob question, but I'm still learning.

Share this post


Link to post
Share on other sites
Since gameIsRunning is inside a condition, what type of variable do you think it is?

You should learn how to define a function and call a function immediately. Functions are used to contain your code so your program has better structure.

You should also have better variable for the health variables. cHealth and eHealth and especially yn is a bad variable name. Make general variable names. Edited by warnexus

Share this post


Link to post
Share on other sites

For those of us that cant read code formatted like that:

http://codebin.org/view/94bf6d09

 

Ok I am confused what you actually what the program to do so I am just going to stab in the dark.

 

while (eHealth > 0)
{
	eHealth - attack;

	if (eHealth <= 0)
	{
		cout<< "You have killed the goblin."<< endl;
		cout<< endl;
		++stats;
		break;
	}
}

In your loop you are doing eHealth - attack.

 

So lets think about this, your doing the golbins health minus attack which is 1. Now you never store the 1 and it is just forgotten about. Now ehealth is greater than 0 because its still 8 because it is never saved. Now the program will go into an infinite loop.

 

However lets fix this and do eHealth = eHealth - attack;

First loop:

  eHealth = 1;

  do if statement = no

Second loop:

  eHealth = -6;

  do if statement = yes

    output "You killed the goblin \n"

    add one to stats;

    break while loop;

 

Additionally stats was never assigned so you will get a runtime failure, so if you change it to int stats = 0;

 

Ok your next question is going to be it comes up with but it also printed "You have died" this is becuase you have forgotten to add a break statement for the case of attacking the goblin. So it will execute case1 which you kill the goblin, it never finds a break to break the case so it does case2 also then case2 finds a break and finishs.

 

Hope this is enough information and good luck!

Share this post


Link to post
Share on other sites

Not really about your code to help you out, but to help you out later on when posting code here.  
 
When using the advanced editor (BBCODE Mode turned off) their is a GUI button that looks like this: <>.  The code button.  You can put your code in that so your code will be formatted.
 
If just using BBCODE, You can just do the code tags.

Share this post


Link to post
Share on other sites

I hope that by now you should have figured out what your problem is but just to add a few comments, you would greatly improve your code if you learned functions, as Servant of the Lord said. Using that structure you can find where your problem is much easily.

Also, learn how to use the debugger from your compiler to see the values of your variables and to see why you are not getting the results that you expect.

And lastly, as Chad Smith also said, use the code tags to post your code here. It makes the code much more readable for us here. I also don't know if you used indentation on your code or not but it is another tip to make it more readable.

Don't be frightened if you are just starting to code. It seems a lot to grasp just to make a text based rpg but if you follow these tips you will notice how much faster you will find errors in your code.

Share this post


Link to post
Share on other sites

Ok so I understand what your saying there, but how do i declare each thing (gameIsRunning, IN_SHOP, EXPLORING, IN_SHOP). Sorry if it's a very noob question, but I'm still learning.

 

The same way you declare your other variables:

bool gameIsRunning = true;

 

You can use an enum for states:

enum GameState {Exploring, Fighting, Shopping};
 
GameState currentGameState = Exploring;
 
if(currentGameState == Exploring)
{
    //...
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!