mercredi 12 juin 2019

Random number generators and making a "maze"

I just started on a maze generator project for fun and I'm having some issues with using rand() and srand() in creating my maze.

My "maze" so far is just a 20x20 int array that I've set up to be 0 by default. In my createMaze(), I'll go through each element and generate two random numbers, a randomNum and a randomThresh. If the randomNum is >= to the randomThresh, I set the value of the element = 1. This tells me later that the spot needs to be "filled" by an asterisk.

Where I am confused is that the output looks not random at all. It will have the same pattern of having the first entire column filled and the rest of the columns after that mostly filled as well.

* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * *
* * * * * * *
* * * * * * * *
* * * * *

I've tried changing what I've used for srand() but that doesn't really work or make sense to change, as the numbers that I am getting for both the threshold and the number itself SEEM completely random when I print them out.

I've also tried changing the position of the threshold to inside the y-traversal loop rather than the x-traversal loop, so a given threshold is the same for an entire column. This hasn't worked either, as the result is the same.

game.cpp

#include <iostream>
#include "maze.h"

using namespace std;

int main() {
    Maze myMaze(1000);//Create Maze object
    myMaze.createMaze();//Set the maze
    myMaze.print();//Print the maze
    return 0;
}

maze.cpp

#include <iostream>
#include <string>
#include <ctime>//Used for the arguements in srand() to pass in time()
#include <cstdlib>//Used for both random functions srand() and rand()
#include "maze.h"

using namespace std;

Maze::Maze(int seed) {
    m_seed = seed;
}

bool Maze::createMaze() {//Set the m_grid of a particular Maze
    int randomNum = 0;
    int randomThresh = 0;
    srand(rand()*time(NULL)*m_seed);
    for(int y = 0; y < 20; y++) {//Height-wise traverse
        for(int x = 0; x < 20; x++) {//Length-wise traverse
            randomNum = rand()%m_seed;
            randomThresh = rand()%m_seed;
            if(randomNum >= randomThresh) {m_grid[x][y] = 1;}//Set the point in the grid if # >= threshold
        }   
    }
    return true;
}

void Maze::print() {
    for(int y = 0; y < 20; y++) {//Height-wise traverse
        for(int x = 0; x < 20; x++) {//Length-wise traverse
            if(m_grid[x][y] == 1) {cout << "* ";}
        }
        cout << endl;
    }
}

#ifndef MAZE_H
#define MAZE_H
#include <string>
#include <iostream>

using namespace std;

class Maze
{
    public:
        Maze(int seed);
        bool createMaze();
        void print();
    private:
        int m_seed;
        int m_grid[20][20] = {};//A 20x20 array that will form a "maze"
};
#endif

I understand that this isn't really a "maze" right now, but I wanted to know what the issue with what I'm doing was before I went any further.




Aucun commentaire:

Enregistrer un commentaire