Right, i'm REALLY new to this whole thing, coding and everythiong. But for my first proper project I've decided to make a Text Based RPG. I've got most of it done now, but am having problms working out how to make random damage from the enemies. At the moment, each Enemy does a set ammount of damage, and I want to be able to have aa set of two or three ammounts of dammage per enemy. here's the code for my battles:

#include <stdlib.h>
#include <iostream>

using namespace std;

int healthpotion = 5;
int magicpotion = 5;
int firespell;
int lightningspell;
int strength = 5;
int health = 15;
int healthmax = 15;
int magicdmg = 2;
int magiclvl = 4;
int magicmax = 4;
int enhealth;
int enattack;
int choice;
void battlechoice();


void goblinbattle()
{
    cout << "Player VS Goblin\n\nWhat will you do?\n1: Attack           " << strength << " Dammage\n2: Magic            " << magicdmg << " Dammage\n3: Potions\n";
    cin >> choice;
    if ( choice == 1 )
    {
        enhealth = enhealth - strength;
        system("cls");
        cout << "You Attack the Goblin!\n\nThe Goblin now has: " << enhealth << "/20 life left.\n";
        cin.get();
        cin.ignore();
        system("cls");
        if ( enhealth <= 0 )
        {
            cout << "WELL DONE! You Killed the Goblin!";
            cin.get();
            system("cls");
            battlechoice();
        }
        else
        {
            health = health - enattack;
            cout << "The Goblin attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                goblinbattle();
            }
        }
    }
    if ( choice == 2 )
    {
        if (magiclvl >=2){
        magiclvl = magiclvl - 2;
        enhealth = enhealth - magicdmg;
        system("cls");
        cout << "You Use Magic on the Goblin! You now have " << magiclvl << "/4 magic left.\n\nThe Goblin now has: " << enhealth << "/20 life left.\n";
        cin.get();
        cin.ignore();
        system("cls");
        if ( enhealth <= 0 )
        {
            cout << "WELL DONE! You Killed the Goblin!";
            cin.get();
            system("cls");
            battlechoice();
        }
        else
        {
            health = health - enattack;
            cout << "The Goblin attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                goblinbattle();
            }
        }
        }
        else
        {
            system("cls");
            cout << "You Don't Have Enough Magic for That!\n";
            cin.get();
            cin.ignore();
            system("cls");
            goblinbattle();
        }
    }
    else if ( choice == 3 )
    {
        system("cls");
        cout << "Your Potions:\n1: Health Potion         " << healthpotion << " left\n2: Magic Potion          " << magicpotion << " left\n3: Cancel\n";
        cin >> choice;
        if ( choice == 1 )
        {
            if ( healthpotion > 0 )
            {
            system("cls");
            healthpotion = healthpotion - 1;
            health = healthmax;
            cout << "You are fully healed!\n";
            cin.get();
            cin.ignore();
            system("cls");
            health = health - enattack;
            cout << "The Goblin attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                goblinbattle();
            }
            }
            else
            {
                system("cls");
                cout << "You don't have any of this Potion!\n";
                cin.get();
                cin.ignore();
                goblinbattle();
            }
        }
        else if ( choice == 2 )
        {
            if ( magicpotion > 0 )
            {
            system("cls");
            magiclvl = magicmax;
            cout << "Your magic is fully restored!\n";
            cin.get();
            cin.ignore();
            system("cls");
            health = health - enattack;
            cout << "The Goblin attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                goblinbattle();
            }
        }
        else
            {
                system("cls");
                cout << "You don't have any of this Potion!\n";
                cin.get();
                cin.ignore();
                goblinbattle();
            }
        }
        else if ( choice == 3 )
        {
            system("cls");
            goblinbattle();
        }
    }
    else
    {
        system("cls");
        cout << "Enter a correct decision!";
        cin.get();
        cin.ignore();
        system("cls");
        goblinbattle();
    }
}

void ratbattle()
{
    cout << "Player VS Rat\n\nWhat will you do?\n1: Attack           " << strength << " Dammage\n2: Magic            " << magicdmg << " Dammage\n";
    cin >> choice;
    if ( choice == 1 )
    {
        enhealth = enhealth - strength;
        system("cls");
        cout << "You Attack the rat!\n\nThe rat now has: " << enhealth << "/10 life left.\n";
        cin.get();
        cin.ignore();
        system("cls");
        if ( enhealth <= 0 )
        {
            cout << "WELL DONE! You Killed the Rat!";
            cin.get();
            system("cls");
            battlechoice();
        }
        else
        {
            health = health - enattack;
            cout << "The Rat attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                ratbattle();
            }
        }
    }
    if ( choice == 2 )
    {
        if (magiclvl >= 2) {
        magiclvl = magiclvl - 2;
        enhealth = enhealth - magicdmg;
        system("cls");
        cout << "You Use Magic on the rat! You now have " << magiclvl << "/4 magic left.\n\nThe rat now has: " << enhealth << "/10 life left.\n";
        cin.get();
        cin.ignore();
        system("cls");
        if ( enhealth <= 0 )
        {
            cout << "WELL DONE! You Killed the Rat!";
            cin.get();
            system("cls");
            battlechoice();
        }
        else
        {
            health = health - enattack;
            cout << "The Rat attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                ratbattle();
            }
        }
        }
        else
        {
            system("cls");
            cout << "You Don't Have Enough Magic for That!\n";
            cin.get();
            cin.ignore();
            system("cls");
            ratbattle();
        }
    }
    else if ( choice == 3 )
    {
        system("cls");
        cout << "Your Potions:\n1: Health Potion         " << healthpotion << " left\n2: Magic Potion          " << magicpotion << " left\n3: Cancel\n";
        cin >> choice;
        if ( choice == 1 )
        {
            if ( healthpotion > 0 )
            {
            system("cls");
            healthpotion = healthpotion - 1;
            health = healthmax;
            cout << "You are fully healed!\n";
            cin.get();
            cin.ignore();
            system("cls");
            health = health - enattack;
            cout << "The rat attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                ratbattle();
            }
            }
            else
            {
                system("cls");
                cout << "You don't have any of this Potion!\n";
                cin.get();
                cin.ignore();
                ratbattle();
            }
        }
        else if ( choice == 2 )
        {
            if ( magicpotion > 0 )
            {
            system("cls");
            magiclvl = magicmax;
            cout << "Your magic is fully restored!\n";
            cin.get();
            cin.ignore();
            system("cls");
            health = health - enattack;
            cout << "The rat attacks you! You take: " << enattack << " Dammage. Leaving you with: " << health << "/15 life!";
            cin.get();
            system("cls");
            if (health <= 0)
            {
                cout << "Aww! You Died... Better Luck Next Time!";
                cin.get();
                system("cls");
                battlechoice();
            }
            else
            {
                ratbattle();
            }
        }
        else
            {
                system("cls");
                cout << "You don't have any of this Potion!\n";
                cin.get();
                cin.ignore();
                ratbattle();
            }
        }
        else if ( choice == 3 )
        {
            system("cls");
            ratbattle();
        }
    }
    else
    {
        system("cls");
        cout << "Enter a correct decision!";
        cin.get();
        cin.ignore();
        system("cls");
        ratbattle();
    }
}

void battlechoice()
{
    cout << "Welcome to the Battledome!";
    cin.get();
    cout << "Select your opponent:\n";
    cout << "1: Rat\n";
    cout << "2: Goblin\n";
    cout << "3: Heal\n";
    cout << "4: Close Programme\n";
    cin >> choice;
    if ( choice == 1 )
    {
        system("cls");
        enhealth = 10;
        enattack = 2;
        ratbattle();
    }
    else if ( choice == 2 )
    {
        system("cls");
        enhealth = 20;
        enattack = 4;
        goblinbattle();
    }
    else if( choice == 3 )
    {
        system("cls");
        health = healthmax;
        magiclvl = magicmax;
        cout << "You are Fully Healed!\n";
        cin.get();
        cin.ignore();
        system("cls");
        battlechoice();
    }
    else if ( choice == 4 )
    {
        system("cls");
        cout << "Good Bye!";
        cin.get();
        cin.ignore();
        int main();
    }
    else
    {
        system("cls");
        cout << "Enter a correct decision!";
        cin.get();
        cin.ignore();
        system("cls");
        battlechoice();
    }
}

int main()
{
    cout << "Welcome to my battle test.";
    cin.get();

    system("cls");
    battlechoice();
}

Recommended Answers

All 2 Replies

#include <cstdlib> // I'm needed for rand() and srand()
#include <ctime>   // I'm needed for time()
#include <iostream>

int main()
{
    // Put me at the start of the program
    srand((unsigned)time(0));
    
    const int N = 100;
    
    // Watch as random numbers between 0 and N are generated
    for (int i = 0; i < 10; i++)
        std::cout << rand() % N << '\n';
}

Look up srand() and rand() functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.