1 Dimensional hollow grid array?

Hi there. I've been tasked with creating a simple console game with some tasks included on how to complete it. My first task is to
"
1. Write code which asks the user which size environment they want to play in and read user input of width and height.
2. Use these values to create a 1-dimentional array of size width * height.
3. Clear the array by setting all elements to ‘ ‘ (space).
4. Edge the array with ‘*’ symbols. Populate the array with a random scattering of ‘*’. See
screengrab above.

The "screengrab" for clarification looks like this:
https://gyazo.com/09f52ba8e7316d1bb2e5bead238ab7b2

Okay so I'm on the step of trying to create this hollow grid with a border of '*'s and I have code for it but I think something must be wrong with it. I'm not great with this, as when I've had a similar task before, we weren't using arrays to make the grid. Any help would be greatly 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
  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 j = 0; j < width; j++)
	{
		gridArray[j] = '*';
		gridArray[(width * height) - j] = '*';
	}

	for (int k = 0; k < height; k++)
	{
		gridArray[(k * width)] = '*';
		gridArray[(k * width) - 1] = '*';
	}
to map 2d into 1d in a C array, the formula is
[desired_row*maxcols+desired_col]
that is the same as a 2-d array
grid[desired_row][desired_col];

Is that what you are trying to do / asking?


Yeah, trying to represent a 1d array as a multidimensional as stated in the tasks. I need to have it have the border around the edge and then will have to have random obstacles inside the grid too.
ok... so you should be able to populate the "matrix" using that formula.
you can memset the top and bottom to * if you want, fast and 1 line of code.
Then you can loop over the rest, * out the sides (maxcol-1 and 0th col) and randomize the others?

I don't see exactly what is wrong with what you have, but that should work.
Easier, initialize all to * and randomize the middle part..
Last edited on
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
//  Convert a 2 dim row and col to a 1 dim index
int index (int r, int c, int w)
{  return r * w + c;
}

void make_top_border (char grid[], int w, int h)
{   int r = 0;
    for (int c=0; c<w; c++)
        grid[index(r,c,w)] = '*';
}
        
void make_bottom_border (char grid[], int w, int h)
{   int r = h-1;

    for (int c=0; c<w; c++)
        grid[index(r,c,w)] = '*';
}
        
void make_left_border (char grid[], int w, int h)
{   int c = 0;

    for (int r=0; r<h; r++)
        grid[index(r,c,w)] = '*';
}

void make_right_border (char grid[], int w, int h)
{   int c = w-1;

    for (int r=0; r<h; r++)
        grid[index(r,c,w)] = '*';
}

yes, that looks right to me, I think you are getting it.

I would still just do
memset(grid, '*', width*height); //I think, double check... memeset must be used with care.
and then do

int r,c;
for(r = 1; r < height -1; r++)
for(c = 1; c < width-1; c++)
{
double d = rand();
if (rand < -0.1) //10% chance
grid[index(r,c,w)] = '*'; //hopefully I didnt revers r and c there... trying to multi-task here
else
grid[index(r,c,w)] = ' ';
}

and call it good. But I prefer less lines of code generally.


Last edited on
Can I just say, going back to the original post, I am doing all of this in a class member function. Therefore I can't be declaring new local functions inside. Can someone please just explain what I have to change from my original code for it to work the way I want? I feel like there's just a simple change to perform.
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

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];
       memset (gridArray, '*',  width*height); //again, test this by itself, stop here and run it.  
                                                                  //should print the grid as all *s
                             //it is possible I have the parameters to memset out of order..!!!

        srand((int)clock());  //or whatever seed. might use a constant seed until debugged. 

      int r,c;
      for(r = 1; r < height-1; r++)
      for(c = 1; c< width-1; c++)
        if(rand() > 0.1) 
          gridArray[r*width+c] = ' '; //90% are turned to spaces.  the others are already *s. 

//done!





you need <cmemory> for memset. You need something for rand, I think its in <cstdlib>, I forget. I don't use it much and forget. Its possible to unroll that loop into a single for loop, but I can't think thru that right this min.
Last edited on
I need to be able to make this without the use of memset as we haven't been taught that and I don't believe they would be happy with it. What is a simple solution to changing my current code to make it work? As I believe it is a simple change, but don't know what.
memset replaces

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

with one faster, smaller, better line of code. You can use the loop if you fear to get ahead of your class. Most teachers seem to like it when a student learns stuff outside of class, though. You could use it to brown - nose :)

Last edited on
Alright, thanks mate
Anytime. I am about to go offline... if you have any trouble, let me know right away. Ive done a lot of 1-d matrix math, but trying to help you while in a meeting at work, which has my concentration a bit muddled.
Topic archived. No new replies allowed.