0

I've seen several threads on this, but I'm still not understanding why I've having the problem of going through my struct array.

So I have a header file with two typedef structs that I use in my c source code:

typedef struct Point{
    int y;
    int x;
} point;

typedef struct Room{
    point top_left;
    point top_right;
    point bottom_left;
    point bottom_right;
} room;



#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "dungeon.h"

#define HEIGHT 105
#define WIDTH 160

int randomSeed();
void createDungeon();

int main() {
    createDungeon();
    return 0;
}

void createDungeon(){
    char dungeonGrid[HEIGHT][WIDTH];
    room *rooms = malloc(10 * sizeof(room));
    int i,y,x;
    int a;

    for(i = 0; i < 10; i++){
        srand(randomSeed());
        int height = (rand() % (10 + 1 - 5)) + 5;
        int width = (rand() % (15 + 1 - 7)) + 7;

        int randomY = (rand() % (105 + 1 - 0)) + 0;
        int randomX = (rand() % (160 + 1 - 0)) + 0;

        rooms[i].top_left.y = randomY;
        rooms[i].top_left.x = randomX;
        rooms[i].top_right.y = randomY;
        rooms[i].top_right.x = randomX + width;
        rooms[i].bottom_left.y = randomY + height;
        rooms[i].bottom_left.x = randomX;
        rooms[i].bottom_right.y = randomY + height;
        rooms[i].bottom_right.x = randomX + width;

    }

   for(y = 0; y < HEIGHT; y++){
        for(x = 0; x < WIDTH; x++){
            dungeonGrid[y][x] = ' ';
        }
    }

    for(y = 0; y < HEIGHT; y++){
        for(x = 0; x < WIDTH; x++){
            for(a = 0; a < 10; a++){
                if(rooms[a].top_left.y <= y  &&  y <= rooms[a].bottom_left.y && rooms[a].top_left.x <= x  && rooms[a].top_right.x >= x){
                    dungeonGrid[y][x] = '.';
                }
            }
        }
    }


    for(y = 0; y < HEIGHT; y++){
        for(x = 0; x < WIDTH; x++) {
            printf("%c", dungeonGrid[y][x]);
        }
        printf("\n");
    }

}

in

t randomSeed(){
    srand(time(NULL));
    int randNum = (rand() % (100000 + 1 - 0)) + 0;
    return randNum;
}

What I'm trying to do: Basically I'm making a dungeon of size 105x160, with random sized rooms inside of it. These rooms are going to be shown in the terminal as a set of rows and columns that use the character '.' I'm basically using a for loop to find a random x and random y on the dungeon grid, and use that as my starting point to place a room. I'm also using rand() to get a random height and width. The minimum height and width needs to be 7x5 and I just chose an arbitrary number for the maximum size that it should be.

The problem: I checked the pointer for my rooms struct (*rooms) and found that at rooms[0 - 9], all of the points for top_left, top_right, etc are exactly the same number and it shouldn't be.

My Question: How do I properly iterate through my struct array, so that the there are different coordinates for each room? Maybe there is something wrong with the way I call rand()? I made a method to get various random numbers and I thought that my for loops would call that method and give a random number each time, so my coordinates will be different for each room.

DsDude
  • 135
  • 1
  • 8
  • 1
    Call `srand` once at the beginning of `main`. – user3386109 Jan 25 '17 at 20:37
  • That doesn't seem to be affecting anything, basically my problem is that all my coordinates are the same for each room in the room struct array, so in my terminal only one room is being produced in the dungeon, because obviously every coordinate is the same for every room, which it shouldn't be. – DsDude Jan 25 '17 at 20:43
  • Nevermind, I see what you mean't, I need to delete the other srands too. – DsDude Jan 25 '17 at 20:45
  • 1
    Thank you so much, it seems that was the only problem lol! – DsDude Jan 25 '17 at 20:45

0 Answers0