-1

I'm trying to make random level generator that uses mersenne twister. Here's the code (it's just beginning, so it doesn't make much sense):

Generator.h:

//Level generator

#pragma once

class GameMap
{
public:


    static int bgmap[256][256];
    static int fg1map[256][256];
    static int fg2map[256][256];
    static int genposx, genposy, width, height;


    static std::mt19937 twister(int);
    static std::uniform_int_distribution<int> dist1(int, int);
    static std::uniform_int_distribution<int> dist2(int, int);
    static std::random_device rd;
    void Generate(int sizex, int sizey, int seed);

    GameMap();
    ~GameMap();
};

Generator.cpp:

//#include <SFML/Graphics.hpp>
#include <random>
#include "Generator.h"

GameMap::GameMap()
{
    dist1(1, 8);
    dist2(1, 248);
}


void GameMap::Generate(int sizex, int sizey, int seed = rd())
{
    twister(seed);

    genposx = 1;
    genposy = 1;

    do
    {
        genposx = dist2(twister);
        genposy = dist2(twister);
        width = dist1(twister);
        height = dist1(twister);

    } while (whatever);
}

The problem is that I can't convert uniform_int_distrubution to int. I get Intellisense error messages:

no suitable conversion from unifgorm_int_distribution<int>" to "int" exists

argument of type "std::mt19937 (*)(int)" is incompatible with parameter of type "int"

too few arguments in function call

All of those are on these lines:

genposx = dist2(twister);
genposy = dist2(twister);
width = dist1(twister);
height = dist1(twister);

I've lost many hours by searching in web an answer, but I couldn't find anything. Please help.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
Mohrcore
  • 23
  • 8
  • Well, why would you be trying to convert a `uniform_int_distribution` to an `int`? That makes no sense. I don't believe you that you've "lost many hours by searching in web an answer", because there is loads of reading material on the proper way to use a `uniform_int_distribution` and none of it is represented in your code. You're not even providing the right number of arguments to your functions `dist1` and `dist2`... – Lightness Races in Orbit Jul 20 '15 at 17:28
  • Why is everything in your class a static data member? You haven't provided definitions for any of those static members, and for some odd reason you keep writing the types of their constructor arguments when you declare them, so the compiler thinks they are member function declarations. This is probably not the answer you want to hear, but the errors you've made suggest you'd benefit greatly from reading a [C++ book](https://stackoverflow.com/q/388242/241631). – Praetorian Jul 20 '15 at 17:32
  • @Praetorian - No, this is not an answer which I wouldn't like to hear. Programming is just not actually in the first place of my interests, and I don't spend majority of my free time on it. Usually when I program I don't use objects, because it's low level programming aimed for hardware, so I'm newbie in that. I'm gonna buy some book, because it seems that what I learned about objects to this moment is a bullshit. Anyway, thanks for reply. – Mohrcore Jul 20 '15 at 17:57

1 Answers1

4

I think your main problem is declaring your class members as functions. I made some suggested changes to your code with brief explanations:

class GameMap
{
public:

    // are these really meant to be static (one for all GameMap's)
    // or do you want one set of map data for every GameMap object (non static)? 
    /*static*/ int bgmap[256][256];
    /*static*/ int fg1map[256][256];
    /*static*/ int fg2map[256][256];
    /*static*/ int genposx, genposy, width, height;


    // static std::mt19937 twister(int); // function declaration?
    std::mt19937 twister;

    // probably you don't want static function declarations
    std::uniform_int_distribution<int> dist1; //(int, int);
    std::uniform_int_distribution<int> dist2; //(int, int);
    static std::random_device rd;
    void Generate(int sizex, int sizey, int seed);

    GameMap();
    ~GameMap();
};

GameMap::GameMap()
: dist1(1, 8)
, dist2(1, 248) // initialize these here
{
}


void GameMap::Generate(int sizex, int sizey, int seed = rd())
{
    twister.seed(seed); // probably you meant this?

    genposx = 1;
    genposy = 1;

    do
    {
        genposx = dist2(twister);
        genposy = dist2(twister);
        width = dist1(twister);
        height = dist1(twister);

    } while (whatever);
}
Galik
  • 47,303
  • 4
  • 80
  • 117