基于C++的文本游戏 我现在在Visual Studio 2010中用C++制作了一个小型的基于控制台的文本游戏。 我曾经遇到过这样的问题;当我输入我的姓名并选择难度后,我会制作介绍性文本,然后输入: cout <<"Welcome "<<userName<<"... You are a lone: "<<pickRace<<" Your journey will be a "<<difficulty<<" one.";

基于C++的文本游戏 我现在在Visual Studio 2010中用C++制作了一个小型的基于控制台的文本游戏。 我曾经遇到过这样的问题;当我输入我的姓名并选择难度后,我会制作介绍性文本,然后输入: cout <<"Welcome "<<userName<<"... You are a lone: "<<pickRace<<" Your journey will be a "<<difficulty<<" one.";,c++,C++,我希望它显示为:欢迎布莱克。。。你是一个孤独的人类/兽人,你的旅程将是轻松/中等/艰难的 但我来欢迎布莱克。。。你是一个孤独的1/2,你的焦距将是1/2/3 这是一个问题,我认为由于我的开关的谁能告诉我,我需要如何重写他们,使其出现的名称,而不是数字 原始代码: cout <<"Please pick your race: \n"; cout <<"1 - Human\n"; cout <<"2 - Orc\n"; int pickRace; cout <

我希望它显示为:欢迎布莱克。。。你是一个孤独的人类/兽人,你的旅程将是轻松/中等/艰难的

但我来欢迎布莱克。。。你是一个孤独的1/2,你的焦距将是1/2/3

这是一个问题,我认为由于我的开关的谁能告诉我,我需要如何重写他们,使其出现的名称,而不是数字

原始代码:

cout <<"Please pick your race: \n";
cout <<"1 - Human\n";
cout <<"2 - Orc\n";
int pickRace;
cout <<"Pick your race: ";
cin >>pickRace;

switch (pickRace)
{
case 1:
    cout <<"You picked the Human race.\n";
    break;
case 2:
    cout <<"You Picked the Orc race\n";
    break;
default:
    cout <<"Error - Invalid imput; only 1 or 2 allowed.\n";
}


int difficulty;
cout <<"\nPick your level diffuculty: \n";
cout <<"1 - Easy\n";
cout <<"1 - Medium\n";
cout <<"3 - Hard\n";

cout <<"Pick your level difficulty: ";
cin >>difficulty;

switch (difficulty)
{
case 1:
    cout <<"You picked Easy.\n\n";
    break;
case 2:
    cout <<"You picked Medium.\n\n";
    break;
case 3:
    cout <<"You picked Hard.\n\n";
    break;
default:
    cout <<"Error - Invalid imut; only 1,2 or 3 allowed.\n";
}

pickRace和Demobility是整数。您正在打印整数,而不是实际的难度。您需要以某种方式逻辑地表示难度和pickRace

您正在将pickRace和难度存储为整数。试着做一些类似的事情:

int pickRace;
string raceText;    //we will store the race type using this
cout <<"Pick your race: ";
cin >>pickRace;

switch (pickRace)
{
    case 1:
        cout <<"You picked the Human race.\n";
        raceText = "Human";
        break;
    case 2:
        cout <<"You Picked the Orc race\n";
        raceText = "Orc";
        break;
    default:
        cout <<"Error - Invalid imput; only 1 or 2 allowed.\n";
}
考虑对它们使用枚举和重载运算符:

#include <iostream>
#include <cassert>

enum difficulty { EASY = 1, MEDIUM = 2, HARD = 3 };

std::istream& operator>>( std::istream& is, difficulty& d )
{
     int i;
     is >> i;
     assert( i > 0 && i < 4 ); // TODO: Use real error handling, throw an exception
     d = difficulty( i );
     return is;
}

std::ostream& operator<<( std::ostream& os, difficulty d )
{
    switch( d ) {
    case EASY: return os << "easy";
    case MEDIUM: return os << "medium";
    case HARD: return os << "hard";
    }
    return os << "unknown[" << (int)d << "]";
}

int main()
{
    difficulty d;
    std::cout << "Pick difficulty: 1-easy, 2-medium, 3-hard: ";
    std::cin >> d;
    std::cout << "You picked difficulty: " << d << std::endl;
}

当您将选项存储为整数时,为什么希望它打印字符串

你可以用


您可能希望使用查找表在枚举或数字标识符ID与其表示的文本之间进行转换

例如:

struct Race_Text_Entry
{
    const char *  text;
    unsigned int  id;
};

static const Race_Text_Entry race_name_table[] =
{
  {"Unknown", 0},
  {"Human",   ID_HUMAN_RACE},
  {"Orc",     ID_ORC_RACE},
  {"Elf",     ID_ELF_RACE},
};
static const unsigned int NUM_RACE_ENTRIES =
    sizeof(race_name_table) / sizeof(race_name_table[0]);

std::string Race_ID_To_Text(unsigned int id)
{
    unsigned int i = 0;
    std::string race_name = "Race unknown";
    for (i = 0; i < NUM_RACE_ENTRIES; ++i)
    {
       if (race_name_table[i].id == id)
       {
           race_name = race_name_table.text;
           break;
       }
    }
    return race_name;
}

int main(void)
{
    std::cout << "My race: " << Race_ID_To_Text(ID_RACE_HUMAN) << "\n";
    return 0;
}

常量查找表作为数组的一个很好的优点是,它可以存储在程序的只读数据部分,并加载常量数据。与初始化期间创建std::map变量相比,初始化时间可以忽略不计。

代码。我们需要查看您的代码。当我们没有看到原始代码时,无法告诉您如何重写某些内容。向我们展示你声明和设置pickRace和难度值的代码。猜猜你想让我们再猜猜作业问题吗?@jrok不,你不明白:这是游戏,它已经开始了。OP:我用一个朋友宣言击中了兽人!您是否将pickRace和Conference存储为整数?使用std::string或字符数组。除了最后一行之外,这里的一切似乎都很好。您可能必须使用Shubendra提到的std::string raceText;作为字符串前缀。或者以与声明username相同的方式声明字符串。您建议的一个问题是,必须在程序初始化期间加载std::map。查找表可能是更好的解决方案,因为它是常量。
#include <map>


std::map<int, std::string> difficulty;

difficulty[1] = "easy";
difficulty[2] = "medium";
difficulty[3] = "hard";

int choice_difficulty;
std::cin>>choice_difficulty;

/*Check if user entered correct number*/
std::map<int, std::string>::iterator it = difficulty.find(choice_difficulty);
if(it == difficulty.end())
    std::cout << "wrong choice";

cout <<"Welcome "<<userName<<" Your journey will be a "<<difficulty[choice_difficulty];
struct Race_Text_Entry
{
    const char *  text;
    unsigned int  id;
};

static const Race_Text_Entry race_name_table[] =
{
  {"Unknown", 0},
  {"Human",   ID_HUMAN_RACE},
  {"Orc",     ID_ORC_RACE},
  {"Elf",     ID_ELF_RACE},
};
static const unsigned int NUM_RACE_ENTRIES =
    sizeof(race_name_table) / sizeof(race_name_table[0]);

std::string Race_ID_To_Text(unsigned int id)
{
    unsigned int i = 0;
    std::string race_name = "Race unknown";
    for (i = 0; i < NUM_RACE_ENTRIES; ++i)
    {
       if (race_name_table[i].id == id)
       {
           race_name = race_name_table.text;
           break;
       }
    }
    return race_name;
}

int main(void)
{
    std::cout << "My race: " << Race_ID_To_Text(ID_RACE_HUMAN) << "\n";
    return 0;
}