Text based game assignment where I randomly assign objects into an array

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();

// Namespace
using namespace std;



// Main function
int main()
{

//Objects
Monster monster1;
monster1.HP = 3;
Monster monster2;
monster2.HP = 3;
Monster monster3;
monster3.HP = 3;
Monster monster4;
monster4.HP = 4;
Monster monster5;
monster5.HP = 4;
Monster monster6;
monster6.HP = 4;
Monster monster7;
monster7.HP = 4;
Monster monster8;
monster8.HP = 5;
Monster monster9;
monster9.HP = 5;
Monster monster10;
monster10.HP = 6;
Monster monster11;
monster11.HP = 6;
Monster monster12;
monster12.HP = 7;
Monster monster13;
monster13.HP = 7;
Monster monster14;
monster14.HP = 7;

Weapon crossbow;
crossbow.attackPower = 3;
Weapon flail;
flail.attackPower = 4;
Weapon broadSword;
broadSword.attackPower = 5;
Weapon dragonSlayer;
dragonSlayer.attackPower = 6;
Weapon spellOfTheGods;
spellOfTheGods.attackPower = 7;

Character player;
player.XP = 1;
player.attackPower = 1;
player.weapon = 1;


//Player Movement
while (player.XP > 0) {

// Map
int index = playerLocationNumber;
map[index] = 'P';
cout << map << endl;
for (int x = 0; x < 29; x++) {
if (map[x] != playerLocationNumber) {
map[x] = '*';
}
}

// Map spaces for enemies and items
for (int x = 1; x >= 1 && x < 28; x++) {
placement[x] = map[x];
}
> Would placing them in an array and shuffling them always have a different output?

It would have a random output (assuming that the random number generator was seeded randomly).

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
#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_assert
    static_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' ;
    }
}

http://coliru.stacked-crooked.com/a/c755ca7aa03e2992
http://rextester.com/OIYP60583
Thanks, this looks solid! I'll try it out and let you know the results. On line 35: std::mt19937: what does this do and what does it stand for?
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

https://en.wikipedia.org/wiki/Mersenne_Twister

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

http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
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 :/
Topic archived. No new replies allowed.