我正在尝试写一个简单的C++程序,将帮助导航用户通过迷宫[已关闭]

oxf4rvwz  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(135)

昨天关门了。
Improve this question
在我的程序中,我使用了一个迷宫的图片作为参考,并从中创建了方向。我的第一次输入似乎很顺利,但从那里开始就变得忙碌起来,因为它输出了我在迷宫中特定位置的每一个cout。我不确定我做错了什么。我是c++的新手,正在遵循在线教程。所以请你友善一点,因为我只是想学一门新的技能。2下面是我正在使用的迷宫的图片,以供参考。

我一直在使用if语句来尝试和导航用户。它是半工作的,但我似乎在某个地方犯了错。
下面是我的代码:

#include <iostream>

using namespace std; 

int main()

{

int choice; 

cout << endl; 
cout << "Welcome to the Maze Challenge! Your goal is to navigate your way through the maze by moving left or right to victory!"; 
cout << endl << endl; 
cout << "To move right please use 1, and to move left please use 0"; 
cout << endl << endl; 

cout << "Your are starting at Position 1. Would you like to turn right or left?: "; 
cin >> choice; 

//Position 1
if (choice == 1){
    cout << "You are now at Position 3. Would you like to turn right or left?: ";    
    cin >> choice; 
}

//Position 2
if  (choice == 0){ 
     cout << "You are now at Position 2. Would you like to turn right or left?: ";  
     cin >> choice; 
}
    
if (choice == 1) { 
    cout << "Sorry! You have landed in dead end X1. Good luck next time!" << endl; 
    
} 

if (choice == 0) { 
    cout << "Sorry! You have landed in dead end X2. Good luck next time!" << endl; 
} 

    
//Position 3 
if (choice == 1) { 
    cout << "You are now at Position 4. Would you like to turn right or left?: "; 
    cin >> choice; 
} 

if (choice == 0) { 
    cout << "Sorry! you have landed in dead end X3. Good luck next time!" << endl; 
} 

//Position 4
if (choice == 1) { 
    cout << "You are now at Position 5. Would you like to turn right or left?: "; 
    cin >> choice; 
} 

if (choice == 0) { 
    cout << "You are now at Position 7. Would you like to turn right or left?: "; 
}

下面是输出:

vdzxcuhz

vdzxcuhz1#

定义迷宫数据,以便从当前位置确定2个目的地(左和右)。
例如非常简单......

//Here, the special value 0 is used to indicate goal.
const int Goal = 0;

//Here, negative values indicate dead end.
//e.g. -1 is "X1".
const int Data[][2] =
{
    0,0, // dummy
    2,3,    // Left and Right destinations  from  Position 1  are  Position 2 and 3.
    -2,-1,  // From Position 2, both destinations are dead ends ("X2" and "X1").
    -3,4,   // From Position 3, ...
    7,5,    // From Position 4, ...
    6,-4,   // From Position 5, ...
    -6,-5,  // From Position 6, ...
    8,-7,   // From Position 7, ...
    9,-8,   // From Position 8, ...
    -9,Goal // Right destination from Position 9 is the Goal.
};

使用此数据,代码可以编写为:

int main(int argc, char *argv[])
{
    //Here, the special value 0 is used to indicate goal.
    const int Goal = 0;

    //Here, negative values indicate dead end.
    //e.g. -1 is "X1".
    const int Data[][2] =
    {
        0,0, // dummy
        2,3,    // Left and Right destinations  from  Position 1  are  Position 2 and 3.
        -2,-1,  // From Position 2, both destinations are dead ends ("X2" and "X1").
        -3,4,   // From Position 3, ...
        7,5,    // From Position 4, ...
        6,-4,   // From Position 5, ...
        -6,-5,  // From Position 6, ...
        8,-7,   // From Position 7, ...
        9,-8,   // From Position 8, ...
        -9,Goal // Right destination from Position 9 is the Goal.
    };

    std::cout << "Welcome to the Maze Challenge!" << std::endl;
    int CurrPos = 1;  //Start is Position 1.
    while( true )
    {
        std::cout << std::endl;
        
        //Check if current position is goal or dead end...
        if( CurrPos == Goal )
        {
            std::cout << "** Goal! **" << std::endl;
            break;
        }
        if( CurrPos < 0 )
        {
            std::cout << "You have landed in dead end X" << -CurrPos << std::endl;
            break;
        }

        //Show current position
        std::cout << "You are now at position " << CurrPos << std::endl;

        //Input
        int choice = -1;
        while( choice!=0 && choice!=1 )
        {
            std::cout << "0=Left, 1=Right > ";
            std::cin >> choice;
        }
        //Move to choiced direction
        CurrPos = Data[CurrPos][choice];
    }
    std::cout << "(END)" << std::endl;
    return 0;
}

相关问题