Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于文本的游戏非分支if语句 我正在研究C++中的基于文本的游戏。我想知道,如果不嵌套这么多if语句,您如何处理不同的事件。我所说的一个例子是 if ( userChoice == 1 ) { //do something cout << "Pick 1 , 2, or 3" << endl; if ( userChoice == 1 ) { cout << "pick 1, 2, 3/ some storry line" << endl; if ( userChoice == 3 ) { cout << " storyline...123...." << endl; if ( userChoice == 2 ) { //etc } } } } if(userChoice==1) { //做点什么 cout_C++_If Statement - Fatal编程技术网

基于文本的游戏非分支if语句 我正在研究C++中的基于文本的游戏。我想知道,如果不嵌套这么多if语句,您如何处理不同的事件。我所说的一个例子是 if ( userChoice == 1 ) { //do something cout << "Pick 1 , 2, or 3" << endl; if ( userChoice == 1 ) { cout << "pick 1, 2, 3/ some storry line" << endl; if ( userChoice == 3 ) { cout << " storyline...123...." << endl; if ( userChoice == 2 ) { //etc } } } } if(userChoice==1) { //做点什么 cout

基于文本的游戏非分支if语句 我正在研究C++中的基于文本的游戏。我想知道,如果不嵌套这么多if语句,您如何处理不同的事件。我所说的一个例子是 if ( userChoice == 1 ) { //do something cout << "Pick 1 , 2, or 3" << endl; if ( userChoice == 1 ) { cout << "pick 1, 2, 3/ some storry line" << endl; if ( userChoice == 3 ) { cout << " storyline...123...." << endl; if ( userChoice == 2 ) { //etc } } } } if(userChoice==1) { //做点什么 cout,c++,if-statement,C++,If Statement,我会为每个选项创建一个单独的函数: void doStoryLine1() { cout << "Options: ..." << endl; if(userChoice == 1) fightDragon(); else if(userChoice == 2) fightOrcs(); else if(userChoice == 3) marryPrincess(); else

我会为每个选项创建一个单独的函数:

void doStoryLine1() {
    cout << "Options: ..." << endl;
    if(userChoice == 1)
        fightDragon();
    else if(userChoice == 2)
        fightOrcs();
    else if(userChoice == 3)
        marryPrincess();
    else
        /* invalid option? */
}

void doStoryLine2() {
    ...
}

void selectStoryLine() {
    cout << "Options: ..." << endl;
    if(userChoice == 1)
        doStoryLine1();
    else if(userChoice == 2)
        doStoryLine2();
    else if(userChoice == 3)
        doStoryLine3();
    else
        /* invalid option? */
}
void doStoryLine1(){

cout我会为每个选项创建一个单独的函数:

void doStoryLine1() {
    cout << "Options: ..." << endl;
    if(userChoice == 1)
        fightDragon();
    else if(userChoice == 2)
        fightOrcs();
    else if(userChoice == 3)
        marryPrincess();
    else
        /* invalid option? */
}

void doStoryLine2() {
    ...
}

void selectStoryLine() {
    cout << "Options: ..." << endl;
    if(userChoice == 1)
        doStoryLine1();
    else if(userChoice == 2)
        doStoryLine2();
    else if(userChoice == 3)
        doStoryLine3();
    else
        /* invalid option? */
}
void doStoryLine1(){

cout您描述的问题似乎是异步编程(例如Node JS)中的常见问题

您可以通过使用控制流编程(如序列/瀑布和承诺)打破这些循环

注意:我以前没有使用过这个标题,但它的工作原理应该与JS中的futures类似:

用JS编写代码(你可以从C++中获得灵感)


您描述的问题似乎是异步编程(例如Node JS)中的常见问题

您可以通过使用控制流编程(如序列/瀑布和承诺)打破这些循环

注意:我以前没有使用过这个标题,但它的工作原理应该与JS中的futures类似:

用JS编写代码(你可以从C++中获得灵感)


通过使用数据驱动的设计,可以消除if语句的分支

在底部有一个循环,用于检查输入,并使用当前上下文对输入求值,然后更改上下文

每个if语句都成为“场景”(或房间或状态,选择一个对您正在编写的程序有意义的名称)。每个场景都有一个id、一个描述,以及一组有效的输入和结果显示的场景编号

您有一个表示结束的场景

您有一个循环:

loop until current scene is the end
 Print current scene description
 Ask for input
 Evaluate input
“求值输入”根据当前场景的有效输入检查输入,如果有效,则将当前场景设置为指定场景

程序将当前场景初始化为第一个场景,然后开始循环

因此,对于您的示例(显然不完整,但应能让您了解所需的数据),您可以看到以下场景:

id: 1
description: "Pick 1 , 2, or 3"
inputs: "1" => 2, "2" =>, "3" =>

id: 2
description: "pick 1, 2, 3/ some storry line"
inputs: "1" =>, "2" =>, "3" => 3

id: 3
description: " storyline...123...."
inputs: "1" =>, "2" =>, "3" =>
通常数据来自文件

下面是一个示例(尚未编译或调试):

struct场景
{
场景(int-id\uu,int-description\uu)
:id(id_)
,说明(说明)
{
}
int-id;
std::字符串描述;
std::map InputOneXTScene;
};
void main(int,char**)
{
地图场景;
int-id=[1,2,3];
字符串描述=[“第一”、“第二”、“第三”];
int nextScenes[3][3]=[1,2,3]、[1,3,2]、[1,2,0];
std::字符串输入[3]=[“1”、“2”、“3”];
对于(int i=0;i!=3;++i)
{
场景[ID[i]]=场景(ID[i],描述);
Scene&Scene=scenes.at(id[i]);
对于(int j=0;j!=3;++j)
{
scene.inputToNextScene[input[j]=nextScenes[i][j];
}
}
int currentsecene=1;
std::字符串输入;
while(当前场景!=0)
{
Scene&Scene=scenes.at(当前场景);
std::cout>输入;
if(scene.inputToNextScene.find(input)!=scene.inputToNextScene.end())
{
currentScene=scene.inputToNextScene.at(输入);
}
}

cout通过使用数据驱动的设计,您可以摆脱if语句的分支

在底部有一个循环,用于检查输入,并使用当前上下文对输入求值,然后更改上下文

每个if语句都成为“场景”(或房间或状态,选择一个对您正在编写的程序有意义的名称)。每个场景都有一个id、一个描述,以及一组有效的输入和结果显示的场景编号

您有一个表示结束的场景

您有一个循环:

loop until current scene is the end
 Print current scene description
 Ask for input
 Evaluate input
“求值输入”根据当前场景的有效输入检查输入,如果有效,则将当前场景设置为指定场景

程序将当前场景初始化为第一个场景,然后开始循环

因此,对于您的示例(显然不完整,但应能让您了解所需的数据),您可以看到以下场景:

id: 1
description: "Pick 1 , 2, or 3"
inputs: "1" => 2, "2" =>, "3" =>

id: 2
description: "pick 1, 2, 3/ some storry line"
inputs: "1" =>, "2" =>, "3" => 3

id: 3
description: " storyline...123...."
inputs: "1" =>, "2" =>, "3" =>
通常数据来自文件

下面是一个示例(尚未编译或调试):

struct场景
{
场景(int-id\uu,int-description\uu)
:id(id_)
,说明(说明)
{
}
int-id;
std::字符串描述;
std::map InputOneXTScene;
};
void main(int,char**)
{
地图场景;
int-id=[1,2,3];
字符串描述=[“第一”、“第二”、“第三”];
int nextScenes[3][3]=[1,2,3]、[1,3,2]、[1,2,0];
std::字符串输入[3]=[“1”、“2”、“3”];
对于(int i=0;i!=3;++i)
{
场景[ID[i]]=场景(ID[i],描述);
Scene&Scene=scenes.at(id[i]);
对于(int j=0;j!=3;++j)
{
scene.inputToNextScene[input[j]=nextScenes[i][j];
}
}
int currentsecene=1;
std::字符串输入;
while(当前场景!=0)
{
Scene&Scene=scenes.at(当前场景);
std::cout>输入;
if(scene.inputToNextScene.find(input)!=scene.inputToNextScene.end())
{
currentScene=scene.inputToNextScene.at(输入);
}
}
看和看

简短版本:

对每个“分支”使用单独的函数或对象。 不要使用“if/else”,而是使用
std::map
。将用户输入ID映射到处理它们的函数。

请参阅和

简短版本:

对每个“分支”使用单独的函数或对象。
不要使用“if/else”,而是使用
std::map
。将用户输入ID映射到处理它们的函数。

好的,首先,您为什么可能需要
(userChoice==1)
两次,然后查看开关状态