I'm working on a project that involves developing a game similar to nethack, I have to write it only in C (for now anyway). The idea is that I need to create rooms that are made up of periods ('.'). These rooms need to be placed randomly in the terminal, which I have a grid (basically the dungeon). I've written some code, so hopefully you'll be more encouraged to help me with my problem. The code is posted below and I'll explain it in detail and my problem below it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "dungeon.h"
#define HEIGHT 105
#define WIDTH 160
#define N 10
/*Declared the functions that I created.*/
void createDungeon();
bool doOverlap(point l1, point r1, point l2, point r2);
void makeRoom(room aRoom);
int main() {
srand(time(NULL));
createDungeon();
return 0;
}
/*
* Created a function that initializes the dungeon. Each room struct
* has four points that make up the room, and are assigned.
*/
void createDungeon() {
char dungeonGrid[HEIGHT][WIDTH];
//room *rooms = malloc(10 * sizeof(room));
//room *rooms_t = malloc(10 * sizeof(room));
room rooms[N];
int i, y, x, q, r, f;
int a;
int counter = 0;
makeRoom(rooms[counter]);
for (int i = 1; i < 10; i++) {
makeRoom(rooms[i]);
if (doOverlap(rooms[i].top_left, rooms[i].bottom_right, rooms[counter].top_left, rooms[counter].bottom_right)) {
makeRoom(rooms[i]);
counter++;
}
}
/*for (i = 0; i < 10; i++) {
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;
/*Created two for loops that goes through the dungeon grid and puts a space.*/
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
dungeonGrid[y][x] = ' ';
}
}
/*Created three for loops that go through the dungeon grid and assigns a '.' for each
* point in the room to the grid.
*/
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] = '.';
}
}
}
}
/*These two for loops print out every character that it finds in the dungeon grid.*/
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
printf("%c", dungeonGrid[y][x]);
}
printf("\n");
}
}
/*Created a boolean method that deals with the rooms overlapping that takes in
* 4 points, l1 = top left point and r1 = bottom right point of the first rectangle,
* l2 = top left point, and r2 = bottom right point of the second rectangle.
*/
bool doOverlap(point l1, point r1, point l2, point r2) {
if (l1.x > r2.x || l2.x > r1.x) {
return false;
}
if (l1.y < r2.y || l2.y < r1.y) {
return false;
}
return true;
}
void makeRoom(room aRoom)
{
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;
aRoom.top_left.y = randomY;
aRoom.top_left.x = randomX;
aRoom.top_right.y = randomY;
aRoom.top_right.x = randomX + width;
aRoom.bottom_left.y = randomY + height;
aRoom.bottom_left.x = randomX;
aRoom.bottom_right.y = randomY + height;
aRoom.bottom_right.x = randomX + width;
}
Now let me explain my code a little bit, at first I only had 2 functions, the createdungeon() function and the doOverlapping() function, because if you look at the commented block of my code, you will see that is how I initially create all of my rooms and I put them in a room * array that was malloc'd. This way actually worked in the sense that I was able to print out all 10 of my rooms in the terminal, but the problem was the overlapping, because in the code I initially made it so that you create all the rooms first, and it would be hard to compare, so I create a third function called makeRoom(), and this would make just one room, and so the idea is that I would make one room and place it randomly somewhere, and then start a for loop where i = 1 and compare it with the previous room. The initial that I placed, I used a counter on it, so that if there is an overlapping, then make another room at that 'i', and then increase the counter so that you keep comparing the room that you are trying to place, with the room that is already placed right before it.
The problem right now, is that no rooms are showing up in my terminal, and I'm not sure why. Any help would be appreciated.
EDIT: I forgot to show my header that I made
#ifndef DUNGEON_DUNGEON_H
#define DUNGEON_DUNGEON_H
typedef struct Point {
int y;
int x;
} point;
typedef struct Room {
point top_left;
point top_right;
point bottom_left;
point bottom_right;
} room;
#endif //DUNGEON_DUNGEON_H