我正在尝试使用此处描述的 BSP 为 roguelike 制作地牢生成器:Basic BSP
我对树结构有点陌生,我已经研究了一段时间,似乎真的无法弄清楚我哪里出错了。现在我有了它,所以它只会打印出所有“#”来表示每个叶节点的房间尺寸,这样我就可以看到它是否正常工作。我不断收到分段错误,我真的不确定我现在哪里出错了。如果有人可以查看我的代码并给我一些建议,我将不胜感激。谢谢!
#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define MIN_ROOM_HEIGHT 5
#define MIN_ROOM_WIDTH 10
struct tree
{
int max_x, min_x;
int max_y, min_y;
struct tree *Right_Down, *Left_Up;
};
typedef struct tree room;
room create_rooms(room **map, WINDOW *board, int max_x, int min_x, int max_y, int min_y)
{
room *temp = NULL;
if(!(*map))
{
temp = (room *)malloc(sizeof(room));
temp->Left_Up = temp->Right_Down = NULL;
(temp)->max_x = max_x;
(temp)->min_x = min_x;
(temp)->max_y = max_y;
(temp)->min_y = min_y;
*map = temp;
}
/* room structures for smaller rooms this room will be
* split into, Left/Up or Right/Down, depending on whether
* horizontal or vertical split was chosen.
*/
// room newLeft_Up, newRight_Down;
/* Position to split at for horizontal and vertical */
int split_horiz, split_vert;
/* Divide room into 2 smaller rooms, unless doing so
* would make them below minimum room size
*/
int split_type = rand() % 2;
int stop = 0;
/* 1 is Horizontal Split */
if(split_type == 1)
{
if((*map)->max_y > MIN_ROOM_HEIGHT+4)
{
while(stop == 0)
{
if((split_horiz = rand() % ((*map)->max_y-2)) < MIN_ROOM_HEIGHT+4);
{
stop = 1;
}
}
create_rooms(&(*map)->Left_Up, board, (*map)->max_x, (*map)->min_x, split_horiz-1, (*map)->min_y);
create_rooms(&(*map)->Right_Down, board, (*map)->max_x, (*map)->min_x, (*map)->max_y, split_horiz+1);
}
}
/* 2 is Vertical Split */
else
{
if((*map)->max_x > MIN_ROOM_WIDTH+4)
{
while(stop == 0)
{
if((split_vert = rand() % ((*map)->max_x-2)) < MIN_ROOM_WIDTH+4);
{
stop = 1;
}
}
create_rooms(&(*map)->Left_Up, board, split_vert-1, (*map)->min_x, (*map)->max_y, (*map)->min_y);
create_rooms(&(*map)->Right_Down, board, (*map)->max_x, split_vert+1, (*map)->max_y, (*map)->min_y);
}
}
char tiles[(*map)->max_y-1] [(*map)->max_x-1];
int i, j;
/* Print room tiles if this is a leaf node */
if((!((*map)->Left_Up)) && (!((*map)->Right_Down)))
{
for(i=(*map)->min_y; i < (*map)->max_y-1; i++)
{
for(j=(*map)->min_x; j < (*map)->max_x-1; j++)
{
tiles[i][j] = '#';
wmove(board, i, j);
wprintw(board, "#");
}
}
}
wrefresh(board);
}
int main()
{
/* ncurses initializations */
initscr();
start_color();
keypad(stdscr, TRUE);
refresh();
noecho();
/* counters */
int i, j;
/* seed srand for later use */
srand(time(NULL));
/* Initialize Game Board */
WINDOW *board;
board = newwin(LINES, COLS, 0, 0);
box(board, 0, 0);
room *map;
map = NULL;
create_rooms(&map, board, COLS-1, 1, LINES-1, 1);
wrefresh(board);
/* sleep for 5 secs */
sleep(5);
/* close ncurses */
endwin();
return 0;
}