C++ 检测多维数组中的墙C++;问题

C++ 检测多维数组中的墙C++;问题,c++,arrays,C++,Arrays,我需要一些人帮我弄清楚这件事。由于某种原因,当我向前移动时 我的数组比如说“e”代表东方,我已经做了一个探测墙 碰撞,使玩家无法继续前进。然而,当我试图 往西走“w”,往回走2,而不是1。有什么办法吗 解决这个问题 现在,使用e按钮向右移动,使用west按钮向左移动。我使用了一个多维数组和一个开关来检测您所在的房间,并提供了玩家坐标 #include <iostream> #include <string> #include <vector> #include

我需要一些人帮我弄清楚这件事。由于某种原因,当我向前移动时 我的数组比如说“e”代表东方,我已经做了一个探测墙 碰撞,使玩家无法继续前进。然而,当我试图 往西走“w”,往回走2,而不是1。有什么办法吗 解决这个问题

现在,使用e按钮向右移动,使用west按钮向左移动。我使用了一个多维数组和一个开关来检测您所在的房间,并提供了玩家坐标

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <windows.h>

using namespace std;

int playerX = 0; // x
int playerY = 0; // y

int nextX; // pY
int nextY; // pX

bool win = false;

string move;

char dungeon[11][11] = {

{ 's','c','c','c','r','w','c','c','c','r','r',},
{ 'w','w','w','c','w','r','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',},
{ 's','c','c','r','r','c','c','c','c','r','r',}

};

void movePlayer();
void location(int X, int Y);

int main()
{
    do {

        movePlayer();
        location(playerX, playerY);

    } while (win == false);

}

void movePlayer() {

    string move;
    cin >> move;

    // reference Grid-Cave-Mk1 Author: Heather Southall Date: 2017

    if (move == "n") {
        nextY = playerY - 1;
        if ((nextY >= 0) && (nextY < 11)) {
            playerY = nextY;
        }
        else {
            cout << "Can't move there" << endl;
        }
    }
    else if (move == "e") {
        nextX = playerX + 1;
        if ((nextX >= 0) && (nextX < 11)) {
            playerX = nextX;
        }
        else {
            cout << "Can't move there" << endl;
        }
    }
    else if (move == "w") {
        nextX = playerX - 1;
        if ((nextX >= 0) && (nextX < 11)) {
            playerX = nextX;
        }
        else {
            cout << "Can't move there" << endl;
        }
    }
    else if (move == "s") {
        nextY = playerY + 1;
        if ((nextY >= 0) && (nextY < 11)) {
            playerY = nextY;
        }
        else {
            cout << "Can't move there" << endl;
        }
    }
}

void location(int X, int Y) {

    cout << "X: " << X << endl;
    cout << "Y: " << Y << endl;

    char local = dungeon[Y][X];

    switch (local) {
    case 's':
        cout << "Starter Room" << endl;
        break;
    case 'c':
        cout << "Corridor" << endl;
        break;
    case 'r':
        cout << "Room" << endl;
        break;
    case 'w':
        nextX = playerX - 1;
        if ((nextX >= 0) && (nextX < 11)) {
            playerX = nextX; playerY = nextY;
            break;
        }
    }
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int playerX=0;//x
int playerY=0;//Y
int nextX;//派克
int nextY;//二甲苯
布尔温=假;
串动;
查尔地牢[11][11]={
{'s','c','c','c','r','w','c','c','c','r','r',},
{'w','w','w','c','w','r','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',},
{'s','c','c','r','r','c','c','c','c','c','r','r',}
};
void movePlayer();
空隙位置(整数X,整数Y);
int main()
{
做{
movePlayer();
位置(playerX、playerY);
}while(win==false);
}
void movePlayer(){
串动;
cin>>移动;
//参考网格-Cave-Mk1作者:Heather Southall日期:2017
如果(移动=“n”){
nextY=playerY-1;
如果((nextY>=0)和&(nextY<11)){
playerY=nextY;
}
否则{

cout这是一个误导性日志的问题-您不打印当前位置,而是正在检查“type”的位置。因此,您可以在以下情况下访问日志[5,0]和[3,0]:

  • 您从[4,0]房间向东移动。您检查[5,0]的位置(第一次打印)。您撞到了墙,因此当前位置移回[4,0]
  • 气馁,你向西移动。你检查[3,0]处的内容(第二次打印
您应该更改打印的消息,以便通知用户当前位置和测试位置



注:撞击墙壁应将您移动到上一个位置,而不是将位置平移[-1,0]。

这是一个误导性日志的问题-您不打印当前位置,而是正在检查“类型”的位置。因此,您可以在以下情况下访问日志[5,0]和[3,0]:

  • 您从[4,0]房间向东移动。您检查[5,0]的位置(第一次打印)。您撞到了墙,因此当前位置移回[4,0]
  • 气馁,你向西移动。你检查[3,0]处的内容(第二次打印
您应该更改打印的消息,以便通知用户当前位置和测试位置



注:撞击墙壁应将您移动到上一个位置,而不是将该位置平移[-1,0].

您确定每次输入“w”时都会发生这种情况吗?不仅仅是当您移动到“w”位置时会发生这种情况吗?使用
cout也不是一个坏主意,因为nextX和nextY没有初始化,而是在位置()的“w”情况下使用的。希望在这之前使用过。只有当它到达数组中的“w”时才使用。当我尝试从这里返回时,它返回两个空格而不是一个(例如5到3)。您应该打印出实际的房间(在两个函数之前),而不是由
位置(x,y)
测试的房间。您位于[4,0]。您应该朝墙移动(e) 。它打印[5,0],但它会将您移回[4,0]处的房间。如果您向西移动,您将位于[3,0]。因此您将获得日志[5,0]->[3,0]。无关:如果您将播放器的位置放置在
结构位置{int x,y;…}
,并添加成员函数,如
位置{position operator+=(const position t&)
您可以用比现在所需更少的精力来隔离和修复问题。这也有助于向其他人提出问题。您确定每次输入“w”时都会发生这种情况,而不仅仅是在移动到“w”位置时?使用
不是一个坏主意。nextX和nextY也没有初始化,但是在location()的“w”案例中使用了t。我们希望它们是在发生这种情况之前使用的。只有当它到达数组中的“w”时才使用。当我尝试从此返回时,它返回两个空格而不是一个(例如5到3)。您应该打印出实际的房间(在两个函数之前),而不是由
location(x,y)测试的房间
。你在[4,0]。你向墙(e)移动。它打印[5,0],但它会将你移回[4,0]的房间。如果你向西移动,你将在[3,0]。因此你会得到日志[5,0]>[3,0]。无关:如果你把玩家的位置放在
结构位置{int x,y;…}
并添加成员函数,如
position\u t&operator+=(const position\u t&)
您可以用比现在所需更少的精力来隔离和修复问题。这在向他人提出问题时也很有帮助。谢谢您的帮助。我认为通过执行nextX=playerX-1;,我是在向后移动一个空间。有没有更好的方法来做到这一点?它会将您向后移动,但前提是您来自西区。如果您要移动pl在
location()
中,使用
move()
仅用于设置
nextX
nextY
,然后在
location()
中既有当前位置也有新位置可用。这会将函数更改为
checkNext()
movePlayer()
。但是如果你不想重新组织atm,你可以在
move()
的开头存储一个
prevX
prevY
。谢谢你的帮助。我想通过执行nextX=playerX