HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Menu class/struct for RPG

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
structmenuforrpgclass

Problem

I plan to use this menu struct in an RPG played in the terminal. I want to make a RPG similar to D&D. I would like advice on my methods for the menu struct, especially the "hide" and "unhide" option methods.

main.cpp

#include 
#include 
#include 
#include 
#include 

const int invalid_index = -2;
const int default_index = -1;
const std::string defualt_title = "";
const std::initializer_list defualt_options = {""};

struct menu{
    menu(const std::string& title, const std::initializer_list& options)
        : title(title), options(options), current_index(default_index){;}; 
    menu(void)
        : title(defualt_title), options(defualt_options), current_index(default_index){;};

    std::vector hidden_options;
    std::vector options;
    std::string title;
    int current_index;

    void insert_option(int index, std::string option){
        if(index >= 0 && index = 0 && index = 0 && index = 0 && index = 0 && index < options.size()){       
                options.insert(options.begin() + index, hidden_options[index]);
            }else{;}
        }else{;}
    };

    friend std::ostream& operator<<(std::ostream& ostream, const menu& r){
        ostream << r.title << '\n';
        for(int i = 0; i < r.options.size(); i++){
            ostream << "[" << i << "] " << r.options[i] << '\n';
        }ostream << "Enter index: ";
        return ostream; 
    };  
};

int main(){
    menu title_screen("Anyone Know a good RPG?", {"Play","Help","Quit"});
    return 0;   
}

Solution

-
defualt_title and defualt_options are misspelled.

-
Those empty else{;} statements are unnecessary. If there is eventually a need for actual else statements, then they can be added later.

-
With those "stray" data members, this should really be a class so that they can be private. Data members should not be directly exposed to the interface. Any functions that shouldn't be called by the user should also be private, otherwise they should be public.

Context

StackExchange Code Review Q#97607, answer score: 5

Revisions (0)

No revisions yet.