so im creating treasure hunting game user moves through maze hidden health , traps. goal find treasure without dying. however, need create map , have map generated. wondering if there way copy , paste text based maze array without putting in main function , instead drawmap function instead of filling each cell. appreciated. thank you!
// header files #include <cstdlib> #include <curses.h> #include <iostream> #include <windows.h> using namespace std; handle console = getstdhandle(std_output_handle); // use of setconsoletextattribute() // function prototypes void titlescreen(); // prints title , 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 white system("cls");// clears screen in order remove black background titlescreen(); // display title // do-while loop starts { cin >> option; // take in input if(option == 1) // temporary option check next screen { //display maze system("cls");// clears screen in order 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 user, temporary return 0; } void titlescreen(){ cout << " welcome treasure hunter!\n\n"; cout << "in order beat game must find treasure\n"; cout << " located in maze. can move using \n"; cout << " arrow keys or wasd.\n\n"; cout << " warning! there traps take life away as\n"; cout << " add life! however, hidden careful!\n "; cout << " goodluck , 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 = 0; < 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 = 0; < 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 }
you define mazes 2d array of strings, stored global variable this:
#define level_count (2) const char* maps[level_count][12] = { { "||||||||||||", "| | |", "| | |", "| |||| |", "| |", "| |", "|||||| |", "| | |", "| | | |", "| | |", "| | |", "||||||||||||", }, { "||||||||||||", "| | |", "| ||||| |", "| | |", "| |", "| ||||", "| |", "| | |", "| | |", "||||||| |", "| |", "||||||||||||", }, };
then can load them char array, setting spaces zero:
void loadmap( char arr[][12], int level) { if((level < 0) || (level >= level_count)) return; for(int = 0; < 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 0 else arr[i][j] = row[j]; } } }
call loadmap
mapcreation
function after initializing zeros (in case of strings in map array less 12 chars long , terminating null encountered), apply randomized traps , treasure placements.
e.g:
void mapcreation( char arr[][12], int level ) { int traps = 0; int lives = 0; int treasure = 0; int x; int y; for(int = 0; < 12; i++) { for(int j = 0; j < 12; j++) { arr[i][j] = 0; } } // load 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...