-
Notifications
You must be signed in to change notification settings - Fork 0
/
hunt-and-kill.c
151 lines (124 loc) · 3.21 KB
/
hunt-and-kill.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2023 <[email protected]>
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc., 59
// Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdlib.h>
#include "util/dynarr.h"
#include "maze.h"
#define LEN(arr) (sizeof(arr)/sizeof(arr[0]))
static void hunt(maze_t *m);
static int
random_comparer(const void *p0, const void *p1)
{
(void) p0; (void) p1;
return rand() % 2 == 0 ? -1 : 1;
}
static void
shuffle(void *arr, size_t n, size_t size)
{
qsort(arr, n, size, random_comparer);
}
static int
count_visited_nbors(maze_t *m, int x, int y)
{
size_t i;
maze_wall_t *nbor, walls[4];
int visited_nbors;
walls[0] = MAZE_WALL_NORTH;
walls[1] = MAZE_WALL_SOUTH;
walls[2] = MAZE_WALL_WEST;
walls[3] = MAZE_WALL_EAST;
visited_nbors = 0;
for (i = 0; i < LEN(walls); ++i) {
if ((nbor = maze_get_nbor_sharing_wall(m, x, y, walls[i]))
&& *nbor != MAZE_WALL_ALL)
visited_nbors += 1;
}
return visited_nbors;
}
static int
count_unvisited_nbors(maze_t *m, int x, int y)
{
size_t i;
maze_wall_t *nbor, walls[4];
int unvisited_nbors;
walls[0] = MAZE_WALL_NORTH;
walls[1] = MAZE_WALL_WEST;
walls[2] = MAZE_WALL_SOUTH;
walls[3] = MAZE_WALL_EAST;
unvisited_nbors = 0;
for (i = 0; i < LEN(walls); ++i) {
if ((nbor = maze_get_nbor_sharing_wall(m, x, y, walls[i]))
&& *nbor == MAZE_WALL_ALL)
unvisited_nbors += 1;
}
return unvisited_nbors;
}
static void
walk(maze_t *m, int x, int y)
{
size_t i;
int nx, ny;
maze_wall_t *nbor, walls[4];
if (count_unvisited_nbors(m, x, y) == 0) {
hunt(m);
return;
}
walls[0] = MAZE_WALL_NORTH;
walls[1] = MAZE_WALL_WEST;
walls[2] = MAZE_WALL_SOUTH;
walls[3] = MAZE_WALL_EAST;
shuffle(walls, LEN(walls), sizeof(walls[0]));
for (i = 0; i < LEN(walls); ++i) {
nx = x + MAZE_WALL_OFFSET_X(walls[i]);
ny = y + MAZE_WALL_OFFSET_Y(walls[i]);
if ((nbor = maze_get(m, nx, ny))
&& *nbor == MAZE_WALL_ALL) {
maze_remove_wall(m, x, y, walls[i]);
walk(m, nx, ny);
break;
}
}
}
static void
hunt(maze_t *m)
{
size_t i;
int x, y;
maze_wall_t *nbor, walls[4];
walls[0] = MAZE_WALL_NORTH;
walls[1] = MAZE_WALL_WEST;
walls[2] = MAZE_WALL_SOUTH;
walls[3] = MAZE_WALL_EAST;
for (y = 0; y < m->height; ++y) {
for (x = 0; x < m->width; ++x) {
if (*maze_get(m, x, y) != MAZE_WALL_ALL ||
count_visited_nbors(m, x, y) == 0)
continue;
for (i = 0; i < LEN(walls); ++i) {
if ((nbor = maze_get_nbor_sharing_wall(m, x, y, walls[i]))
&& *nbor != MAZE_WALL_ALL) {
maze_remove_wall(m, x, y, walls[i]);
walk(m, x, y);
return;
}
}
}
}
}
extern void
maze_hunt_and_kill(maze_t *m, int w, int h, int seed)
{
maze_init(m, "hunt_and_kill", w, h, seed);
walk(m, rand()%w, rand()%h);
}