C++基于文本的游戏

问题描述:

我目前正在使用Visual Studio 2010在C++中制作一个基于小型控制台的文本游戏。 我已经列出了问题;当我拿到我的名字输入和选择困难我去作介绍性文字,我输入:C++基于文本的游戏

cout <<"Welcome "<<userName<<"... You are a lone: "<<pickRace<<" Your journey will be a "<<difficulty<<" one."; 

而且我希望它的出现:欢迎布雷克......你是一个孤独的人/兽人您的旅程将是一个简单/中/硬的一个。

但是我以欢迎布莱克的身份出现......你是一个孤独的人,你的乔尼将是一个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"; 
} 
+7

代码。我们需要看你的代码。 – jrok

+0

当我们还没有看到原创文字时,无法告诉您如何重写某些内容。向我们展示您声明并将值设置为“pickRace”和“难度”的代码。 – nhgrif

+0

猜你想让我们第二次猜测作业问题? –

pickRace和难度都是整数。你打印的是整数而不是实际的难度。您需要以某种方式逻辑地表示难度(和选择范围)

您正在存储pickRacedifficultyintegers。尝试做类似:

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"; 
} 

注意raceText字符串变量。

重复这个难度。

然后使用raceText和difficultyText打印您的留言:

out <<"Welcome "<<userName<<"... You are a lone: "<<raceText<<" Your journey will be a "<<difficultyText<<" one."; 
+0

要么使用的std :: string或使用字符数组。 – shubendrak

+0

一切似乎都只是在最后一行的cout user2864157

+0

你可能必须在Shubendra提到的(std :: string raceText;)中使用'std ::'前缀字符串。或声明的字符串,你声明的一样'userName' – Paddyd

考虑使用enum S和超载operator<<operator>>他们:

#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; 
} 

为什么你指望它来打印string你的时候正在存储的选项为int s ...

您可以使用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]; 
+0

一个问题你的建议是,'的std :: map'具有程序的初始化过程中加载。查找表可能是一个更好的解决方案,因为它是不变的。 –

您可能需要使用查找表来enum S或数字标识符(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; 
} 

一个很好的优点到恒定查找表作为数组是,它可以被存储在程序的只读数据部分和装载有恒定的数据。与在初始化期间创建变量相比,初始化时间可以忽略不计。