1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <map>
namespace game // http://en.cppreference.com/w/cpp/language/namespace
{
// http://www.stroustrup.com/C++11FAQ.html#enum
enum class race_type : unsigned int { human, orc, dwarf, elf, undead, invalid };
struct race_info
{
std::string name ;
std::string greeting ;
int initial_strength ;
int initial_health = 100 ; // http://www.stroustrup.com/C++11FAQ.html#member-init
// ...
};
const std::map< race_type, race_info > race_map = // http://www.stroustrup.com/C++11FAQ.html#uniform-init
{
{ race_type::human, { "human", "put some introdctory information about humans here", 50 } },
{ race_type::orc, { "orc", "intro stuff for orc", 70 } },
{ race_type::dwarf, { "dwarf", "When the third age of Valmor ... magic of any kind.", 30 } },
{ race_type::elf, { "elf", "something about elves", 15 } },
{ race_type::undead, { "undead", "info about undead", 60 } },
{ race_type::invalid, { "???", "???", -1 } }
} ;
std::string to_string( race_type r )
{
const auto iter = race_map.find(r) ; // unqualified looks up at namespace scope, finds game::race_map
return iter != race_map.end() ? iter->second.name : "unknown race" ;
}
race_type to_race_type( std::string str )
{
for( char& c : str ) c = std::tolower(c) ;
for( const auto& pair : race_map ) if( pair.second.name == str ) return pair.first ;
return race_type::invalid ;
}
std::ostream& operator<< ( std::ostream& stm, race_type r ) { return stm << to_string(r) ; }
std::istream& operator>> ( std::istream& stm, race_type& r )
{
std::string name ;
if( stm >> name )
{
// http://en.cppreference.com/w/cpp/language/unqualified_lookup
r = to_race_type(name) ; // unqualified looks up at namespace scope, finds game::to_race_type
// http://en.cppreference.com/w/cpp/io/basic_ios/clear
if( r == race_type::invalid ) stm.clear( std::ios::failbit ) ;
}
else r = race_type::invalid ;
return stm ;
}
race_type get_race_type()
{
race_type r ;
std::cout << "race ( " ;
for( const auto& p : race_map ) if( p.first != race_type::invalid ) std::cout << p.second.name << ' ' ;
std::cout << ")? " ;
if( std::cin >> r && r != race_type::invalid ) return r ;
std::cin.clear() ; // http://en.cppreference.com/w/cpp/io/basic_ios/clear
std::cin.ignore( 1000, '\n' ) ; // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
std::cout << "invalid race. try again\n" ;
return get_race_type() ;
}
void race_fun( race_type r ) ;
}
int main()
{
std::cout << "Welcome to the game...\n"
"This game takes place in the forth age of on a small planet called valmor.\n"
"One hero must fight their way to the end and bring peace back to the forth age.\n"
"Press enter to continue\n\n" ;
std::cin.get();
//Prompt the user to enter their race
std::cout << "Choose a race. Each race as its own racial advantages and disadvantages.\n" ;
const game::race_type race = game::get_race_type() ;
// http://en.cppreference.com/w/cpp/language/adl
race_fun(race) ; // Koenig lookup, finds game::race_fun
std::cout << "\nEnter the first name of your character: " ;
std::string name;
std::cin >> name ; // Koenig lookup, finds std::operator>> ( std::istream&, std::string& )
std::cout << "\n\nWelcome " << name << ", the " << to_string(race) << ".\n" ;
}
void game::race_fun( race_type r )
{
if( r != race_type::invalid )
{
const race_info& info = race_map.find(r)->second ;
std::cout << "\n\n" << info.greeting << '\n'
<< "your current strength is: " << info.initial_strength << '\n'
<< "and your current health is: " << info.initial_health << '\n' ;
}
}
|