阵列打印基本地牢爬虫

时间:2016-11-13 17:26:19

标签: c++ arrays string multidimensional-array

我的名字是Eric,我在编写一个基本的" dungeon crawler"时遇到了问题。使用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;
 }

代码的快速解释:

多维数组将用作显示正在进行的内容的图形。该数组以函数&#34; initGrid&#34;开头。将打印出来#34;。 &#34;数组和屏幕上的字符串

Generate Pieces函数将该数组填充为&#34;。 &#34;字符串,并使用随机数生成器,它放置&#34; T&#34;字符串和1&#34; X&#34;串。这&#34; X&#34;将是目标,而T将成为杀死玩家的陷阱。

Generate Player做同样的事情,但只放置1&#34; G&#34;串。这是玩家。

调用initGrid之后,主函数内部是&#34; firstTime&#34;功能,没有任何复杂,只是向屏幕显示数据。

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

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

我在这里写下一个空白,我接下来要做什么,起初看起来似乎是一个简单的问题,但是它给了我一笔钱。

感谢阅读,我希望尽快得到答案。

1 个答案:

答案 0 :(得分:1)

我使用asanubsan运行了您的代码,告诉我您在第23行cout << gridArray[i][x];访问了gridArray[8],但您访问的{{1}}不存在

看起来你混合了行和列。我建议你也使用消毒剂。

相关问题