-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.h
101 lines (79 loc) · 2.4 KB
/
maze.h
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
#ifndef _MAZE_H
#define _MAZE_H
#define MAZE_DISPLAY_EMPTY_SQUARE 'o'
#define MAZE_DISPLAY_NO_SQUARE '.'
#define MAZE_DEFAULT_REWARD 0
#define MAZE_GET_SQUARE(M,X,Y) (*(*((M)->table + (X)) + (Y)))
#define MAZE_GET_UP_SQUARE(M,X,Y) MAZE_GET_SQUARE(M,X,(Y)+1)
#define MAZE_GET_DOWN_SQUARE(M,X,Y) MAZE_GET_SQUARE(M,X,(Y)-1)
#define MAZE_GET_LEFT_SQUARE(M,X,Y) MAZE_GET_SQUARE(M,(X)-1,Y)
#define MAZE_GET_RIGHT_SQUARE(M,X,Y) MAZE_GET_SQUARE(M,(X)+1,Y)
#define ELEMENT_TRANSLATE(E,TX,TY) __extension__ \
({ \
ELEMENT_SET_COORD((E),(E)->coord.x + (TX),(E)->coord.y + (TY)); \
})
#define MAZE_IS_VALID_POSITION(M,X,Y) \
(((X) >= 0) && ((X) < (M)->width) && ((Y) >= 0) && ((Y) < (M)->height))
#define MAZE_IS_GOAL(M,X,Y) \
((MAZE_GET_SQUARE(M,X,Y)->element) && (MAZE_GET_SQUARE(M,X,Y)->element->type == MAZE_ELEM_TYPE_GOAL))
#define MAZE_MOVE_ELEMENT(M,SX,SY,DX,DY) __extension__ \
({ \
/* \
if ((DX >= 0) && (DX < (M)->width) && (DY >= 0) && (DY < (M)->height)) \
{ \
*/ \
(MAZE_GET_SQUARE((M),(DX),(DY)))->element = \
(MAZE_GET_SQUARE((M),(SX),(SY)))->element; \
(MAZE_GET_SQUARE((M),(SX),(SY)))->element = 0; \
/* \
} \
*/ \
})
#define MAZE_ELEMENT_TRANSLATE(M,E,TX,TY) __extension__ \
({ \
if (MAZE_IS_VALID_POSITION(M,(E)->coord.x + (TX),(E)->coord.y + (TY))) \
{ \
MAZE_MOVE_ELEMENT(M,(E)->coord.x,(E)->coord.y, \
(E)->coord.x + (TX), (E)->coord.y + (TY)); \
ELEMENT_TRANSLATE(E,TX,TY); \
} \
})
#define MAZE_ELEMENT_MOVE_UP(M,E) MAZE_ELEMENT_TRANSLATE(M,E,0,1)
#define MAZE_ELEMENT_MOVE_DOWN(M,E) MAZE_ELEMENT_TRANSLATE(M,E,0,-1)
#define MAZE_ELEMENT_MOVE_LEFT(M,E) MAZE_ELEMENT_TRANSLATE(M,E,-1,0)
#define MAZE_ELEMENT_MOVE_RIGHT(M,E) MAZE_ELEMENT_TRANSLATE(M,E,1,0)
enum element_type
{
MAZE_ELEM_TYPE_INIT,
MAZE_ELEM_TYPE_GOAL
};
struct point
{
unsigned int x;
unsigned int y;
};
struct element
{
char name;
struct point coord;
signed int reward;
enum element_type type;
};
struct square
{
struct element *element;
};
struct maze
{
unsigned int height;
unsigned int width;
struct square ***table;
};
struct maze *maze_create(unsigned int width, unsigned int height);
void maze_delete(struct maze *m);
struct square *maze_add(struct maze *m, struct square *s, unsigned int x, unsigned int y);
void maze_remove(struct maze *m, unsigned int x, unsigned int y);
void maze_display(struct maze *m);
struct square *square_duplicate(struct square *s);
void square_free(struct square *s);
#endif