Need help Implementing maze into 2d array c++

时间:2021-05-17 02:07:36

So Im creating a treasure hunting game where the user moves through a maze with hidden health and traps. The goal is to find the treasure without dying. However, I need to create a map and I have a map I generated. I was wondering if there was a way I could just copy and paste my text based maze into the array without putting it in the main function and instead the drawMap function instead of filling each cell. Any help would be greatly appreciated. Thank you!

所以我正在创建一个寻宝游戏,用户在隐藏的健康和陷阱的迷宫中移动。目标是找到宝藏而不会死亡。但是,我需要创建一个地图,我有一个我生成的地图。我想知道是否有一种方法可以将基于文本的迷宫复制并粘贴到数组中而不将其放在main函数中,而是将drawMap函数替换为填充每个单元格。任何帮助将不胜感激。谢谢!

// Header Files
#include <cstdlib>
#include <curses.h>
#include <iostream>
#include <windows.h>


using namespace std; 

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()


// Function Prototypes

void titleScreen(); // Prints Title and instructions


void mapCreation( char arr[][12], int level);

void drawMap(char arr[][12]);

bool update(char arr[][12], int &level, int &lives, int &score);


// Main Program
int main ()
    {
    // initialize variables
    int option;
    char baseMap[12][12];
    int level = 1;
    int lives = 3;
    int score = 0;
    bool gameOver = false;
    bool levelCompleted = false;


    SetConsoleTextAttribute(console, 240); // change background to white

    system("CLS");// clears screen in order to remove black background

    titleScreen(); // Display Title

    do // do-while loop starts
    {

    cin >> option; // take in input

        if(option == 1) // temporary option to check for next screen
            {
            //Display Maze

            system("CLS");// clears screen in order to remove black background
            while(gameOver == false)
                {
                 mapCreation( baseMap, level );
                 while(gameOver == false || levelCompleted == false )
                      {
                       drawMap(baseMap);
                       update(baseMap, level, lives, score);
                      }
                }
             }
        }
    while( option !=1); // condition of do-while loop

    system("pause"); // Pause for user, only temporary


    return 0;

   }








void titleScreen(){
    cout << " Welcome to Treasure Hunter!\n\n";
    cout << "In order to beat this game you must find the treasure\n";
    cout << " that is located in the maze. You can move using the \n";
    cout << " arrow keys or WASD.\n\n";
    cout << " Warning! There are traps that will take life away as\n";
    cout << " well as add life! However, they are hidden so be careful!\n ";
    cout << " Goodluck and have fun!\n\n\n\n";


}


void mapCreation( char arr[][12], int level )
    {
      int traps = 0;   
      int lives = 0;
      int treasure = 0;
      int x;
      int y;
        for(int i = 0; i < 12; i++)
            {
             for(int j = 0; j < 12; j++)
                {
                 arr[i][j] = 0;
                }

            }
        arr[1][1] = '1';
        switch (level)
        {
        case 1:
        arr[0][1] = '|';
        arr[1][1] = '|';
        arr[2][1] = '|';
        arr[2][2] = '|';
        arr[2][3] = '|';
        arr[2][4] = '|';
        arr[0][6] = '|';
        arr[1][6] = '|';
        arr[1][8] = '|';
        arr[2][6] = '|';
        arr[2][8] = '|';
        arr[3][4] = '|';
        arr[3][6] = '|';
        arr[3][7] = '|';
        arr[3][8] = '|';
        arr[4][4] = '|';
        arr[4][6] = '|';
        arr[5][1] = '|';
        arr[6][1] = '|';
        arr[6][3] = '|';
        arr[6][4] = '|';
        arr[6][5] = '|';
        arr[6][6] = '|';
        arr[6][7] = '|';
        arr[7][1] = '|';
        arr[7][6] = '|';
        arr[8][1] = '|';
        arr[8][6] = '|';
        arr[8][8] = '|';
        arr[9][1] = '|';
        arr[9][6] = '|';
        arr[9][8] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 2)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 1)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 2: // Level 2 Map

        arr[0][9] = '\n';
        arr[1][0] = '|';
        arr[1][1] = '|';
        arr[1][2] = '|';
        arr[1][4] = '|';
      //  arr[1][0] = '\n';
        arr[2][2] = '|';
        arr[2][4] = '|';
        arr[2][5] = '|';
        arr[2][6] = '|';
        arr[2][7] = '|';
        arr[2][8] = '|';
      //  arr[2][9] = '\n';
        arr[3][2] = '|';
        arr[3][4] = '|';
        arr[3][7] = '|';
      //  arr[3][9] = '\n';
        arr[4][7] = '|';
        arr[4][9] = '\n';
        arr[5][2] = '|';
        arr[5][4] = '|';
        arr[5][7] = '|';
      //  arr[5][9] = '\n';
        arr[6][0] = '|';
        arr[6][1] = '|';
        arr[6][2] = '|';
        arr[6][4] = '|';
        arr[6][6] = '|';
        arr[6][7] = '|';
        arr[6][8] = '|';
      //  arr[6][9] = '\n';
        arr[7][3] = '|';
        arr[7][5] = '|';
        arr[7][9] = '\n';
        arr[8][4] = '|';
        arr[9][4] = '|';
     //   arr[9][9] = '\n';
        arr[0][11] = '|';
        arr[1][11] = '|';
        arr[2][11] = '|';
        arr[3][11] = '|';
        arr[4][11] = '|';
        arr[5][11] = '|';
        arr[6][11] = '|';
        arr[7][11] = '|';
        arr[8][11] = '|';
        arr[9][11] = '|';
        arr[10][11] = '|';
      //  arr[11][11] = '\n';
        arr[10][0] = '|';
        arr[10][1] = '|';
        arr[10][2] = '|';
        arr[10][3] = '|';
        arr[10][4] = '|';
        arr[10][5] = '|';
        arr[10][6] = '|';
        arr[10][7] = '|';
        arr[10][8] = '|';
        arr[10][9] = '|';
        arr[10][11] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 4)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 2)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 3: // Level 3 Map

        arr[1][4] = '|';
        arr[1][6] = '|';
        arr[1][7] = '|';
        arr[1][8] = '|';
        arr[1][9] = '|';
        arr[2][2] = '|';
        arr[2][4] = '|';
        arr[3][0] = '|';
        arr[3][1] = '|';
        arr[3][2] = '|';
        arr[3][3] = '|';
        arr[3][4] = '|';
        arr[3][6] = '|';
        arr[4][6] = '|';
        arr[5][3] = '|';
        arr[5][2] = '|';
        arr[5][6] = '|';
        arr[5][7] = '|';
        arr[5][8] = '|';
        arr[5][9] = '|';
        arr[6][0] = '|';
        arr[6][1] = '|';
        arr[6][2] = '|';
        arr[6][6] = '|';
        arr[7][4] = '|';
        arr[7][5] = '|';
        arr[7][6] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 6)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 3)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 4:
        arr[3][2] = '|';
        arr[3][3] = '|';
        arr[3][4] = '|';
        arr[3][5] = '|';
        arr[3][6] = '|';
        arr[3][7] = '|';
        arr[4][3] = '|';
        arr[5][3] = '|';
        arr[5][5] = '|';
        arr[5][6] = '|';
        arr[5][7] = '|';
        arr[5][8] = '|';
        arr[6][3] = '|';
        arr[6][5] = '|';
        arr[6][8] = '|';
        arr[7][3] = '|';
        arr[7][5] = '|';
        arr[7][8] = '|';
        arr[8][3] = '|';
        arr[8][5] = '|';
        arr[8][8] = '|';
        arr[9][3] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 8)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 4)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }
        break;

        case 5:
        arr[0][1] = '|';
        arr[1][1] = '|';
        arr[1][6] = '|';
        arr[2][3] = '|';
        arr[2][4] = '|';
        arr[2][5] = '|';
        arr[2][6] = '|';
        arr[3][1] = '|';
        arr[3][6] = '|';
        arr[4][1] = '|';
        arr[4][2] = '|';
        arr[4][3] = '|';
        arr[4][4] = '|';
        arr[4][5] = '|';
        arr[4][6] = '|';
        arr[4][7] = '|';
        arr[4][8] = '|';
        arr[4][9] = '|';
        arr[5][1] = '|';
        arr[7][2] = '|';
        arr[7][3] = '|';
        arr[7][4] = '|';
        arr[7][5] = '|';
        arr[7][6] = '|';
        arr[7][7] = '|';
        arr[7][8] = '|';
        arr[8][3] = '|';
        arr[8][6] = '|';
        arr[9][6] = '|';
        while(treasure < 1)
            {
              x = (rand() % 10);
              y = (rand() % 10);
             if(arr[x][y] == '0')
               {
                arr[x][y] = 2;
                treasure++;
               }
            }
        while(traps < 10)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] == '0')
                {
                 arr[x][y] = 3;
                 traps++;
                }
            }
        while(lives < 5)
            {
             x = (rand() % 10);
             y = (rand() % 10);
             if(arr[x][y] = '0')
                {
                 arr[x][y] = 4;
                }
            }        
        break;
        }
    }

void drawMap(char arr[][12])
    {
     for(int i = 0; i < 12; i++)
        {
         for(int j = 0; j < 12; j++ )
            {
             if(arr[i][j] != 3 && arr[i][j] != 4)
               {
                cout << arr[i][j];
               }
            }
        }
    }

bool update(char arr[][12], int &level, int &lives, int &score)
    {
     bool levelCompleted = false;
     bool gameOver = false;

     return 0; // temporary holder
    }

1 个解决方案

#1


1  

You could define your mazes as 2D array of strings, stored as a global variable something like this:

您可以将迷宫定义为2D字符串数组,存储为全局变量,如下所示:

#define LEVEL_COUNT (2)
const char* maps[LEVEL_COUNT][12] = 
{
    {
        "||||||||||||",
        "|       |  |",
        "|       |  |",
        "|    ||||  |",
        "|          |",
        "|          |",
        "||||||     |",
        "| |        |",
        "| |    |   |",
        "|      |   |",
        "|      |   |",
        "||||||||||||",
    },
    {
        "||||||||||||",
        "|       |  |",
        "|   |||||  |",
        "|       |  |",
        "|          |",
        "|       ||||",
        "|          |",
        "|   |      |",
        "|   |      |",
        "|||||||    |",
        "|          |",
        "||||||||||||",
    },
};

Then you can load them into your char array, setting spaces to zero:

然后你可以将它们加载到char数组中,将空格设置为零:

void loadMap( char arr[][12], int level)
{
     if((level < 0) || (level >= LEVEL_COUNT))
         return;

     for(int i = 0; i < 12; i++)
     {
         const char* row = maps[level][i];
         for(int j = 0; j < 12; j++)
         {
             if(row[j] == 0)
                 break; // end of string
             if(row[j] == ' ')
                 arr[i][j] = 0;  // set spaces to zero
             else
                 arr[i][j] = row[j];
         }
     }
}

Call loadMap from your mapCreation function after initializing to all zeros (in case any of the strings in the map array are less than 12 chars long and a terminating null is encountered), then apply your randomized traps and treasure placements.

初始化为全零后,从mapCreation函数调用loadMap(如果地图数组中的任何字符串长度小于12个字符且遇到终止空值),则应用随机陷阱和宝藏。

e.g:

例如:

void mapCreation( char arr[][12], int level )
{
  int traps = 0;   
  int lives = 0;
  int treasure = 0;
  int x;
  int y;
  for(int i = 0; i < 12; i++)
  {
      for(int j = 0; j < 12; j++)
      {
          arr[i][j] = 0;
      }
  }

  // load the map:
  loadMap(arr, level);

  arr[1][1] = '1';
  switch (level)
  {
    case 1:
    while(treasure < 1)
    {
         x = (rand() % 10);
         y = (rand() % 10);
         if(arr[x][y] == '0')
         {
            arr[x][y] = 2;
            treasure++;
         }
     }
     // etc...

#1


1  

You could define your mazes as 2D array of strings, stored as a global variable something like this:

您可以将迷宫定义为2D字符串数组,存储为全局变量,如下所示:

#define LEVEL_COUNT (2)
const char* maps[LEVEL_COUNT][12] = 
{
    {
        "||||||||||||",
        "|       |  |",
        "|       |  |",
        "|    ||||  |",
        "|          |",
        "|          |",
        "||||||     |",
        "| |        |",
        "| |    |   |",
        "|      |   |",
        "|      |   |",
        "||||||||||||",
    },
    {
        "||||||||||||",
        "|       |  |",
        "|   |||||  |",
        "|       |  |",
        "|          |",
        "|       ||||",
        "|          |",
        "|   |      |",
        "|   |      |",
        "|||||||    |",
        "|          |",
        "||||||||||||",
    },
};

Then you can load them into your char array, setting spaces to zero:

然后你可以将它们加载到char数组中,将空格设置为零:

void loadMap( char arr[][12], int level)
{
     if((level < 0) || (level >= LEVEL_COUNT))
         return;

     for(int i = 0; i < 12; i++)
     {
         const char* row = maps[level][i];
         for(int j = 0; j < 12; j++)
         {
             if(row[j] == 0)
                 break; // end of string
             if(row[j] == ' ')
                 arr[i][j] = 0;  // set spaces to zero
             else
                 arr[i][j] = row[j];
         }
     }
}

Call loadMap from your mapCreation function after initializing to all zeros (in case any of the strings in the map array are less than 12 chars long and a terminating null is encountered), then apply your randomized traps and treasure placements.

初始化为全零后,从mapCreation函数调用loadMap(如果地图数组中的任何字符串长度小于12个字符且遇到终止空值),则应用随机陷阱和宝藏。

e.g:

例如:

void mapCreation( char arr[][12], int level )
{
  int traps = 0;   
  int lives = 0;
  int treasure = 0;
  int x;
  int y;
  for(int i = 0; i < 12; i++)
  {
      for(int j = 0; j < 12; j++)
      {
          arr[i][j] = 0;
      }
  }

  // load the map:
  loadMap(arr, level);

  arr[1][1] = '1';
  switch (level)
  {
    case 1:
    while(treasure < 1)
    {
         x = (rand() % 10);
         y = (rand() % 10);
         if(arr[x][y] == '0')
         {
            arr[x][y] = 2;
            treasure++;
         }
     }
     // etc...