2D Array to create a grid map

How would I create a simple grid-like map using a 2D array, in C++?
I want to create a grid-like map as a 2D array with 15 columns and 10 rows. This map will be interactive, I.e.- the user can move up, down, left or right.

So for a simple start it will look like this: (P being the player which can move around within the walls, the underscores will show up as empty spaces)

WWWWWWWWWWWWWWW
W________________________W
W________________________W
W________________________W
W__________P_____________W
W________________________W
W________________________W
W________________________W
W________________________W
WWWWWWWWWWWWWWW

Really appreciate any help to get started, thank you
Ive got a game im working on that is almost exactly like that... want me to post the source code? you could pick it apart and find some useful info for your game.
I don't think it would help much, I am so bad at all this so it will be hard to read for me but I have made a lot of progress since I posted this, I have the grid created and I have managed to use a pointer to change the location of 'P' by the user entering W for up, A for left, D for right and S for down.

I had it working fine in main (), now I am trying to move it to a class and keep getting errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "Player.h"
#include "map.h"

void Movement(char key, char *player)
{	
	char key = getch();
	do {
    switch(key)
      {
		case 'w': //UP
		  if (*player != 'W'){
			  *player = ' '; player -= 16; *player = 'P'; system("cls"); for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
		  else {for (int y = 0; y < 10; y++) cout << Map::Map[1][1] << endl; break;}
        case 'a': //LEFT
		 if (*player != 'W'){
         *player = ' '; player -= 1; *player = 'P'; system("cls"); for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
		  else {for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
        case 'd': //RIGHT
			if (*player != 'W'){
         *player = ' '; player += 1; *player = 'P'; system("cls"); for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
		  else {for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
		case 's': //DOWN
			if (*player != 'W'){
         *player = ' '; player += 16; *player = 'P'; system("cls"); for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
		  else {for (int y = 0; y < 10; y++) cout << Map[y] << endl; break;}
        default: break;
	  }
	}
	while(exit != 1);


I am having trouble displaying the grid which is a 2D array called Map. The error message coming up is A non-static member reference must be valid to a specific object.
Sorry just realised that looks messy to read, its 6.30AM trying to get this done staying up all night.
//columns 15 and rows 10.
//to make your life easier is to use a for loop.
//you can achieve a lot with for loop and much more simpler when coding.
//easy to understand as well.
int main()
{
int grid[10][15];
for(int i = 0;i < 10;i++) //row by 10.
{
for(int j =0;j < 15;j++) //columns by 15.
{
grid[i][j]='W';
}
}
return 0;
}
I do not want to help you to much but I hope you get my graft. If statement makes the code messy, hard to understand and which would start to confuse your brain. Good Luck.
Last edited on
You could make a structure called player.
 
struct player{int i,j;};player plr;

I think this is more helpfull than a pointer. Also you could try making something like this:
1
2
3
4
5
6
7
8
9
10
void game(char map[][randomnumberhere]){
char direction;
while(your exit condition here){
display(map);
direction=detect_keyboard_input();
move_player(direction,plr);
change_map(map,plr);
clear();
}
}

If you still can't figure it out then I can write you a full code but that wont help you too much. Also the move_player function can be something like this
1
2
3
4
5
6
7
void move_player(char direction,player plr){
switch(direction){
case 'w':if(plr.i>=0)plr.i--;break;
case 's':if(plr.i<=max_number_of_lines)plr.i++;break;
....
//and so on
}

After you change the player's coords then you change the array. Also having a variable "player lastpos " that contains the players last position is also helpfull. You just do
1
2
Map[plr.i][plr.j]='P';
Map[laspos.i][laspos.j]=whatever you want here;

Hope this helps! Happy coding!
Last edited on
Topic archived. No new replies allowed.