需要帮助在二维数组c++中实现迷宫
Need help Implementing maze into 2d array c++
所以我创建了一个寻宝游戏,用户可以在隐藏生命值和陷阱的迷宫中移动。我们的目标是在不死的情况下找到宝藏。然而,我需要创建一个地图,并且我已经生成了一个地图。我想知道是否有一种方法可以将基于文本的迷宫复制并粘贴到数组中,而不将其放在主函数中,而是放在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!nn";
cout << "In order to beat this game you must find the treasuren";
cout << " that is located in the maze. You can move using the n";
cout << " arrow keys or WASD.nn";
cout << " Warning! There are traps that will take life away asn";
cout << " well as add life! However, they are hidden so be careful!n ";
cout << " Goodluck and have fun!nnnn";
}
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
}
您可以将迷宫定义为字符串的2D数组,存储为全局变量,如下所示:
#define LEVEL_COUNT (2)
const char* maps[LEVEL_COUNT][12] =
{
{
"||||||||||||",
"| | |",
"| | |",
"| |||| |",
"| |",
"| |",
"|||||| |",
"| | |",
"| | | |",
"| | |",
"| | |",
"||||||||||||",
},
{
"||||||||||||",
"| | |",
"| ||||| |",
"| | |",
"| |",
"| ||||",
"| |",
"| | |",
"| | |",
"||||||| |",
"| |",
"||||||||||||",
},
};
然后你可以将它们加载到你的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];
}
}
}
初始化为全零后,从mapCreation
函数调用loadMap
(如果映射数组中的任何字符串长度小于12个字符,并且遇到终止null),然后应用随机陷阱和宝藏放置。
例如:
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...
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 需要帮助在二维数组c++中实现迷宫
- 实现迷宫树以在DFS,BFS中使用
- 3D迷宫OpenGL实现