Help with code
Feb 15, 2017 at 9:31am UTC
I'm trying to figure out about how in my generate functions, I can randomly place a 'P' character and a 'G' character. But I don't understand how to change what I used for the random scatter of '*'s. Would really appreciate some help.
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
#include "stdafx.h"
#include "Environment.h"
#include <iostream>
#include <ctime>
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
}
srand(time(NULL));
for (int row = 0; row < width; row++)
{
for (int col = 0; col < height; col++)
{
gridArray[row * width + col];
if ((rand() % 100) < 30)
{
gridArray[row * width + col] = '*' ;
}
}
}
}
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 < 5; i++)
{
}
}
void Environment::generatePlayer()
{
srand(time(NULL));
int spawn;
spawn = rand() % (width * height) + 1;
if (gridArray[goal])
}
void Environment::generateGoal()
{
srand(time(NULL));
int goal;
}
Environment::~Environment()
{
}
Last edited on Feb 15, 2017 at 10:14am UTC
Feb 15, 2017 at 2:12pm UTC
You could try something like this (please, consider it merely a hint, because my advice would be to use the far more reliable vectors!):
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
#include <iostream>
#include <ctime>
class Environment {
public :
Environment() = default ;
~Environment();
void setEnvironment();
void printEnvironment();
void generatePlayer();
void generateGoal();
private :
int width {0};
int height {0};
int spawn_row {0};
int spawn_col {0};
int goal_row {0};
int goal_col {0};
char ** gridArray;
};
Environment::~Environment()
{
free(gridArray);
}
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 = (char **)malloc(sizeof (char **) * width);
for (int i = 0; i < height; i++)
{
gridArray[i] = (char *)malloc(sizeof (char *) * height);
}
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
gridArray[i][j] = ' ' ;
// fill right and left columns with asterisks
for (int i = 0; i < width; i++)
{
gridArray[i][height-1] = '*' ;
gridArray[i][0] = '*' ;
}
// std::cout << "\nleft and right columns filled with asterisks" << std::endl;
// printEnvironment();
// fill top and bottom rows with asterisks
for (int i = 0; i < height; i++)
{
gridArray[0][i] = '*' ;
gridArray[width-1][i] = '*' ;
}
// std::cout << "\ntop and bottom rows filled with asterisks" << std::endl;
// printEnvironment();
srand(time(NULL));
for (int row = 0; row < width; row++)
{
for (int col = 0; col < height; col++)
{
if ((rand() % 100) < 30)
{
gridArray[row][col] = '*' ;
}
}
}
// std::cout << "\nAbout 30% of environment filled with asterisks" << std::endl;
// printEnvironment();
}
void Environment::printEnvironment()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
std::cout << gridArray[i][j];
}
std::cout << std::endl;
}
}
void Environment::generateGoal()
{
srand(time(NULL));
// Note: gridArray indexes varies from 0 to width -1
// and from 0 to height -1.
// To set a variable that might be used as an index
// for gridArray requires:
// * (width - 1) to remain into right boundary
// * (width - 2) to exclude right column
// * (width - 2) + 1 to exclude right and left columns
// * (height - 1) to remain into top boundary
// * (height - 2) to exclude top row
// * (height - 2) + 1 to exclude top and bottom rows
// Also: check needed to prevent goal being placed
// on top of spawn (if already generated)
while ( (goal_row = rand() % (width - 2) + 1) == spawn_row
&& (goal_col = rand() % (height - 2) + 1) == spawn_col )
;
gridArray[goal_row][goal_col] = 'G' ;
}
void Environment::generatePlayer()
{
srand(time(NULL));
// Note: see Environment::generateGoal().
while ( (spawn_row = rand() % (width) + 1) == goal_row
&& (spawn_col = rand() % (height) + 1) == goal_col )
;
gridArray[spawn_row][spawn_col] = 'P' ;
}
int main()
{
Environment env;
env.setEnvironment();
env.generateGoal();
env.generatePlayer();
env.printEnvironment();
}
Topic archived. No new replies allowed.