Please Help Me Out Or Look Over My Source Code

Started by
6 comments, last by pulpfist 14 years, 4 months ago
this is a basic rpg that I'm trying to make in c++. It's only in the very rought draft version as of yet but I can't get it to work the way I want it to!!! when it goes to the fight function, no matter what the sword choice is, it just uses longsword!!! give it a run in your compiler and you can find more bugs further down the line too!!! heres the source code: (any comments about my coding style greatly appreciated!!!)
#include <iostream>
#include <string>
using namespace std;
void cityLongsword();
void cityShortsword();
void fight();
void welcome();
void displayMenu();
void welcome()
{
    string fightOption;
    string name;
    cout<<"\t\t\t\tWelcome to my RPG";
    cout<<"\nGive yourself a name: ";
    cin>>name;
    cout<<"Welcome "<<name;
    cout<<"\n\nAs the hero is walking into the village\na monster comes out and begins to attack!";
    cout<<"\n\nDo you want to fight or run\? ";
    cin>>fightOption;
    if (fightOption=="fight")
    {
        cout<<"\n\nThe fight has begun!!!";
        fight();
    }
    if (fightOption=="run")
    {
        cout<<"\n\nYou don't manage to escape, you have no choice but to fight!!!";
        fight();
    }
}
void fight()
{
    string swordType;
    cout<<"\nThe monster inflicts 2 damage to you.";
    cout<<"\nYour HP is now 8!!!";
    cout<<"\nChoose your weapon: ";
    cout<<"\n\nSword Type:";
    cout<<"\n===========";
    cout<<"\nLongsword";
    cout<<"\nShortsword";
    cout<<"\nDagger\n\n";
    cin>>swordType;
    if(swordType=="longsword"||"l")
    {
        cout<<"\nYou inflict 3 damage to the monster.";
        cout<<"\nThe monster runs away for fear of being hurt more";
        cout<<"\nYou walk inside the city walls victorious!!!";
        cityLongsword();
    }
    if(swordType=="shortsword"||"s")
    {
        cout<<"\nYou inflict 2 damage to the monster.";
        cout<<"\nThe monster bites you and you lose 4 HP!!!";
        cout<<"\nThe monster walks away, happy with its meal of your arm.";
        cout<<"\nYou limp inside the city walls";
        cityShortsword();
    }
    if (swordType=="dagger"||"d")
    {
        string again;
        cout<<"\nYour trusty dagger barely scratches the monsters scales.";
        cout<<"\nYou have only made him angrier!!!";
        cout<<"\nThe monster kills you with one swipe of it's claw!!!";
        cout<<"\n\nYou have died, play again? ";
        cin>>again;
        if (again=="yes")
        {
            welcome();
        }
        if (again=="no")
        {
        cout<<"exiting...";
        }
    }

}
void cityLongsword()
{
    string menuOption;
    cout<<"\n\nAs you venture inside the city,\nyou realise that you are being followed by someone";
    cout<<"\nYou hear someone call your name and as you turn around,\nyou are hit and you fall to the ground";
    cout<<"\nWhen you wake up... You have lost all your belongings and you don't know where you are!!!";
    cout<<"\nYou stand up and start picking up coins scattered on the ground";
    cout<<"\n\n======================================";
    cout<<"\nYou have gained 20 gold!!!";
    cout<<"\n======================================";
    cout<<"\n\nGo to the menu\? ";
    cin>>menuOption;
    if (menuOption=="yes"||"y")
    {
        displayMenu();
    }
    if (menuOption=="no"||"n")
    {
        cout<<"\n\nWhat do you want to do now? ";
    }
}
void cityShortsword()
{
    cout<<"\n\nAs you limp in, people rush over to bandage your arm.";
}
void displayMenu()
{
    string menuOption;
    cout<<"\n\n==================";
    cout<<"\nBuy Armour";
    cout<<"\nGo To The Arena";
    cout<<"\n===================\n";
    cin>>menuOption;
}
int main()
{
    string fightOption;
    string name="hero";
    cout<<"\t\t\t\tWelcome to my RPG";
    cout<<"\nGive yourself a name: ";
    cin>>name;
    cout<<"Welcome "<<name;
    cout<<"\n\nAs the hero is walking into the village\na monster comes out and begins to attack!";
    cout<<"\n\nDo you want to fight or run\? ";
    cin>>fightOption;
    if (fightOption=="fight")
    {
        cout<<"\n\nThe fight has begun!!!";
        fight();
    }
    if (fightOption=="run")
    {
        cout<<"\n\nYou don't manage to escape, you have no choice but to fight!!!";
        fight();
    }
    return 1;
}

I know it's not commented whatsoever but if someone would look over it for me it would be greatly appreciated!!! Any ideas on what else to put in the game or how to structure it would be good too!!! thanks for your time...
Advertisement
wrong:if(swordType=="longsword"||"l")correct:if(swordType=="longsword"||swordType=="l")
While DevFred addressed your most immediate concern, you also requested help with improving the structure of your code. The first thing that strikes me is that the way you're going with your game is going to make it extremely difficult to maintain. The reason for this is that rather than representing the state of the game as data (variables), you are writing redundant code with a high frequency of repetition so that it can handle any given state the game is in. In fact, most of the functions in your game completely disregard any "progress" the player has made so far. A better place to start would be to represent the player object as a simple class or structure, and then pass this piece of information to the functions so that they may use it to determine the state of the player, and then apply the changes they make so that the next function will have a correct view of the progress of the game.

Here is an example of what I'm talking about. Don't be freaked out if this seems alien to you. Read up on C++ classes if needed - you'll need it in the long run anyway.

#include <iostream>#include <string>class Player{public:    Player(const std::string& name)         : m_name(name), m_hitPoints(100)    {}    void addHitPoints(int count)    { m_hitPoints += count; }    void removeHitPoints(int count)    {        m_hitPoints -= count;        if(m_hitPoints <= 0)            die();    }    void die()     { std::cout << "You are dead." << std::endl; }    unsigned int getHitPoints() const     { return m_hitPoints; }    const std::string& getName() const    { return m_name; }private:    std::string m_name;    int m_hitPoints;};// Here's an example function that might operate on the player object.void hurtPlayer(Player& player){    // Hurts player for 10 hit points.    player.removeHitPoints(10);}
The suggestions mentioned so far is all good but there is one thing you absolutely must do at this point, and that is to set up a game loop. All games, from WoW to Tetris, has one.

The game loop typically resides in the main function and will work as the "heartbeat" of the game.

Here is a sketch to give you an idea

string command;while(true) // This is the game loop, running forever{	command = displayMenu();	if(command == "quit")	{		break; // Terminate the game loop and quit the game	}	else if(command == "fight")	{		fight();	}	else if(command == "run")	{		cout << "You can not run, you have to fight..." << endl;		fight();	}	else	{		cout << "Unknown command " + command << endl;	}}
Thanks for all your help guys!!!
Player class is definetly needed alright!!! ammmmm I'm a bit unsure about classes but I could understand most of that one windryder!! (OMG I might be learning something!!!)

thanks very much, I was getting fairly confused with all the functions!!!
I think I'll make classes for player, monster, mugger, fight and mabye menus??
also, is it possible to make a sub-class?? something like monster and mugger classes being a part of an enemy class??

I might post an improvement and maybe ye could tell me what else to change?
Thanks again!!!
Quote:Original post by soitsthateasy
[...] is it possible to make a sub-class?? something like monster and mugger classes being a part of an enemy class??


It most certainly is; it's called inheritance. Inheritance is one of the most fundamental concepts in Object Oriented programming (C++ is an OOP language). While I'm not going to try to fully explain inheritance here, the basic idea is that a subclass "inherits" the methods and data of its parent class. This means that any function that you can call on the parent class, is also valid on the subclass. Syntactically, it might look something like this:

class Enemy{   // ... "Generic" enemy data/code here ...   };class Wolf : public Enemy{   // ... Data and code specific to Wolf here ...};


I'm happy to hear that you feel like you are learning! It is very important if you want to stay motivated.
@pulpfist: Would that be like for the main menu??

so would this be a good example of a class? (very basic, I know. But I have to start somewhere!!!)

#include <iostream>#include <string>using namespace std;int playersHp;class cPlayer{    public:    void setName(string nameInput)    {        name=nameInput;    }    int hp()    {        return playersHp;    }    int damage(int playersHp, int damage)    {        int oldHp;        int newHp;        oldHp=playersHp;        newHp=oldHp-damage;        return newHp;    }    string getName()    {        return name;    }    private:    string name;};int main(){    cPlayer hero;    string nameInput;    cout<<"welcome to my RPG\n";    cout<<"Enter a character name: ";    cin>>nameInput;    hero.setName(nameInput);    cout<<"Hello " <<hero.getName() <<". Enjoy the game!";    return 0;}


Thanks for all your help!!!

also, how do you colour text and how could I make the muggers armour for the higher the armour value, the lower the damage he recieves??

Thanks again!!!!!!!!

[Edited by - soitsthateasy on December 6, 2009 10:44:42 AM]
Quote:Original post by soitsthateasy
@pulpfist: Would that be like for the main menu??

so would this be a good example of a class? (very basic, I know. But I have to start somewhere!!!)

*** Source Snippet Removed ***

Thanks for all your help!!!

also, how do you colour text and how could I make the muggers armour for the higher the armour value, the lower the damage he recieves??

Thanks again!!!!!!!!


Well it could be, but you could use the same idea to create sub menus of course.
The reason I suggested a game loop was basically when I saw that you were calling the fight function from the welcome function and the welcome function from the fight function. Crisscrossing function calls like that will hurt you very bad very soon, and make the code hard to maintain.

What could happen is that the program enters the welcome function and then jumps into the fight function, and then jumps into a new welcome function (the last one didn't complete and is still waiting around on the stack)
This is not a good thing and will eventually lead to unpredicted behaviour or worse.
My bottom line is; create functions that do one specific thing and let those functions finish before moving on to the next game cycle. Using menu-driven loops is one way that can help you do that

[Edited by - pulpfist on December 6, 2009 5:19:47 PM]

This topic is closed to new replies.

Advertisement