Hello fellow programmers, I have to place weapons/enemies in random spots in a map and they cannot be in the same spots. I'm not sure how to make it so that they are in an array and so that they take certain spots in the map randomly. Would placing them in an array and shuffling them always have a different output? Or do I have to use nested for loops to randomly assign them? I'm pretty new to programming so any advice is greatly appreciated! I created an array towards the bottom of the code named placement where I might be able to assign them to the correct spots, but I'm lost, thanks in advance!
// Declarations
char map[] = { "**************************D" };
int randomMovementNumber;
const char PLAYERLOCATIONSYMBOL = 'P';
int playerLocationNumber = 0;
int placement[28];
// Classes
class Character
{
public:
int XP;
int attackPower;
int weapon;
};
class Monster
{
public:
int HP;
int XP;
};
class Weapon
{
public:
int attackPower;
};
// Function prototypes
void playerTravel();
void exploreSpace();
void emptyRoom();
#include <iostream>
#include <algorithm>
#include <random>
int main()
{
// place characters 'W' (weapons) and 'E' (enemies) at random positions in an array
const std::size_t NWEAPONS = 9 ; // number of 'W's
const std::size_t NENEMIES = 7 ; // number of 'E's
const std::size_t ARRAY_SZ = 60 ;
// sanity check: http://en.cppreference.com/w/cpp/language/static_assertstatic_assert( ARRAY_SZ >= (NWEAPONS+NENEMIES), "array is too small" ) ;
char array[ARRAY_SZ] ;
// place weapons in the first NWEAPONS positions
for( std::size_t i = 0 ; i < NWEAPONS ; ++i ) array[i] = 'W' ;
// place enemies in the next NENEMIES positions
for( std::size_t i = 0 ; i < NENEMIES ; ++i ) array[NWEAPONS+i] = 'E' ;
// fill the rest of the array with '_' (empty)
for( std::size_t i = (NWEAPONS+NENEMIES) ; i < ARRAY_SZ ; ++i ) array[i] = '_' ;
// randomly shuffle the elements of the array
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
// http://en.cppreference.com/w/cpp/numeric/random
// repeat this ten times (for testing)
for( int i = 0 ; i < 10 ; ++i )
{
// note: the random device in the GNU library on windows may be a spurious random device
// if that is the case, seed mt19937 with the current time
std::shuffle( array, array+ARRAY_SZ, std::mt19937( std::random_device{}() ) ) ;
// print out the suffled array
for( char c : array ) std::cout << c ;
std::cout << '\n' ;
}
}
The Mersenne Twister is a pseudorandom number generator (PRNG).
It is by far the most widely used general-purpose PRNG. ... It was designed specifically to rectify most of the flaws found in older PRNGs. It was the first PRNG to provide fast generation of high-quality pseudorandom integers.
The most commonly used version of the Mersenne Twister algorithm is based on the Mersenne prime 219937−1. The standard implementation of that, MT19937, uses a 32-bit word length
mersenne_twister_engine is a random number engine based on Mersenne Twister algorithm.
It produces high quality unsigned integer random numbers of type UIntType on the interval [0, 2w-1].
The following type aliases define the random number engine with two commonly used parameter sets:
Defined in header <random>
Type Definition mt19937 ... 32-bit Mersenne Twister by Matsumoto and Nishimura, 1998
Thanks for the information! I'm not quite sure why but when I run it I just get a non stop stream of white marks. I checked the code and it matches up so I'm not quite sure how to fix that :/