C++ Learning Community Forum
August 01, 2010, 02:48:25 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: First Text Based Role Playing Game (RPG)  (Read 918 times)
Cetrix
N00b!!1
*
Posts: 1


View Profile
« on: December 11, 2008, 06:41:47 PM »

Ok, i've been into C++ for a while now and still only know very little, at the moment i am working on a C++ RPG to help me boost my C++ skills in large amounts of code, i don't know if its any good yet, so i want you to decide and also to point out in ways i can improve...

Here's the code:

(Only the main.exe as the other bit is pointless which leads on to this...)
Code:
// Game Created by Cetrix
// Please don't take my code and not give me credit...
// Its not nice and just proves you can't do it yourself...
// Thank You.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int money = 0;
    int lvl = 5;
    int health = 140;
    int first = 0;
    int choice;
    int damage = 10;
    int creature1;
    int wolf_hp = 4;
    int bear_hp = 8;
    int goblin_hp = 10;
    int chicken_hp = 1;
    int damage2;
    int r;
    int wolf_def = 1;
    int bear_def = 1;
    int goblin_def = 1.5;
    int killed = 0;
    int ha_lvl = 10;
    int ha_damage = 27;
    int ha_hp = 170;
    int kill_lvl = 6;
    int kill_damage = 10;
    int kill_hp = 120;
    int glad_lvl = 6;
    int glad_damage = 12;
    int glad_hp = 140;
    int juicer_lvl = 8;
    int juicer_damage = 15;
    int juicer_hp = 200;
    int arena = 0;

        srand((unsigned)time(0));
    for(int index=0; index<1; index++){
        r = (rand()%4)+0;
    }


win:
    creature1 = r;

    cout<<"\n\n";
    cout<<"Along the road you come across a: ";
    _sleep(2000);
    if (creature1 == 0) {
        cout<<"  WOLF!";
        wolf:
        cout<<"\n WOLF has "<<wolf_hp<<" Health!";
        cout<<"\nIt attacks you and does: ";
        damage2 = rand()%14;
        cout<< damage2 <<" damage!";
        _sleep(2000);
        health = health - damage2;
        cout<<"\nYou have "<< health <<" health!";
        if ( health <=0 ) {
            cout<<" You are deaded!!\n";
        }
        else {
        damage = damage / wolf_def;
        wolf_hp = wolf_hp - damage;
        cout<<"\nYou do: "<<damage<<" damage! WOLF has: "<<wolf_hp<<" health left!";
        }   if (wolf_hp <= 0) {
            cout<<"\n\n WOLF is deaded!\n";
        }
        else {
            goto wolf;
        }
        }
    if (creature1 == 1) {
        cout<<" BEAR!";
                bear:
       cout<<"\n BEAR has "<<bear_hp<<" Health!";
        cout<<"\nIt attacks you and does: ";
        damage2 = rand()%20;
        cout<< damage2 <<" damage!";
        _sleep(2000);
        health = health - damage2;
        cout<<"\nYou have "<< health <<" health!";
        if ( health <=0 ) {
            cout<<" You are deaded!!\n";
        }
        else {
        damage = damage / bear_def;
        bear_hp = bear_hp - damage;
        cout<<"\nYou do: "<<damage<<" damage! BEAR has: "<<bear_hp<<" health left!";
        }   if (bear_hp <= 0) {
            cout<<"\n\n BEAR is deaded!\n";
        }
        else {
            goto bear;
        }
        }
    if (creature1 == 2) {
        cout<<" GOBLIN!";
                goblin:
       cout<<"\n GOBLIN has "<<goblin_hp<<" Health!";
        cout<<"\nIt attacks you and does: ";
        damage2 = rand()%40;
        cout<< damage2 <<" damage!";
        _sleep(2000);
        health = health - damage2;
        cout<<"\nYou have "<< health <<" health!";
        if ( health <=0 ) {
            cout<<" You are deaded!!\n";
        }
        else {
        damage = damage / goblin_def;
        goblin_hp = goblin_hp - damage;
        cout<<"\nYou do: "<<damage<<" damage! GOBLIN has: "<<goblin_hp<<" health left!";
        }   if (goblin_hp <= 0) {
            cout<<"\n\n GOBLIN is deaded!\n";
        }
        else {
            goto goblin;
        }
        }
    if (creature1 == 3) {
        cout<<" CHICKEN!";
                chicken:
        cout<<"\n CHICKEN has "<<chicken_hp<<" Health!";
        cout<<"\nIt attacks you and does: ";
        damage2 = rand()%150;
        cout<< damage2 <<" damage!";
        _sleep(2000);
        health = health - damage2;
        cout<<"\nYou have "<< health <<" health!";
        if ( health <=0 ) {
            cout<<" You are deaded!!\n";
            exit(0);
        }
        else {
        damage = damage;
        chicken_hp = chicken_hp - damage;
        cout<<"\nYou do: "<<damage<<" damage! Chick has: "<<chicken_hp<<" health left!";
        }   if (chicken_hp <= 0) {
            cout<<"\n\n CHICKEN is deaded!\n";
        }
        else {
            goto chicken;
        }
        }
        cout<<"==================================================================\n";
        cout<<"\n\n Congrats! You killed it!!";
        killed = killed + 1;
        lvl = lvl + 1;
        health = health + 10;
        money = money + 100;
        cout<<"\nYou are now lvl: "<< lvl <<" with "<< health <<" health! You also gained: "<<money<<" silvers!";
        _sleep(1000);
        cout<<"\n\nAfter cleaning your blades, you look up!!";
        cout<<"\nWithout realising you have made it it 'Ashrock'!";
        goto ashrock;

ashrock:

    int choice2;

    cout<<"\n\n";
    cout<<"==================================================================\n\n";
    cout<<"Welcome to Ashrock! Where are you going?";
    cout<<"\n\n";
    cout<<"1. Red Moon Inn.\n";
    cout<<"2. Training.\n";
    cout<<"3. Dueling Arena.\n";
    cout<<"4. Leave Ashrock.\n";
    cout<<"::: ";
    cin>> choice2;
    cout<<"\n==================================================================\n\n";

    if (choice2 == 1) {goto rm_inn;}
    if (choice2 == 2) {goto train_ashrock;}
    if (choice2 == 3) {goto arena_ashrock;}
    if (choice2 == 4) {goto leave_ashrock;}

rm_inn:
    int choice_inn_ashrock;

    cout<<"\n\n";
    cout<<"==================================================================\n";
    cout<<"Welcome to Red Moon Inn!";
    cout<<"\nWhat do you want to do?";
    cout<<"\n1. Get a room (5 silvers)";
    cout<<"\n2. Leave";
    cout<<"\n ::: ";
    cin>> choice_inn_ashrock;
    cout<<"\n==================================================================\n\n";

    if (choice_inn_ashrock == 1) {
        if (money <=0) {
            cout<<"You can't afford it! Get out!";
            goto ashrock;
        }
        else {
        cout<<"\n\n";
        cout<<"==================================================================\n";
        cout<<"You get a room and rest...";
        health = health + 3;
        money = money - 5;
        cout<<"You now have: "<< health <<" health! and "<<money<<" silvers!";
        cout<<"\n==================================================================\n\n";
        goto rm_inn;
    }
    }
    if (choice_inn_ashrock == 2) {goto ashrock;}

train_ashrock:
    int train_choice;

    cout<<"\n\n";
    cout<<"==================================================================\n";
    cout<<"Welcome to Training!\n";
    cout<<"\nWhat do you want to do?";
    cout<<"\n1. Train.";
    cout<<"\n2. Leave.";
    cout<<"\n ::: ";
    cin>> train_choice;
    cout<<"\n==================================================================\n\n";

    if (train_choice == 1) {
        if (lvl >= 10) {
            cout<<"==================================================================\n";
            cout<<"Sorry, you are too high to train here!\n";
            cout<<" You should try the arena!\n";
            cout<<"==================================================================\n\n";
            goto ashrock;
        }
        if (lvl <= 9) {
        cout<<"\n\n";
        cout<<"==================================================================\n";
        cout<<"You train hard!";
        lvl = lvl + 1;
        health = health + 10;
        cout<<"\n You are level: "<<lvl<<" with "<<health<<" health!\n";
        cout<<"==================================================================\n\n";
        goto train_ashrock;
        }
    }
    if (train_choice == 2) {
        cout<<"\n\n==================================================================\n";
        cout<<"You leave the training area!\n";
        cout<<"==================================================================\n\n";
        goto ashrock;
        }

arena_ashrock:
    int arena_ashrock_choice;
    int opponent;

            srand((unsigned)time(0));
    for(int index=0; index<1; index++){
        opponent = (rand()%4)+0;
    }

    cout<<"\n\n";
    cout<<"==================================================================\n";
    cout<<"Welcome to Arena!";
    cout<<"\nWhat do you want to do?";
    cout<<"\n1. Fight a duel (TO THE DEATH)";
    cout<<"\n2. Leave.";
    cout<<"\n ::: ";
    cin>> arena_ashrock_choice;
    cout<<"\n==================================================================\n\n";

    if (arena_ashrock_choice == 1) {
        if (arena == 1) {
            cout<<"As you've won a duel you cannot duel again...";
            goto ashrock;
        }
        else {
        cout<<"\n\n";
        damage = (lvl * lvl) / 3;
        cout<<"==================================================================\n";
        if (opponent == 0) {
        cout<<"You will be fighting: The Killer!\n";
        cout<<"He's Lvl: "<<kill_lvl<<" and does: "<<kill_damage<<" damage each hit!\n";
        cout<<"You enter the Arena!\n";
        fight1:
            if (kill_hp <=0 ) {
            cout<<"\nOpponent is dead!\n";
            lvl = lvl + 1;
            health = health + 10;
            cout<<"\nYou lvled up to: "<<lvl<<"!";
            cout<<"\nHere's your prize money: 1000 silvers\n";
            money = money + 1000;
            cout<<"You now have: "<<money<<" silvers!\n\n";
            cout<<"\n==================================================================\n\n";
        arena = arena + 1;
        goto arena_ashrock;
        }
            else {
        cout<<"\n Opponent Attacks!\n";
        health = health - kill_damage;
        cout<<"\nHealth Left: "<<health;
        if (health <=0 )
            {cout<<"\nYou are deaded!\n";
            cin.get();
            exit(0);
            }
        _sleep(2000);
        cout<<"\nYou attack!\n";
        kill_hp = kill_hp - damage;
        cout<<"\nOpponent Health: "<<kill_hp;
        _sleep(2000);
        goto fight1;
        }
        }
        if (opponent == 1) {
        cout<<"You will be fighting: The Gladiator!\n";
        cout<<"He's Lvl: "<<glad_lvl<<" and does: "<<glad_damage<<" damage each hit!\n";
        cout<<"You enter the Arena!\n";
        fight2:
        if (glad_hp <=0 ) {
            cout<<"\nOpponent is dead!\n";
            lvl = lvl + 1;
            health = health + 10;
            cout<<"\nYou lvled up to: "<<lvl<<"!";
            cout<<"\nHere's your prize money: 1000 silvers\n";
            money = money + 1000;
            cout<<"You now have: "<<money<<" silvers!\n\n";
            cout<<"\n==================================================================\n\n";
        arena = arena + 1;
        goto arena_ashrock;
        }
            else {
        cout<<"\n Opponent Attacks!\n";
        health = health - glad_damage;
        cout<<"\nHealth Left: "<<health;
        if (health <=0 )
            {cout<<"\nYou are deaded!\n";
            cin.get();
            exit(0);
            }
        _sleep(2000);
        cout<<"\nYou attack!\n";
        glad_hp = glad_hp - damage;
        cout<<"\nOpponent Health: "<<glad_hp;
        _sleep(2000);
        goto fight2;
        }
        }
        if (opponent == 2) {
        cout<<"You will be fighting: The Juicer!\n";
        cout<<"He's Lvl: "<<juicer_lvl<<" and does: "<<juicer_damage<<" damage each hit!\n";
        cout<<"You enter the Arena!\n";
        fight3:
        if (juicer_hp <=0 ) {
            cout<<"\nOpponent is dead!\n";
            lvl = lvl + 1;
            health = health + 10;
            cout<<"\nYou lvled up to: "<<lvl<<"!";
            cout<<"\nHere's your prize money: 1000 silvers\n";
            money = money + 1000;
            cout<<"You now have: "<<money<<" silvers!\n\n";
            cout<<"\n==================================================================\n\n";
        arena = arena + 1;
        goto arena_ashrock;
        }
            else {
        cout<<"\n Opponent Attacks!\n";
        health = health - juicer_damage;
        cout<<"\nHealth Left: "<<health;
        if (health <=0 )
            {cout<<"\nYou are deaded!\n";
            cin.get();
            exit(0);
            }
        _sleep(2000);
        cout<<"\nYou attack!\n";
        juicer_hp = juicer_hp - damage;
        cout<<"\nOpponent Health: "<<juicer_hp;
        _sleep(2000);
        goto fight3;
        }
        }
        if (opponent == 3) {
        cout<<"You will be fighting: Ha## Tur####!\n";
        cout<<"He's Lvl: "<<ha_lvl<<" and does: "<<ha_damage<<" damage each hit!\n";
        cout<<"Unlucky, you may not return alive...\n";
        cout<<"\nYou enter the Arena!";
        fight4:
        if (ha_hp <=0 ) {
            cout<<"\nOpponent is dead!\n";
            lvl = lvl + 1;
            health = health + 10;
            cout<<"\nYou lvled up to: "<<lvl<<"!";
            cout<<"\nHere's your prize money: 1000 silvers\n";
            money = money + 1000;
            cout<<"You now have: "<<money<<" silvers!\n\n";
        cout<<"\n==================================================================\n\n";
        arena = arena + 1;
        goto arena_ashrock;
        }
            else {
        cout<<"\n Opponent Attacks!\n";
        health = health - ha_damage;
        cout<<"\nHealth Left: "<<health;
        if (health <=0 )
            {cout<<"\nYou are deaded!\n";
            cin.get();
            exit(0);
            }
        _sleep(2000);
        cout<<"\nYou attack!\n";
        ha_hp = ha_hp - damage;
        cout<<"\nOpponent Health: "<<ha_hp;
        _sleep(2000);
        goto fight4;
        }
        }
    }
    if (arena_ashrock_choice == 2) {
        goto ashrock;
        }
    }

leave_ashrock:
    int leave;

    cout<<"\n\n";
    cout<<"==================================================================\n";
    cout<<"You leave Ashrock!\n";
    cout<<"\nWhere are you going?";
    cout<<"\n1. Ashmere.";
    cout<<"\n2. Haves.";
    cout<<"\n ::: ";
    cin>> leave;

    if (leave == 1) {
        if (lvl >= 10) {
            cout<<"==================================================================\n";
            cout<<"\n\nFarwell, Friend!";
            goto ashrock;
        }
        else {
            cout<<"==================================================================\n";
            cout<<"\n\nYou are too low to go there! Please stay and train!";
            goto ashrock;
        }
    }
    cin.get();
}

Like i said any help would be highly appreciated and i am still very much a begginer...

Also a D/L link:

http://sharemino.com/file/254/TheExiled-rar.html

And i would appreciate it if someone could tell me how to get the installer to make a shortcut and everything, cos at the moment it only puts the files in directory. Also How do i change the .exe's icon?

(Sorry im asking for soo much help but i need it =D)
« Last Edit: December 11, 2008, 07:51:24 PM by Cetrix » Logged
mleveill
1337 L0LCoder
C++ Freak
***
Posts: 430


IM IN UR PROGRAMZ WRITIN UR CODEZ!


View Profile
« Reply #1 on: December 11, 2008, 09:35:17 PM »

if you want to improve your skills, get rid of all the goto statements, you don't need ANY Smiley
try modularizing your code (break it up into different functions)

also, try using constant integers for defining things (instead of having to remember that 0=wolf, 1=bear, etc)


example (untested):

Code:
#include <stdlib.h>
#include <stdio.h>

const int WOLF = 0;
const int BEAR = 1;
const int TOTAL_ANIMALS = 2;

void printAnimal(int animal) {
    if (animal == WOLF) {
        std::cout << "wolf";
    } else if (animal == BEAR) {
        std::cout << "bear";
    }
}

void getAttacked(int animal) {
    std::cout << "Got attacked by a ";
    printAnimal(animal);
    std::cout << "!!!" << std::endl;
}

int main() {
    srand((unsigned)time(0));
    getAttacked(rand() % TOTAL_ANIMALS);
    getAttacked(rand() % TOTAL_ANIMALS);
    getAttacked(rand() % TOTAL_ANIMALS);
    return 0;
}

in my example, you could also use a 'switch' statement instead of 'if-else' statements (if you know how to do that)
Logged

Quote from: kohlrak
Common interests aren't always the best thing. Especially programming wise.
I could just picture a couple fighting over weather to "xor eax by eax" or "and eax by 0"...
Then kids'll come home and argue with their parents that readabilty is better than size and that "mov eax, 0" would be best because that's "more readable." (Even if it really isn't...)

LOLCode - http://lolcode.com/home
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #2 on: December 11, 2008, 10:13:27 PM »

if you want to improve your skills, get rid of all the goto statements, you don't need ANY Smiley


whats wrong with GOTO?
Logged
sofaking
Jr. Nerd
***
Posts: 44



View Profile
« Reply #3 on: December 11, 2008, 10:32:25 PM »

whats wrong with GOTO?



XKCD
Logged

My games and programs: Pong Clone - Connect 4 - SOFA-8 - SOFABOY - Raycasting Demo
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #4 on: December 11, 2008, 10:34:56 PM »


i see. o.0 (dosnt want dinosaur to run over his computer) guess ima stay away from goto

but i still think in shorter programs its not bad. just would be annoying in REALLy long ones
Logged
mleveill
1337 L0LCoder
C++ Freak
***
Posts: 430


IM IN UR PROGRAMZ WRITIN UR CODEZ!


View Profile
« Reply #5 on: December 11, 2008, 11:40:48 PM »

Quote
but i still think in shorter programs its not bad.
no, not at all Wink

it MIGHT be understandable to use in a short function if you had no other reasonable choice.
this might occur if... you have a few nested loops or something and it makes the code more readable.
there was a topic on this a little while back.... found it: http://www.cpplc.net/forum/index.php/topic,625.0.html although this example isn't a very severe case

high five for xkcd reference Smiley
Logged

Quote from: kohlrak
Common interests aren't always the best thing. Especially programming wise.
I could just picture a couple fighting over weather to "xor eax by eax" or "and eax by 0"...
Then kids'll come home and argue with their parents that readabilty is better than size and that "mov eax, 0" would be best because that's "more readable." (Even if it really isn't...)

LOLCode - http://lolcode.com/home
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #6 on: December 11, 2008, 11:47:26 PM »

Quote
but i still think in shorter programs its not bad.
no, not at all Wink

it MIGHT be understandable to use in a short function if you had no other reasonable choice.
this might occur if... you have a few nested loops or something and it makes the code more readable.
there was a topic on this a little while back.... found it: http://www.cpplc.net/forum/index.php/topic,625.0.html although this example isn't a very severe case

high five for xkcd reference Smiley

hmm i see what you mean. while googling GOTO's i came across this http://www.ifi.uzh.ch/req/courses/kvse/uebungen/Dijkstra_Goto.pdf  explaines alot
Logged
biggoron
C++ Freak
***
Posts: 351


View Profile
« Reply #7 on: December 11, 2008, 11:47:55 PM »

Quote
high five for xkcd reference
It's getting easier these days.
Logged

Shonoby
Programmer
Dr. of C++ology
****
Posts: 659


Pixel Artist


View Profile
« Reply #8 on: December 12, 2008, 01:27:25 AM »

It's been a while since i have seen anyone else make an RPG, i made one back in the day of the old forums. It was the first post i ever made too, so you remind me of me. It was basic and use some methods you used, but it used functions and loops. Here is the link.
« Last Edit: December 12, 2008, 01:32:52 AM by Shonoby » Logged

Reality is what we make of it, so by definition we're all living in a fantasy.
- Shonoby
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!