-
Notifications
You must be signed in to change notification settings - Fork 2
/
tremaux.c
191 lines (167 loc) · 4.49 KB
/
tremaux.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "tremaux.h"
#include "maze.h"
#include "mouse.h"
#include "decide.h"
#include "direction.h"
void tremaux_fill(Maze * maze) {
int x, y;
// initialize the whole maze
for(x = 0 ; x <=15 ; x++) {
for(y = 0 ; y <= 15 ; y++) {
set_c_val(maze,x,y,0);
}
}
// set first cell as 1
set_c_val(maze,0,0,1);
}
// increment cell value
void tremaux_increment(Maze * maze, Mouse * mouse) {
int x = mouse_x(mouse),
y = mouse_y(mouse),
c_dir = mouse_c_dir(mouse);
int got_zero = 0,
temp_dir;
if(get_c_val(maze, x, y) == 1 && get_back_val(maze, mouse) >= 2) {
for(temp_dir = 0 ; temp_dir <= 3 ; temp_dir++) {
if(has_wall_dir(maze,x,y,temp_dir) == false) {
if(get_val_dir(maze,x,y,temp_dir) == 0) {
got_zero = 1;
}
}
}
if(got_zero == 0) {
set_c_val(maze, x, y, get_c_val(maze, x, y)+1);
}
}
else if (has_r_wall(maze, x, y, c_dir) &&
has_f_wall(maze, x, y, c_dir) &&
has_l_wall(maze, x, y, c_dir)) {
set_c_val(maze,x,y,100);
}
else set_c_val(maze, x, y, get_c_val(maze, x, y)+1);
}
// increment cell value on return
void tremaux_increturn(Maze * maze, Mouse * mouse) {
int x = mouse_x(mouse),
y = mouse_y(mouse);
set_c_val(maze, x, y, 10);
}
// increment cell value on return
void tremaux_increspeed(Maze * maze, Mouse * mouse) {
int x = mouse_x(mouse),
y = mouse_y(mouse);
set_c_val(maze, x, y, 1);
}
// increment cell value each time entering
int tremaux_decide(Maze * maze, Mouse * moose) {
int x = mouse_x(moose),
y = mouse_y(moose),
x_quad = (x >= 8),
y_quad = (y >= 8),
c_dir = mouse_c_dir(moose);
int low_val = INF_CELL,
new_dir = 0,
temp_dir,
temp_val,
i;
int dir_arry[4] = {
c_dir,
((c_dir+5)-2*(!x_quad*(c_dir==0)+!y_quad*(c_dir==1)+x_quad*(c_dir==2)+y_quad*(c_dir==3)))%4,
((c_dir+3)-2*(!x_quad*(c_dir==0)+!y_quad*(c_dir==1)+x_quad*(c_dir==2)+y_quad*(c_dir==3)))%4,
(c_dir + 2)%4
};
for(i = 0 ; i < 4 ; i++) {
temp_dir = dir_arry[i];
if(has_wall_dir(maze,x,y,temp_dir) == false) {
temp_val = get_val_dir(maze,x,y,temp_dir);
if(temp_val < low_val) {
low_val = temp_val;
new_dir = temp_dir;
}
}
}
// return result
switch((new_dir - c_dir + 4) % 4) { // switch on delta dir
case 0:
return NO_TURN;
break;
case 1:
return L_TURN;
break;
case 2:
return U_TURN;
break;
case 3:
return R_TURN;
break;
default:
return NO_TURN;
break;
}
}
// return to the beginning
int tremaux_return(Maze * maze, Mouse * moose) {
int x = mouse_x(moose),
y = mouse_y(moose),
c_dir = mouse_c_dir(moose);
int new_dir = 0,
temp_dir;
for(temp_dir = 0 ; temp_dir <= 3 ; temp_dir++) {
if(has_wall_dir(maze,x,y,temp_dir) == false) {
if(get_val_dir(maze,x,y,temp_dir) == 1) {
new_dir = temp_dir;
}
}
}
// return result
switch((new_dir - c_dir + 4) % 4) { // switch on delta dir
case 0:
return NO_TURN;
break;
case 1:
return L_TURN;
break;
case 2:
return U_TURN;
break;
case 3:
return R_TURN;
break;
default:
return NO_TURN;
break;
}
}
// return to the end
int tremaux_speed(Maze * maze, Mouse * moose) {
int x = mouse_x(moose),
y = mouse_y(moose),
c_dir = mouse_c_dir(moose);
int new_dir = 0,
temp_dir;
for(temp_dir = 0 ; temp_dir <= 3 ; temp_dir++) {
if(has_wall_dir(maze,x,y,temp_dir) == false) {
if(get_val_dir(maze,x,y,temp_dir) == 10) {
new_dir = temp_dir;
}
}
}
// return result
switch((new_dir - c_dir + 4) % 4) { // switch on delta dir
case 0:
return NO_TURN;
break;
case 1:
return L_TURN;
break;
case 2:
return U_TURN;
break;
case 3:
return R_TURN;
break;
default:
return NO_TURN;
break;
}
}