How to make a map in console?

Pages: 12
Ok so for my text pet game im making i want there to be a small map so that the pet can move around but i honestly done even know how or where to begin with the map, im making a map generator function but like i said im not sure how to draw the map on the screen and put the pet on it and have them move around. The pet will not be player controlled so theres no need for any of that.

1
2
3
4
5
6
7
8
9
void MapGenerator()
{
    char pet = '*';
    int petXCoord = 0;
    int petYCoord = 0;
    int gameMap[10][10];

    for()
}


Thats all i have so far, how should i draw the map?
Last edited on
Have you ever looped through a multidimensional array?

Edit: That was suppose to be more than just the question..

If not, you would start by making a for loop that will loop through your rows, but in that for statement have another that goes through the columns. So it will start at row 1 (technically 0) and then go through each column, and then move on until there are no more rows to go through.
Last edited on
Like this?

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 <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main()
{
    char pet = '*';
    int petXCoord = 0;
    int petYCoord = 0;
    char gameMap[10][10];
    string space = "O";
    string other = "Q";
    string border = "|";
    string bottomBorder = "_";

    for(int i = 0; i < 5; i++)
    {
        cout << other;
        for(int j = 0; j < 5; j++)
        {
            gameMap[i][j] = '0';
        }

    }
}


That doesnt seem to work for me, i know im doing it wrong, and to answer your question i have never looped through a multidemensional array before, i dont even use arrays, i use vectors for pretty much everything. But i need to learn them so now is better than ever.
Last edited on
The beauty and curse about programming is there are multiple ways to solve the same answer. What I'm noticing is that you are using cout to produce the border when you should actually be using the array. So here is how I look at multidimensional array looping:

1
2
3
4
5
6
7
for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
{
	for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
	{
		// This is (usually) where most of your actions will take place.
	}
}


So, let's look at your objective as of right now. To make a perimeter that is 10 spaces wide and 10 spaces tall (We get this size because of your array). We then need to then check to see if we are at the first or last row of the map. If we are, we simply need to fill in the element with one of your borders. if not, then we need to check to see if we are on the first column or the last column, if we are, put in a border, if not, put in a space.

Edit: People can get into more detail if they want, but in a general sense.. An array is like a vector except it has less dynamic properties i.e. it will have a fixed amount of elements in it. Where as a vector (as you know) you can push_back into the vector to add an element.
Last edited on
ok so like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    char pet = '*';
    int petXCoord = 0;
    int petYCoord = 0;
    int rowsToLoop = 10;
    int columnsToLoop = 10;
    char gameMap[10][10];
    char space = '0';

    for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
    {
        for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
        {
            cout << gameMap[rowsToLoop][columnsToLoop];
        }
    }
}


what else do i need to do? i think i need to output the grid, im not going to use a grid, but i would like one for debugging so i know that its actually outputting correctly.
Sort of, except in gameMap[rowsToLoop][columnsToLoop] that should be gameMap[i][j]. The rowsToLoop and columnsToLoop is the amount to loop through. So what you wrote there would keep outputting whatever was in gameMap[10][10] 100 times (10 rows with 10 columns -> 10 * 10 = 100 elements).

Also, you still have to insert some type of value into the elements. So add in

 
gameMap[i][j] = "X";


to the for loop that goes through the columns and see what happens. You're getting there!

Edit: I forgot to explain that you choose to use gameMap[i][j] because i gives you the current row you are on and j gives you the current column you are working with.
Last edited on
ok so i did this

1
2
3
4
5
6
7
8
9
for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
    {
        for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
        {
            gameMap[i][j] = 'x';

            cout << gameMap[i][j] << endl;
        }
    }


but it just outputs x's in a long vertical line, why is that?
Because of how you are using endl. Remember, anything you do in the inner for loop is done to every column.

So it starts at row 0 and is then told to go through every column in row 0 AND while it's going through all the columns, insert x into the corresponding element.

So! You actually need to use endl at the end of each row (In the first for loop after the inner for loop).
ok so like this?

1
2
3
4
5
6
7
8
9
10
for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
    {
        cout << endl;
        for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
        {
            gameMap[i][j] = 'x';

            cout << gameMap[i][j];
        }
    }


that works they are all in a column but theres a gap at the top why is that? theres also another problem, when i change the amount of rows and columns to 30, the x,s dont output right and the program crashes, it looks something like this

xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxx
xx
Last edited on
The gap is because you have put cout << endl; after the inner for loop and not before.

As for the outputting issue, can I see all of your code to see what the issue might be?
ya here it is

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
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main()
{
    char pet = '*';
    int petXCoord = 0;
    int petYCoord = 0;
    char gameMap[10][10];
    char space = '0';

    for(int i = 0; i < 20; i++) // How many rows to loop through.
    {
        cout << endl;
        for(int j = 0; j < 20; j++) // How many columns to loop through.
        {
            gameMap[i][j] = 'x';

            cout << gameMap[i][j];
        }
    }
}
Ah that's because when you put in 20 you are trying to go out of scope of the array. The array only has 100 elements, 10 rows and 10 columns. So when i or j is anything above 10 it is going to say that you are trying to modify the value inside an element that doesn't exist. i.e. gameMap[11][11].

We can solve this issue a few ways. The simplest way for you to do it without confusing you would be to make two ints:

1
2
int maxRows = 10;
int maxColumns = 10;


then you could change your initiliation of gameMap to gameMap[maxRows][maxColumns]. Then! in your for loops instead of using a number you can use maxRows and maxColumns respectively.

There is another, more efficient way, but it might be a little more confusing.
ok i see, i just changed the array size: char gameMap[20][40];

so now i need to put the pet on the map i tried this but its not working

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
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main()
{
    char pet = '*';
    int petXCoord = 0;
    int petYCoord = 0;
    char gameMap[20][40];
    char space = '0';

    for(int i = 0; i < 20; i++) // How many rows to loop through.
    {
        for(int j = 0; j < 40; j++) // How many columns to loop through.
        {
            gameMap[i][j] = ' ';

            cout << gameMap[i][j];
        }
        cout << endl;
    }

    gameMap[1][1] = pet;
}
You have to reprint the map. Have you ever used functions?
"Have you ever used functions?"

all the time. so do i put it in a while loop?
Last edited on
Okay, so basically you are going to have to make a few functions. The first is a function to create the map. Let's call it createMap. The next would be a function to print the map like printMap. Then you'll have to start making functions for the various things you want your pet to do.
Ok, i already had a few, one is calle MapGenerator, thats where the generator is, and MainGame where it will be called, so what do i do now?
Well, besides the fact that you now need to make functions to interact with the pet, you start using your functions. Note, you will need to have a function that generates the map and another function to print the map. Don't have the two be the same function, it won't work.
like this?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

void PetAI();
void MainGame();
void MapGenerator();

int main()
{
    MainGame();
}

void MapGenerator()
{
    char pet = '*';
    int petXCoord = 0;
    int petYCoord = 0;
    char gameMap[20][40];

    for(int i = 0; i < 20; i++) // How many rows to loop through.
    {
        for(int j = 0; j < 40; j++) // How many columns to loop through.
        {
            gameMap[i][j] = ' '; //create a space in each element

            cout << gameMap[i][j];
        }
        cout << endl;
    }
}

void MainGame()
{
    MapGenerator();
}

void PetAI()
{

}
No, because you are printing out the map when you generate it. You want the two to be two separate functions.
Pages: 12