gpt4 book ai didi

c++ - 需要帮助将迷宫实现为二维数组 C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:33 32 4
gpt4 key购买 nike

所以我要创建一个寻宝游戏,用户可以在这个游戏中穿过一个隐藏着生命值和陷阱的迷宫。目标是在不死的情况下找到宝藏。但是,我需要创建一个 map 并且我有一个我生成的 map 。我想知道是否有一种方法可以将基于文本的迷宫复制并粘贴到数组中,而无需将其放入 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
}

最佳答案

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

#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 个字符并且终止空值是遇到),然后应用你的随机陷阱和宝藏放置。

例如:

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...

关于c++ - 需要帮助将迷宫实现为二维数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908203/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com