数组打印出多个字符在基本的地下城爬行



我的名字是Eric,我在使用c++编写基本的"地下城爬行器"时遇到了一个问题。

问题是,当玩家向左或向右移动某些空间时,会生成另一个玩家角色,因此屏幕上有两个而不是一个。

下面是我的代码:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>

using namespace std;

const int columns = 7, rows = 10;
string gridArray[rows][columns];
bool xIsGenerated = false;
bool gISGenerated = false;
int playerX = 0, playerY = 0;

void displayGrid(int rows, int columns){
    for(int i = 0; i < columns; i++){
        for(int x = 0; x < rows; x++){
            cout << gridArray[i][x];
        }
        cout << endl;
    }
    cout << gISGenerated << endl;
    return;
}
void generatePieces(int rows, int columns){
    int tcount = 0;
    for(int i = 0; i < rows; i++){
        for(int x = 0; x < columns; x++){
            srand(time(NULL) + x + i);

            int r = rand() % 5;
            if(r == 1 && tcount < 4){
                gridArray[i][x] = "T ";
                tcount++;
            }else if(r == 2 && !xIsGenerated){
                gridArray[i][x] = "X ";
                xIsGenerated = true;
            }

        }
    }
    if(!xIsGenerated){
        srand(time(NULL)*3);
        int r = rand() % rows+1;
        int c = rand() % columns+1;
        gridArray[r][c] = "X ";
        xIsGenerated = true;
    }
    return;
}
void generatePlayer(int rows, int columns){
    if(!gISGenerated){
        srand(time(NULL)*3);
        int r = rand() % rows+1;
        int c = rand() % columns+1;
        gridArray[r][c] = "G ";
        playerX = r;
        playerY = c;
        gISGenerated = true;
    }
}
void initGrid(int rows, int columns){
    for(int i = 0; i < columns; i++){
        for(int x = 0; x < rows; x++){
            gridArray[i][x] = ". ";
        }
    }

    generatePieces(rows, columns);
    generatePlayer(rows, columns);
    return;
}
//i is the rows
//x is the columns

void movePlayer(){
    char input = 'x';
    cin >> input;

    if(input == 'w' && playerX != 0){
        gridArray[playerX][playerY] = ". ";
        gridArray[playerX-1][playerY] = "G ";
        playerX--;
    }
    if(input == 's' && playerX != 6){
        gridArray[playerX][playerY] = ". ";
        gridArray[playerX+1][playerY] = "G ";
        playerX++;
    }
    if(input == 'a' && playerY != 0){
        gridArray[playerX][playerY] = ". ";
        gridArray[playerX][playerY-1] = "G ";
        playerY--;
    }
    if(input == 'd' && playerY != 9){
        gridArray[playerX][playerY] = ". ";
        gridArray[playerX][playerY+1] = "G ";
        playerY++;
    }

    system("CLS");
    displayGrid(rows, columns);
    cout << playerX << ": " << playerY << endl;
}
void firstTime(){
    displayGrid(rows, columns);
    cout << playerX << ": " << playerY << endl;
    return;
}

int main()
{
    initGrid(rows,columns);
    firstTime();
    while(true){
        movePlayer();
    }

    return 0;
 }

快速代码说明:

一个多维数组将作为图形来显示正在发生的事情。这个以函数"initGrid"开头的数组将打印出"。"数组和屏幕上的字符串

Generate Pieces函数接受填充了"的数组。字符串,并使用随机数生成器,它放置"T"字符串和1个"X"字符串。"X"将是目标,而"T"将是杀死玩家的陷阱。

Generate Player做同样的事情,但只放置1个"G"字符串。这是播放器。

initGrid被调用后,主函数内部是"firstTime"函数,没什么复杂的,只是把数据显示到屏幕上。

最后,我有一个while循环调用函数"movePlayer",使用相同的数组,根据用户输入的内容,它将相应地移动"G"字符串,并用空"替换G字符串的最后一个位置"。"字符串。

我试图返回第二个G字符串的位置,一旦我这样做了,我试图用一个"。字符串,但它失败了,因为代码没有删除第二个字符,一旦第二个字符从数组中取出(第二个g字符对应于第一个g字符移动),第一个g字符被删除。

我不知道下一步该做什么,起初这似乎是一个简单的问题,但它给了我一个挑战。

谢谢你的阅读,我希望能尽快得到我的问题的答案。

我用asan和ubsan运行了你的代码,它告诉我你在第23行cout << gridArray[i][x];中有一个越界访问,你访问了不存在的gridArray[8]

看起来你混淆了行和列。我建议你也用消毒液。

最新更新