How to make a map in console?

Pages: 12
ok so i need to output it in MainGame but how? i cant use i and j outside fo the loops, unless i need to make i and j outside of the for loops then i can use them again, idk, i never used for loops to make a graphical map before so its all very confusing but im understanding it very well so far.
Your map generator function is fine except for the cout's. If you want to successfully print this map with the pet and all, you should make a function that (again) goes through both for loops but instead of initializing what the map has it in it, it just has the cout's and prints the map, spaces and pet and all.
could you just show me what you mean? Im a visual learner its too difficul to try to guess this stuff. I know im supposed to put the couts in a different function but i have no idea how its all supposed to look.
Last edited on
make a separate function:

1
2
3
4
5
6
7
8
9
10
11
void print ( array arr[20][40])
{
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 40; j++)
		{
			cout << arr[i][j] << endl;
		}
		cout << endl;	
	}
}


and have MapGenerator call it, by simply passing in your map.
Last edited on
One thing you will notice is that if you ever change the size of the map, you are going to have to change the size in numerous places. Do yourself a favor and before you go on, do as I suggested before and start using variables.
ok i dont get it i give up, i have 600 other lines of code to work on so i'll just come back to it.

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

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

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

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];
            gameMap[4][4] = pet;
        }
        cout << endl;
    }
}

void MainGame()
{
    MapGenerator();
}

void PetAI()
{

}
Last edited on
You still have to actually USE the function bud.
Yes but you didn't tell it to do anything. It wont know what to do unless you tell it to.

Addition:

Also, pretty much everything you're declaring in your map generator function should be in your MainGame function. If not when you later use the map (to move the pet etc.) it wont actually work. ATM your map only exists in the MapGenerator function, you want it to be part of your game so you should be declaring it in your MainGame function.
Last edited on
ok hold on im just going to make a map generator class, that will make everything easier.
You don't have a map generator type you're trying to make do you? You could have a map class, where you make maps, but you're only going to be using one, so I see little help in that.
what do you mean by map generator type? I plan on having more than one map, with randomly generated events and other things.
Okay, that's fine. It sounded as if you were going to have one map that the pet moved around in. When you create a class you are (for all intense and purposes) creating a new data type. So if you create a map generator class then you'll have a data type (with member functions and the whole shebang) that is a map generator. You'd be able to make new map generators etc. Not new Maps.
You have more than one map? At the same time? That seems a little out of the reach of a console program for you right now. I'd stick to having one. In which case you will only need functions.

The map generator type is what you will use in conjunction with your class. If I have a class called class myClass(){...}. When you go to use the class the variable will be of type myClass.
ok well i'll just stick with functions for now then, ok so now i need to call the map generator right? so in main game i called it, so then... i dont even know. I havbe a learning disorder that makes things very difficult sometimes, its taken me 5 years just to get the basics of C++ down, it should only take the average person 2, so i apologive if im repeating questions. can you just modify my code to make it work? Seeing it completed will help me understand it, and if theres something i dont get i'll ask.
No, we won't write your code for you because you need to learn to do it yourself. I may work on some pseudo-code later on but until then you need to work on the code some more.

Not to sound rude, but I'm not going to give you your code simply because you have a learning disorder. I have an issue with not being able to understand simple concepts and am unable to do basics tasks, but that doesn't mean I have someone do them for me.
The biggest advice I can give you is just take everything slowly, step by step. If you want to do ____ you'll need A, B, and C. So, how do you do A? Well to do A you need 1 and 2. To do B you need 3, 4, and 5. And to do C you need 6, and 7. Make 1, 2, 3, 4, 5, 6, and 7 all functions that do what they need to do. Make functions A, B, and C and have them call their respective helper functions. Then make function _____ and have it call A, B, and C. It always helps to break things up, almost like a tree, focus on the leafs, then branches, then the trunk. Maybe drawing it out visually will help. Take it slow and think very specifically.

I wont write you code because doing it is the best way to learn anything. I have a LD too and doing something (especially doing it wrong) can help a lot. If you have specific questions about why something is working, please contact us, but I know Protomega wont write code, and neither will I. It's for your own good, so you can learn. Good Luck and Good night.
Last edited on
She gives some pretty good advice and her saying good night is a queue that she is tired, which means I think it's time for me to turn off my computer in the basement and head up to my laptop upstairs. Leave me a Private Message with some contact info if you want and we can talk about this elsewhere versus filling up a post which what is essentially a tutorial.

Good night bud.
Last edited on
ok i sent you a pm.
Ok i think i got it working for the most par, now im trying to make an AI so it will move on its own but im having a little trouble with that, what do i need to do here?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include "Prototypes.h"

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

int main()
{
    int PETXCOORD;
    int PETYCOORD;
    int MOVEX;
    int MOVEY;

    MainGame(PETXCOORD, PETYCOORD, MOVEX, MOVEY);
}

void MapGenerator(int PETXCOORD, int PETYCOORD, int MOVEX, int MOVEY)
{
    char pet = '*';
    char gameMap[20][40];
    int mapRows = 20;
    int mapColumns = 40;
    int number;

    for(int i = 0; i < mapRows; i++) // How many rows to loop through.
    {
        for(int j = 0; j < mapColumns; j++) // How many columns to loop through.
        {
            gameMap[i][j] = ' '; //create a space in each element
            gameMap[MOVEX][MOVEY] = pet;
            cout << gameMap[i][j];
            for(int k = 0; k < PETXCOORD; k++)//move Left/Right
            {
                MOVEX;
                for(int l = 0; l < PETYCOORD; k++)//Move up/down
                {
                    MOVEY;
                }
            }
        }
        cout << endl;
    }
}

void MainGame(int PETXCOORD, int PETYCOORD, int MOVEX, int MOVEY)
{
    MapGenerator(PETXCOORD, PETYCOORD, MOVEX, MOVEY);
}

void PetAI(int PETXCOORD, int PETYCOORD, int MOVEX, int MOVEY)
{
    srand(time(0));
    int LR_Movement;
    int UD_Movement;

    LR_Movement = rand() % 2;
    UD_Movement = rand() % 2;

    PETXCOORD = rand() % 20;
    PETYCOORD = rand() % 40;

    switch(LR_Movement)
    {
        case 0:
            MOVEX++;
            break;
        case 1:
            MOVEX--;
            break;
    }

    switch(UD_Movement)
    {
        case 0:
            MOVEY++;
            break;
        case 1:
            MOVEY--;
            break;
    }
}
Last edited on
bump
Topic archived. No new replies allowed.
Pages: 12