Need help with a random map maker

My code is kind of long and cant be repeated over and over again without closing the program. Can someone please help me shrink 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
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
string randoms;

int main()
{
     srand(time(0));

    const string oneone[2] = { "1", "0" };
    string oneones = oneone[rand() % 2];

    const string onetwo[2] = { "1", "0" };
    string onetwos = onetwo[rand() % 2];

    const string onethree[2] = { "1", "0" };
    string onethrees = onethree[rand() % 2];

    const string onefour[2] = { "1", "0" };
    string onefours = onefour[rand() % 2];

    const string onefive[2] = { "1", "0" };
    string onefives = onefive[rand() % 2];


    const string twoone[2] = { "1", "0" };
    string twoones = twoone[rand() % 2];

    const string twotwo[2] = { "1", "0" };
    string twotwos = twotwo[rand() % 2];

    const string twothree[2] = { "1", "0" };
    string twothrees = twothree[rand() % 2];

    const string twofour[2] = { "1", "0" };
    string twofours = twofour[rand() % 2];

    const string twofive[2] = { "1", "0" };
    string twofives = twofive[rand() % 2];


    const string threeone[2] = { "1", "0" };
    string threeones = threeone[rand() % 2];

    const string threetwo[2] = { "1", "0" };
    string threetwos = threetwo[rand() % 2];

    const string threethree[2] = { "1", "0" };
    string threethrees = threethree[rand() % 2];

    const string threefour[2] = { "1", "0" };
    string threefours = threefour[rand() % 2];

    const string threefive[2] = { "1", "0" };
    string threefives = threefive[rand() % 2];


    const string fourone[2] = { "1", "0" };
    string fourones = fourone[rand() % 2];

    const string fourtwo[2] = { "1", "0" };
    string fourtwos = fourtwo[rand() % 2];

    const string fourthree[2] = { "1", "0" };
    string fourthrees = fourthree[rand() % 2];

    const string fourfour[2] = { "1", "0" };
    string fourfours = fourfour[rand() % 2];

    const string fourfive[2] = { "1", "0" };
    string fourfives = fourfive[rand() % 2];


    const string fiveone[2] = { "1", "0" };
    string fiveones = fiveone[rand() % 2];

    const string fivetwo[2] = { "1", "0" };
    string fivetwos = fivetwo[rand() % 2];

    const string fivethree[2] = { "1", "0" };
    string fivethrees = fivethree[rand() % 2];

    const string fivefour[2] = { "1", "0" };
    string fivefours = fivefour[rand() % 2];

    const string fivefive[2] = { "1", "0" };
    string fivefives = fivefive[rand() % 2];


    const string sixone[2] = { "1", "0" };
    string sixones = sixone[rand() % 2];

    const string sixtwo[2] = { "1", "0" };
    string sixtwos = sixtwo[rand() % 2];

    const string sixthree[2] = { "1", "0" };
    string sixthrees = sixthree[rand() % 2];

    const string sixfour[2] = { "1", "0" };
    string sixfours = sixfour[rand() % 2];

    const string sixfive[2] = { "1", "0" };
    string sixfives = sixfive[rand() % 2];


    const string sevenone[2] = { "1", "0" };
    string sevenones = sevenone[rand() % 2];

    const string seventwo[2] = { "1", "0" };
    string seventwos = seventwo[rand() % 2];

    const string seventhree[2] = { "1", "0" };
    string seventhrees = seventhree[rand() % 2];

    const string sevenfour[2] = { "1", "0" };
    string sevenfours = sevenfour[rand() % 2];

    const string sevenfive[2] = { "1", "0" };
    string sevenfives = sevenfive[rand() % 2];


    cout << oneones;
    cout << onetwos;
    cout << onethrees;
    cout << onefours;
    cout << onefives << endl;
    cout << twoones;
    cout << twotwos;
    cout << twothrees;
    cout << twofours;
    cout << twofives << endl;
    cout << threeones;
    cout << threetwos;
    cout << threethrees;
    cout << threefours;
    cout << threefives << endl;
    cout << fourones;
    cout << fourtwos;
    cout << fourthrees;
    cout << fourfours;
    cout << fourfives << endl;
    cout << fiveones;
    cout << fivetwos;
    cout << fivethrees;
    cout << fivefours;
    cout << fivefives << endl;
    cout << sixones;
    cout << sixtwos;
    cout << sixthrees;
    cout << sixfours;
    cout << sixfives << endl;
    cout << sevenones;
    cout << seventwos;
    cout << seventhrees;
    cout << sevenfours;
    cout << sevenfives << endl;
}
Last edited on
First, take your main function and rename it, (I.E. mapper), then make a new main function with a do while loop.

The loop tells you what you can do, each option denoted by a number, then it asks for your choice as a number. Enter the right number to repeat the loop and call the mapper function. Enter a different number to exit the loop and exit the program.

Second, you can reduce your code by ninety percent, depending on what you're trying to do with it.

If you just need "1" or "0" for each space, you can just make a single array 70 elements long, then use a for loop to iterate through each element with random choosing what to put into each. (This can work for any number of possible data choices as long as the same set of choices can be a valid option for each element).
If you want to change or display a specific space of your map later, you can use this formula, ((row-1)* how many columns) + column + 1. (Note this assumes you are referencing a space by column and row with 1 as the first of each.)

Then you use a for loop to iterate through the array with cout, you can have an if statement checking for multiples of how many columns you have (five in this case) to cout an endl at those intervals.
I have two problems
For the first section: I don't know how to add functions into the main code as it is different to Arduono IDE

and secondly: I have no idea how to pull of the second section of what you just said
Here is my take on it, using a bit of DarkLight's concept. Since you are only holding a single character, and not an entire sentence, you don't actually need string, but can use char instead.

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
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;

// defines
#define map_width 10 // the map width
#define map_height 10 // the map height
#define map_icons 3 // the amount of icons you want (ie, '0', '1')

char Map[map_width][map_height]; // This is the global array for your map
// You can access an element by typing Map[#x][#y], where #x/#y are xy cords
// The cordinates are offset by 1
// For example, Map[2][7] is x 3, y 8
// Make sure to not to do any number equal or greater then map_width/map_height
// Map[10][2] will cause errors to occur


// This function will generate a new map when it is called
void GenerateMap()
{
	// The 2 for loops cycle through each element of Map, giving it a new identity (new icon)
	for(int y = 0; y < map_height; y++) // y first
	{
		for(int x = 0; x < map_width; x++) // x second
		{
			// the switch statement lets you have multiple options
			switch(rand() % map_icons) // random number between 0 and what you set for map_icons-1
			{
			case 0: // if the random number is 0, set the map icon to '0'
				Map[x][y] = '0'; // notice that we use '0' for chars, instead of "0"
				break;
			case 1:
				Map[x][y] = '1';
				break;
			case 2: // was just showing you can have more then 2 options
				Map[x][y] = '#';
				break;
			default: // default will only trigger if map_icons is bigger then the amount of cases
				Map[x][y] = ' ';
			}
		}
	}
}

// simple render of map function
void RenderMap()
{
	// for loops to cycle
	for(int y = 0; y < map_height; y++)
	{
		for(int x = 0; x < map_width; x++)
		{
			// display the character at this location
			cout << Map[x][y];
		}
		// make sure we create new lines
		// try deleting this to see what will happen
		cout << endl;
	}
	// bonus new line at the end
	cout << endl;
}

int main()
{
	// this will give a unique map every time
	// try deleting it to see what will happen
	srand(time(NULL));

	// first we generate a map
	GenerateMap();

	// then we can render a map
	RenderMap();

	// just a line of code to keep the program open (you may delete this)
	system("PAUSE");
	return 0;
}


If you have questions, just ask
Last edited on
Thank you, this serves my purpose.
I am also wondering if it is possible to change the color of the text and say only use "0"
Topic archived. No new replies allowed.