c++ - 数组在基本地牢爬虫中打印出多个字符

标签 c++ arrays string multidimensional-array

<分区>

我叫 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调用后,main函数里面就是firstTime函数,没什么复杂的,就是把数据显示到屏幕上。

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

我试图返回第二个 G 字符串的位置,一旦我这样做了,我试图用一个“.”字符串替换它,但它失败了,因为代码没有删除第二个,并且一旦第二个一个不在数组中(第二个 g 字符移动对应于第一个 g 字符)第一个 G 字符被删除。

对于下一步应该做什么,我在这里一片空白,起初这似乎是一个简单的问题,但它让我为钱而奔波。

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

最佳答案

我用 asan 运行了你的代码和 ubsan这告诉我你在第 23 行有越界访问 cout << gridArray[i][x];你在哪里访问 gridArray[8]这是不存在的。

看起来您混淆了行和列。我建议您也使用 sanitizer 。

关于c++ - 数组在基本地牢爬虫中打印出多个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40576697/

相关文章:

javascript - 如何循环遍历数组,并从数据库 Angular 5 获取一些数据

java - 将多个对象添加到二维数组(TicTacToe 游戏)

C# 无法将字符串转换为字节数组以达到所需的结果?

ios - Objective C 为什么将字符串格式化为字符串

c++ - 如何从 LPCTSTR 转换为 std::string?

c++ - 浮点异常(核心转储)#694457

c++ - 如何在C++中基于第3列(索引除以3)快速排序一维数组

c++ - 差价合约监控程序

c++ - 派生类赋值运算符在base中的使用

c++ - 在cuda c++中删除主内存中的数组