map spawning

Hello I'm new to C++ programming my friends and I want to make a simple little text adventure room we have some real basic knowledge on C++

I want to make a simple little map
that looks like this
http://imgur.com/P896Qw6

and I want to make it so I randomly spawn in any of the out side squares and I finish if I make it to the center 4 squares


so far I've gotten this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <time.h>

int main()
{

	char place[5][5];
	int xPlayer = rand() % 6;
	int yPlayer = rand() % 6;

	
	if (xPlayer == 1 || xPlayer == 6)
	{
		yPlayer;
	}
	
	if (yPlayer == 1 || yPlayer == 6)
	{
		xPlayer;
	}
}

I'm pretty sure if my google sources are correct that sould make the 6x6 grid and give me a random spawn anywhere inside, but I can't think of what I would put so I down spawn in the middle of the map
If you don't want to spawn in the center of the map, which is (2,2) then just put:

1
2
3
4
    while(x == 2 && y == 2) {
        xPlayer = rand() % 6;
        yPlayer = rand() % 6;
    }


after your initial assignments to x and y.
You could use one random number to decide on which border you are going to spawn, and then a second random number to decide which of the border squares you are going to spawn.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int side = rand() % 4;
int square = rand() % 5;
switch (side)
{
case 0: // left
	x = 0; y = square;
	break;
case 1: // right
	x = 4; y = square;
	break;
case 2: // top
	x = square; y = 0;
	break;
case 3: // bottom
	x = square; y = 4;
	break;
}
@Peter87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int side = rand() % 4;
int square = rand() % 5;
switch (side)
{
case 0: // left
	x = 0; y = square;
	break;
case 1: // right
	x = 4; y = square;
	break;
case 2: // top
	x = square; y = 0;
	break;
case 3: // bottom
	x = square; y = 4;
	break;
}

With this how would I set it up to move around my drawn map in the OP?
I haven't used many switch/case statements not too sure entirely how they work
Using if statements instead of switch it would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int side = rand() % 4;
int square = rand() % 5;
if (side == 0) // left
{
	x = 0; 
	y = square;
}
else if (side == 1) // right
{
	x = 4;
	y = square;
}
else if (side == 2) // top
{
	x = square;
	y = 0;
}
else // bottom
{
	x = square; 
	y = 4;
}


Note that this is for a 5x5 map. If you want it to work for a 6x6 map you need to make the place array larger and adjust the constants in the code above (I recommend using named constants).

How you should be moving the player around, I don't know. Are you using cin?
Last edited on
@Peter87, Yeah I'm using std::cin to take in commands from the user and with movements of North, East, South and West, (reason for the compass) also trying to make it so that each room has different types of puzzles (that part I can do anyway)
Topic archived. No new replies allowed.