-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
136 lines (110 loc) · 2.38 KB
/
maze.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
#include "maze.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENT_SET_COORD(E,X,Y) __extension__ \
({ \
(E)->coord.x = (X); \
(E)->coord.y = (Y); \
})
struct maze *maze_create(unsigned int width, unsigned int height)
{
struct maze *ret;
ret = (struct maze *) malloc(sizeof(struct maze));
ret->width = width;
ret->height = height;
ret->table = (struct square ***) malloc(width*sizeof(struct square **));
memset((char *)ret->table,0,width*sizeof(struct square **));
while (width--)
{
*(ret->table + width) = (struct square **)
malloc(height*sizeof(struct square *));
memset(
(char *) *(ret->table + width),
0,
height*sizeof(struct square *)
);
}
return ret;
}
void maze_delete(struct maze *m)
{
unsigned int x, y;
x = m->width;
while (x--)
{
y = m->height;
while (y--)
{
if (MAZE_GET_SQUARE(m,x,y))
square_free(MAZE_GET_SQUARE(m,x,y));
}
free(*(m->table + x));
}
free(m->table);
free(m);
}
struct square *
maze_add(struct maze *m, struct square *s, unsigned int x, unsigned int y)
{
struct square *ret;
if (MAZE_GET_SQUARE(m,x,y))
maze_remove(m,x,y);
MAZE_GET_SQUARE(m,x,y) = ret = square_duplicate(s);
if (ret->element)
ELEMENT_SET_COORD(ret->element,x,y);
return ret;
}
void maze_remove(struct maze *m, unsigned int x, unsigned int y)
{
square_free(MAZE_GET_SQUARE(m,x,y));
MAZE_GET_SQUARE(m,x,y) = 0;
}
/* >>> TODO: do a macro if necessary */
struct square *square_duplicate(struct square *s)
{
struct square *ret;
ret = (struct square *) malloc(sizeof(struct square));
memcpy(ret,s,sizeof(struct square));
/* >>> TODO: duplicate the point if necessary */
if (s->element)
{
ret->element = (struct element *) malloc(sizeof(struct element));
memcpy(ret->element,s->element,sizeof(struct element));
}
else
ret->element = 0;
return ret;
}
void square_free(struct square *s)
{
free(s->element);
free(s);
}
void maze_display(struct maze *m)
{
unsigned int x, y;
__extension__ char linebuff[m->width + 1];
linebuff[m->width] = 0;
y = m->height;
while (y--)
{
x = m->width;
while (x--)
{
if (MAZE_GET_SQUARE(m,x,y))
{
if ((MAZE_GET_SQUARE(m,x,y))->element)
{
linebuff[x] =
(MAZE_GET_SQUARE(m,x,y))->element->name;
}
else
linebuff[x] = MAZE_DISPLAY_EMPTY_SQUARE;
}
else
linebuff[x] = MAZE_DISPLAY_NO_SQUARE;
}
printf("%s\n",linebuff);
}
}