Text-based Role-Playing Game with Inventory, Quests, and Experience System

Simple C++ text-based RPG featuring inventory, quests, and experience system for leveling up. Ideal starting point for expanding RPG projects.

Brief Introduction

Text-based Role-Playing Game with Inventory, Quests, and Experience System (cpp)
DESCRIPTION:
A simple text-based role-playing game in C++ implementing an inventory, quests, and experience system. The player can collect items, complete quests, and earn experience to level up.

ADDITIONAL NOTES:
This example is intended as a starting point for more complex text-based RPGs. It is easily extendable with new features such as combat, maps, or dialogues.

EXPLANATIONS:
- The Player class contains methods to add items to the inventory, gain experience, and level up automatically.
- The Mission class manages the quest state (not started, in progress, completed) and assigns rewards upon completion.
- The main() function handles the game loop, showing a text menu and allowing the player to interact with the system.
- The experience system is simple: every 100 experience points the player levels up.
- The inventory is represented as a vector of strings, with the ability to add and display items.

Example Code: cpp

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

// Class representing the player with inventory, experience, and level
class Player {
private:
    std::vector<std::string> inventario; // List of owned items
    int esperienza;
    int livello;

public:
    Player() : esperienza(0), livello(1) {}

    // Adds an item to the inventory
    void aggiungiOggetto(const std::string& oggetto) {
        inventario.push_back(oggetto);
        std::cout << "You obtained: " << oggetto << "\n";
    }

    // Displays the inventory
    void mostraInventario() const {
        std::cout << "\n--- Inventory ---\n";
        if (inventario.empty()) {
            std::cout << "Inventory is empty.\n";
        } else {
            for (size_t i = 0; i < inventario.size(); ++i) {
                std::cout << i + 1 << ". " << inventario[i] << "\n";
            }
        }
        std::cout << "-----------------\n";
    }

    // Adds experience and manages level
    void aggiungiEsperienza(int punti) {
        esperienza += punti;
        std::cout << "You gained " << punti << " experience points!\n";
        // Check for level up every 100 points
        while (esperienza >= 100) {
            esperienza -= 100;
            livello++;
            std::cout << "Congratulations! You reached level " << livello << "!\n";
        }
    }

    int getLivello() const {
        return livello;
    }

    int getEsperienza() const {
        return esperienza;
    }
};

// Enum for quest state
enum class StatoMissione {
    NON_INIZIATA,
    IN_CORSO,
    COMPLETATA
};

// Class representing a quest
class Mission {
private:
    std::string nome;
    std::string descrizione;
    StatoMissione stato;
    std::string ricompensaOggetto;
    int ricompensaEsperienza;

public:
    Mission(const std::string& n, const std::string& d, const std::string& ricompOggetto, int ricompExp)
        : nome(n), descrizione(d), stato(StatoMissione::NON_INIZIATA),
          ricompensaOggetto(ricompOggetto), ricompensaEsperienza(ricompExp) {}

    // Starts the quest
    void inizia() {
        if (stato == StatoMissione::NON_INIZIATA) {
            stato = StatoMissione::IN_CORSO;
            std::cout << "Quest started: " << nome << "\n";
            std::cout << descrizione << "\n";
        } else {
            std::cout << "The quest has already started or completed.\n";
        }
    }

    // Completes the quest and assigns rewards
    void completa(Player& giocatore) {
        if (stato == StatoMissione::IN_CORSO) {
            stato = StatoMissione::COMPLETATA;
            std::cout << "Quest completed: " << nome << "!\n";
            giocatore.aggiungiOggetto(ricompensaOggetto);
            giocatore.aggiungiEsperienza(ricompensaEsperienza);
        } else if (stato == StatoMissione::NON_INIZIATA) {
            std::cout << "You must start the quest first.\n";
        } else {
            std::cout << "Quest already completed.\n";
        }
    }

    StatoMissione getStato() const {
        return stato;
    }

    std::string getNome() const {
        return nome;
    }
};

// Function to show the main menu
void mostraMenu() {
    std::cout << "\n=== Role-Playing Game Menu ===\n";
    std::cout << "1. Show inventory\n";
    std::cout << "2. Start quest\n";
    std::cout << "3. Complete quest\n";
    std::cout << "4. Show player status\n";
    std::cout << "5. Exit\n";
    std::cout << "Choose an option: ";
}

int main() {
    Player giocatore;
    Mission missione1("Retrieve the Lost Sword", "Find the legendary sword hidden in the forest.", "Legendary Sword", 120);

    bool giocoAttivo = true;
    while (giocoAttivo) {
        mostraMenu();
        int scelta;
        std::cin >> scelta;

        switch (scelta) {
            case 1:
                giocatore.mostraInventario();
                break;
            case 2:
                missione1.inizia();
                break;
            case 3:
                missione1.completa(giocatore);
                break;
            case 4:
                std::cout << "\nLevel: " << giocatore.getLivello() << "\n";
                std::cout << "Experience: " << giocatore.getEsperienza() << "/100\n";
                break;
            case 5:
                std::cout << "Exiting game. Goodbye!\n";
                giocoAttivo = false;
                break;
            default:
                std::cout << "Invalid option. Please try again.\n";
                break;
        }
    }

    return 0;
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Software:
  • Standard C++ language (C++11 or higher)
  • No external libraries required
  • Compatible compiler (e.g., g++, clang++)
Configuration:
  • Standard console for input/output
Arduino:
  • Not applicable (standalone PC software)
Code structure:
  • Player class: manages inventory, experience, level
  • Mission class: defines quests with state and rewards
  • Textual interaction functions with the player
Links/Configurations:
  • No external dependencies
  • All code contained in a single source file

Similar Topics

Text menu to choose simple arithmetic operations
C++ program with a text menu for simple arithmetic operations: addition, subtraction, multiplication, and division with zero division handling.
Guess the Number Game (1-1)
Simple C++ Guess the Number game from 1 to 1 with hints. Uses srand(time(NULL)) for randomness and loops until correct guess.
Conversion from Euro to Other Currencies with Fixed Rates
Simple C++ program converting euros to USD, GBP, and JPY using fixed rates. User inputs amount; output shows converted value with two decimals.
Text-based Contact Book with Name Search
C++ text-based contact book program with insertion, display, and case-insensitive name search using vectors and modular functions for easy management.
Managing a Book List with Saving to a Text File
C++ program for managing a book list with add, display, and save features using file I/O and simple data structures for persistence.
Supermarket Queue Simulator with std::queue
Supermarket Queue Simulator in C++ using std::queue to model FIFO customer service, demonstrating queue operations and status updates in a console program.
Warehouse Inventory Management with Structures and Vectors
Complete C++ warehouse inventory management example using structures and vectors for adding, removing, searching, and displaying products efficiently.
Mini daily agenda with appointments sorted by time
Simple C++ program for managing daily appointments with automatic time sorting and console input/output. Ideal for organized daily scheduling.