rogue style map generator

Alright, so I am still very new to C++ and this is one of my first project with that language. I am posting it here to get some feedback if possible to know what I should of maybe done, or if this is alright.

Now I didn’t use classes, …on purpose. I have not really covered that yet in C++ syntax and I have no clue on how to use constructors yet and pointers still confuse me a little. I come from C#, so I do know that wrapping this in a Game object would of made more sense than a namespace.

So this is a map generator made mostly for rogue style RPGs. However, in reality, you could use a Sprite sheet and use it for something else. I wanted to do something different then the usually random dungeon generator, so I made a town generator instead. It creates random size houses at random location without any overlap and with a door at a random location.

The header file is 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

namespace Generator
{
    // Change these two values to get a diferent size map.
    const int MAX_X = 50; 
    const int MAX_Y = 30;   
    const char FLR1 = '.';
    const char FLR2 = '-';
    const char WALL = '#';
    const char TREE = '&';
    const char CHAR = '@'; // Not implemented yet.
    const char ROCK = 'o';
    const char DOOR = ' ';
    
    char map[MAX_Y][MAX_X];
    
    // This function creates the map.
    // It would normally be called once at the beginning of the game.
    void Create()
    {       
        int posx = 0; 
        int posy = 0;
        int wall_x = 0;
        int wall_y = 0;
        int door_x = 0;
        int door_y = 0;
        int tmp = 0;
        bool piv = false, swt = false;
        bool door_in = false;
        bool mapCheck = true;
               
        // Loop to create the initial grounds.       
        for (int y = 0; y < MAX_Y; y++)
        {
            for (int x = 0; x < MAX_X; x++)
            {
                if (y == 0 || x == 0 || y == (MAX_Y - 1) || x == (MAX_X -1)) 
                    map[y][x] = WALL;
                else
                {
                    // Change this value to get more/less clutter.
                    tmp = rand() % 24;       
                    if (tmp == 5)
                        map[y][x] = ROCK;
                    else if (tmp == 6)
                        map[y][x] = TREE;
                    else 
                        map[y][x] = FLR1;
                    tmp = 0;
                }
            }
        }
        
        // Loop to create houses. 
        // 1 loop per house, I found that 6 was a good amount.        
        for (int i = 0; i < 6; i++)
        {
            // Setting all of the random values and some booleans.
            mapCheck = true;
            door_in = false;
            wall_x = rand() % 6 + 4;
            wall_y = rand() % 6 + 4;
            posy = rand() % 20 + 1;
            posx = rand() % 40 + 1;
            door_x = wall_x - (rand() % 2 + 1);
            door_y = wall_y - (rand() % 2 + 1);
            // random bools for the doors.
            piv = static_cast<bool>(rand() % 2);
            swt = static_cast<bool>(rand() % 2);
            
            // This loop checks for overlaping houses.         
            for (int f = posy - 1; f <= (posy + wall_y + 2); f++)
            {
                for (int g = posx - 1; g <= (posx + wall_x + 2); g++)
                {
                    if (map[f][g] == WALL || map[f][g] == FLR2)
                    {
                        mapCheck = false;
                        break;
                    }
                }
            }
            
            // if overlap, try again. :p
            if (!mapCheck)
            {
                i--;
                continue;        
            }
             
            // This loop creates the walls then places doors. 
            for (int y = posy; y <= (wall_y + posy); y++)
            {                
                for (int x = posx; x <= (wall_x + posx); x++)
                {   
                    if (y < MAX_Y - 1 && x < MAX_X - 1)
                    {                       
                        if ( y == posy || x == posx || y == posy + wall_y || x == posx + wall_x)
                                map[y][x] = WALL;
                        else 
                                map[y][x] = FLR2;
                        
                        if (door_in == false)
                        {
                            if (piv == true)
                            {
                                if (swt == true)
                                {
                                    if (map[y][x] == map[y][posx + door_x] && map[y][x] == WALL)
                                    {
                                        map[y][x] = DOOR; 
                                        door_in = true;  
                                    }
                                }
                                else
                                {
                                    if (map[y][x] == map[wall_y + posy][posx + door_x] 
                                        && map[y][x] == WALL)
                                    {
                                        map[y][x] = DOOR; 
                                        door_in = true; 
                                    }
                                }
                            }
                            else
                            {
                                if (swt == true)
                                {
                                    if (map[y][x] == map[posy + door_y][x] && map[y][x] == WALL)
                                    {
                                        map[y][x] = DOOR; 
                                        door_in = true;  
                                    } 
                                }
                                else
                                {
                                    if (map[y][x] == map[posy + door_y][wall_x + posx] 
                                        && map[y][x] == WALL)
                                    {
                                        map[y][x] = DOOR; 
                                        door_in = true;  
                                    } 
                                }
                            }
                        }
                    }                                            
                }
            }
        }
    }
    
    // This function draws the map.
    void Draw()
    {
         COORD p = { 0, 3 };
         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p ); 
           
         for (int i = 0; i < MAX_Y; i++)
         {
             for (int j = 0; j < MAX_X; j++)
             {
                 if (map[i][j] == WALL)
                 {
                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x01);
                     cout << map[i][j];
                 }
                 else if (map[i][j] == FLR1)
                 {
                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x02);
                     cout << map[i][j];
                 }
                 else if (map[i][j] == FLR2)
                 {
                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x06);
                     cout << map[i][j];
                 }
                 else if (map[i][j] == ROCK)
                 {
                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x08);
                     cout << map[i][j];
                 }
                 else if (map[i][j] == TREE)
                 {
                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0A);
                     cout << map[i][j];
                 }
                 else if (map[i][j] == DOOR)
                 {
                     cout << map[i][j];
                 }
             }
             cout << endl;
         }
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
    }
}



The Main cpp is 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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <windows.h>

#include "MapGen.h"

using namespace std;
using namespace Generator;

int main()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
    cout << "\nStart of application" << endl;
    srand(time(0));
    
    while (true)
    {
        // ** Normaly Create() would be outside of the game loop.
        // but for the sake of testing, its in the loop. 
        Create();
        
        // This will draw the map.
        Draw();
    
        COORD end = { 0, 34 };
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), end );
        
        cout << "Press Enter to Continue" << endl;
        cin.ignore(100, '\n');  
    }
    
    return 0;
}


Thank you for any input :)
Topic archived. No new replies allowed.