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!
#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 = newchar[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()
{
}