在二维数组中移动角色 我正在介绍C++类的介绍,我对这个项目很感兴趣。p>

在二维数组中移动角色 我正在介绍C++类的介绍,我对这个项目很感兴趣。p>,c++,C++,我需要让我的角色“H”在数组中自由移动。我已经编写了大量代码,但当我编译并运行它时,我的英雄没有选择移动。我不知道在main中调用函数时出现了什么问题。任何帮助都将不胜感激。我需要维持他在阵法中的新位置,以便他能找到随机放置在阵法中的恶棍。我可以稍后再处理randint部分,但我很难简单地让“H”移动。 以下是我到目前为止的情况: 多谢各位 #include <iostream> using namespace std; void printBoard(char board[][8

我需要让我的角色“H”在数组中自由移动。我已经编写了大量代码,但当我编译并运行它时,我的英雄没有选择移动。我不知道在main中调用函数时出现了什么问题。任何帮助都将不胜感激。我需要维持他在阵法中的新位置,以便他能找到随机放置在阵法中的恶棍。我可以稍后再处理randint部分,但我很难简单地让“H”移动。
以下是我到目前为止的情况:
多谢各位

#include <iostream>
using namespace std;

void printBoard(char board[][8])
{
    for (int x = 0; x < 8; x++)
    {
        for (int y = 0; y < 8; y++)
        {
            cout << board[x][y];
        }
        cout << endl;
    }
}

void move(char board[][8], char umove)
{
    cout << "Please enter which direction you would like to move." << endl;
    cin >> umove;

    if (umove == 'x')
    {
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 8; y++)
            {
                board[x][y] = x - 1;
            }
        }
    }
    else if (umove == 'd')
    {
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 8; y++)
            {
                board[x][y] = y + 1;
            }
        }
    }
    else if (umove == 'a')
    {
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 8; y++)
            {
                board[x][y] = y - 1;
            }
        }
    }

    else if (umove == 'w')
    {
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 8; y++)
            {
                board[x][y] = x + 1;
            }
        }
    }

}

char userinput()
{
    char usermove;
    cout << "Please enter the direction you want to go." << endl;

    cin >> usermove;

    return usermove;
}

int main()
{
    char board[8][8];
    int x;
    int y;
    while (true)
    {
        for (x = 0; x < 8; x++)
        {
            for (y = 0; y < 8; y++)
            {
                board[x][y] = 'e';
            }
        }

        board[0][0] = 'H';

        printBoard(board);
        void move();

        return 0;
    }
}
#包括
使用名称空间std;
无效打印板(字符板[][8])
{
对于(int x=0;x<8;x++)
{
对于(int y=0;y<8;y++)
{
您可以调用
void move()
,这是一种方法声明,您必须使用
move(…)
来代替调用方法。
返回0
导致应用程序完成,但在这种情况下不正确。您使用无限循环,并且必须使用一个条件来完成游戏。
根据你的描述,我建议:

void printBoard(char board[][8]) {
    // same as before
}

bool move(char board[][8], int &Hx, int &Hy) {
    char umove;
    cout << "Please enter which direction you would like to move." << endl;
    cin >> umove;
    if (umove == 'f') // f mean finish it
        return false;

    board[Hx][Hy] = 'e';
    if (umove == 'a') // a mean left
        Hy = Hy == 0 ? 7 : Hy - 1;
    else if (umove == 'd') // d mean right
        Hy = Hy == 7 ? 0 : Hy + 1;
    else if (umove == 'w') // w mean up
        Hx = Hx == 0 ? 7 : Hx - 1;
    else if (umove == 's')  // s mean down
        Hx = Hx == 7 ? 0 : Hx + 1;
    board[Hx][Hy] = 'H';
    return true;
}

int main() {
    char board[8][8];
    int Hx = 0, Hy = 0;
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            board[x][y] = 'e';
        }
    }
    board[Hx][Hy] = 'H';
    bool res = true;
    while (res) {
        printBoard(board);
        res = move(board, Hx, Hy);
    }
    cout << "Game finished!";
    return 0;
}
void打印板(字符板[][8]){
//和以前一样
}
布尔移动(字符板[][8],内部和Hx,内部和Hy){
Charumove;
不能移动;
如果(umove=='f')//f表示完成它
返回false;
板[Hx][Hy]=“e”;
if(umove=='a')//a左均值
Hy=Hy==0?7:Hy-1;
否则如果(umove=='d')//d表示正确
Hy=Hy==7?0:Hy+1;
else如果(umove=='w')//w表示向上
Hx=Hx==0?7:Hx-1;
如果(umove=='s')//s表示向下
Hx=Hx==7?0:Hx+1;
板[Hx][Hy]='H';
返回true;
}
int main(){
炭板[8][8];
int Hx=0,Hy=0;
对于(int x=0;x<8;x++){
对于(int y=0;y<8;y++){
董事会[x][y]=“e”;
}
}
板[Hx][Hy]='H';
bool res=真;
while(res){
印刷板;
res=移动(板、Hx、Hy);
}

无法取消移动();`是代码中的一个方法声明,它不起任何作用。
返回0
完成程序,而
while
循环只执行一次。您有一块
字符板,但您在方法
移动中为其分配了一个整数。我不明白这一点。是否可以编辑和添加更多细节?您想做什么?当我运行程序,当我按w、a、x或d的相应方向时,我想让H在棋盘上移动。是的,这很有帮助。我试图通过使用另一个空函数让H在棋盘上移动,但使用布尔函数似乎是最好的方法。欢迎你。如果你能接受或投票让其他人知道这是真的。