Setting random obstacles?

Hi there everyone. I'm really struggling currently with getting this grid set up for a very basic game I'm trying to create. My task is to make the user input the size of a grid, which sets the inside the hollow and has a border full of stars (this has already been done). Then I need help trying to "populate the array with a random scattering of '*'s which will appear within the size of the grid. I have some comments of how this might potentially be done, but I'm not so sure on how to apply it. Any help is much appreciated!

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
#include "stdafx.h"
#include "Environment.h"
#include <iostream>


Environment::Environment()
{
	width = 0;
	height = 0;
}

void Environment::setEnvironment()
{
	std::cout << "To set the environment, please enter the width and the height:" << std::endl;
	std::cout << "Width: ";
	std::cin >> width;
	std::cout << "Height: ";
	std::cin >> height;
	std::cout << std::endl;

	gridArray = new char[width * height];



	for (int i = 0; i < width * height; i++)
	{
		gridArray[i] = ' ';
	}

	for (int i = 0; i < width; i++)		//fills in top and bottom row top row
	{
		gridArray[i] = '*';		//fills from 0 to width - 1 (top row filling from left to right)
		gridArray[(width * height) - i - 1] = '*';	//fills from width * height-1 (last array element backwards(bottom row from right to left))
	}

	for (int i = 0; i < height; i++)
	{
		gridArray[(i * width)] = '*';	//fills left column from top to bottom
		gridArray[(width * height)- 1 -(i * width)] = '*';	//fills right column from bottom to top
	}


	/*Option
	make a number of how many cells you want to put barriers in. One loop 
          that does that number of times (Loop of 0-5 for 5 barriers)
	each time round, random x value between 1 and width-2, y between 1 and height -2 (when setting this array element, 
	before place, check if one is there*/
}

void Environment::printEnvironment()
{
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			std::cout << gridArray[(i * width) + j];
		}
		std::cout << std::endl;
	}
	for (int i = 0; i < 9; i++)
	{

	}
}

Environment::~Environment()
{

}
Last edited on
you can roll a random number for each cell in the thing and make a choice based off that. You can do it at any rate you think is good as percents, for example 50-50 are stars, or 10% stars 90% not, etc...

picking a value of how many to set and then setting that many randomly also works, but is more trouble.
Topic archived. No new replies allowed.