• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KiNgSGB

My First Text Based RPG help

10 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

1

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.

0

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
0

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.

0

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
0

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!

2

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.

0

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.

0

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)
{
    //...
}
0

Share this post


Link to post
Share on other sites

Thanks for all the help guys. I'm starting to understand a bit more about c++. Need some work on functions but I'll get there eventually.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0