Randomly spawning on a map

Hi guys, I've been trying to get this piece of code to work but I'm hitting a wall
I don't want to go and copy people's codes (as this is for my assignment and i'm pretty sure that isn't allowed and I could get caught???) as it doesn't help me understand where I went wrong.

anyways excuse the long code I've looked at previous posts and people always ask for the full code instead of the little bit they cannot fixed

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
  #include <iostream>
#include <windows.h>
#include <time.h>

using namespace std;

char Map[30][53] = {

"#--------------------------------------------------#",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"|                                                  |",
"#--------------------------------------------------#" };



bool running = true;
int Gamespeed = 100;

int main()
{
	int Z = 0, p1 = 0, p2 = 0;
	srand(time(0));
	p1 = rand() % 48 - 1;
	p2 = rand() % 48 - 1;
	Map[p1][p2] = "Z";

	while (running == true)
	{
		system("cls");
		for (int y = 0; y < 30; y++)
		{
			cout << Map[y] << endl;
		}
		for (int y = 0; y < 30; y++)
		{
			for (int x = 0; x < 53; x++)
			{
				

				switch (Map[y][x])
				{
				case 'M':
				{
					if (GetAsyncKeyState(VK_UP) != 0)
					{
						int y2 = (y - 1);

						switch (Map[y2][x])
						{
						case ' ':
						{
							Map[y][x] = ' ';
							y -= 1;
							Map[y2][x] = 'M';
						}break;
						}
					}

					if (GetAsyncKeyState(VK_DOWN) != 0)
					{
						int y2 = (y + 1);

						switch (Map[y2][x])
						{
						case ' ':
						{
							Map[y][x] = ' ';
							y += 1;
							Map[y2][x] = 'M';
						}break;
						}
					}

					if (GetAsyncKeyState(VK_RIGHT) != 0)
					{
						int x2 = (x + 1);

						switch (Map[y][x2])
						{
						case ' ':
						{
							Map[y][x] = ' ';
							x += 1;
							Map[y][x2] = 'M';
						}break;
						}
					}
					if (GetAsyncKeyState(VK_LEFT) != 0)
					{
						int x2 = (x - 1);

						switch (Map[y][x2])
						{
						case ' ':
						{
							Map[y][x] = ' ';
							x -= 1;
							Map[y][x2] = 'M';
						}
						}
					}
				}
				}
			}
		}
	}

	while (running)
	{
		system("cls");
		for (int y = 0; y < 30; y++)
		{
			cout << Map[y] << endl;
		}
		for (int y = 0; y < 19; y++)
		{
			for (int x = 0; x < 53; x++)
			{
				switch (Map[y][x])
				{
				case 'Z':;
				}
			}
		}
	}

	return 0;
}


as you can see it's this part of the code I'm trying to figure out where I am going wrong
1
2
3
4
5
int Z = 0, p1 = 0, p2 = 0;
	srand(time(0));
	p1 = rand() % 48 - 1;
	p2 = rand() % 48 - 1;
	Map[p1][p2] = "Z";
Last edited on
your rows is only 18 so the 'Z' character will show up outside of your map.
your column is 52 wide so you need to subtract 2 so the 'Z' chracter will not show up on the edge.

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
#include <iostream>
#include <windows.h>
#include <time.h>
#include <cstdlib>

using namespace std;

char Map[30][53] = {

	"#--------------------------------------------------#",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"#--------------------------------------------------#" };



bool running = true;
int Gamespeed = 100;

int main()
{	
	srand(time(0));
	int p1 = (rand() % 16) +1;
	int p2 = (rand() % 50) +1;
	Map[p1][p2] = 'Z';

       //row
	for (int i = 0; i < 18; i++)
	{
               //column
		for (int j = 0; j < 52; j++)
		{ 
			std::cout << Map[i][j];
		}

		std::cout << std::endl;

	}

	int x;
	cin >> x;

	return 0;
}
Last edited on
awesome okey, I see what I was doing wrong, I didn't realize that the two numbers were supposed to be rows and columns when i think about it, it makes perfect sense and I duno how I never managed to see it myself.. blame my crap teachers for not preparing me for my assignment haha :)

am I right in assuming this will only spawn 1? and if so, how would I go about spawning more than one, without having to duplicate the code multiple times?

like

int p1 = (rand() % 16) +1;
int p2 = (rand() % 50) +1;
int p3 = (rand() % 15) +1;
int p4 = (rand() % 23) +1;
int p5 = (rand() % 13) +1;
int p6 = (rand() % 34) +1;

?
or would I have to put in another line of code? involving srand?
Last edited on
you are right, the program will only spawn 1.
you need to put some check to see if the location is taken.
you only need to use srand(time(0)) once.

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
#include <iostream>
#include <windows.h>
#include <time.h>
#include <cstdlib>

using namespace std;

char Map[30][53] = {

	"#--------------------------------------------------#",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"|                                                  |",
	"#--------------------------------------------------#" };



bool running = true;
int Gamespeed = 100;

int main()
{	
	// you only need to use srand once in your program
	srand(time(0));

	int spawn = 0;

	// number of characters you want to populate.
	while (spawn < 4)
	{
		int p1 = (rand() % 16) +1;
		int p2 = (rand() % 50) +1;
		
		if(Map[p1][p2] == ' ')
		{
			Map[p1][p2] = 'Z';
			spawn++;	
		}
	}

	// display 
       
	//row
	for (int i = 0; i < 18; i++)
	{
        //column
		for (int j = 0; j < 52; j++)
		{ 
			std::cout << Map[i][j];
		}

		std::cout << std::endl;

	}

	int x;
	cin >> x;

	return 0;
}
oh okey I see yeah, so you add more by adding a while loop, okey cool I always forget you can do something like that, thanks man!

I'm looking at the code, and I'm trying to figure out what part is meant to check if a spot was taken is it "if(Map[p1][p2] == ' ')" and if so, could you explain a little I understand the vast of it.. I know the code is saying IF Map[p1][p2] == .. I don't get the "' ')" part? is it sort of like a predetermined thing? like putting ' ' means that if something is in this space then it is taken? sorta like you check spaces for 'something' then move on, and if by chance something is inside ' ' then you move to a different location?

I'm probably not explaining myself very good.
Last edited on
ya that is pretty much it.
sorry if i made it confusing. what i meant was if the spot was empty.
it just checks if that particular row and column contains a space.
if it does add a z to it.
if it has a z on it skip.
yeah you didn't real make it confusing for me, its just I wasn't really taught this type of stuff so I'm sorta self-learning while trying to do my assignment :/ sucks I know.

seems pointless to start a new post now, but my next big attack is getting the zombies to randomly move by themselves. and was wondering if you could give me some pointers? could I do something kinda like the randomly spawning zombies but instead of having them spawn make them move to new coordinates kinda... I don't know I'm confusing myself. a little
i'm also still learning.
it is one of those things that grows arms and legs.
it can get really complex.

you would need to track the position of each zombie
check if they are colliding with each other.
if they are then one has to move to a different direction.

I actually gave up on trying to develop a game from scratch using c++.

but here are some links if your interested in developing a complete game written in c++.

https://github.com/LaurentGomila/SFML-Game-Development-Book

you can also watch videos on youtube.

https://www.youtube.com/watch?v=Cbw9f4RReME

have you heard of unity3d.com they have a game engine it is has a steep learning curve but it is really powerful. you code it using c# though. once you get a hang of c++ it is not that difficult to learn.

Topic archived. No new replies allowed.