forked from abunai59/umouse2013
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.c
192 lines (168 loc) · 5.84 KB
/
solve.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
192
#include <p33FJ128MC802.h>
#include <stdbool.h>
#include "decide.h"
#include "move.h"
#include "walls.h"
#include "solve.h"
#include "main.h"
#include "mouse.h"
#include "maze.h"
#include "flood.h"
#include "direction.h"
#include "tremaux.h"
#include "delay_T4.h"
// solve the maze
void solve(void) {
Mouse mouse;
Maze maze;
Mouse * mouse_p = &mouse;
Maze * maze_p = &maze;
init_mouse(mouse_p);
init_maze(maze_p);
if(ALG == R_WALL) { //right wall hug
while (1){
// move forward 1 cell
track();
// get direction and turn
turn(r_wall());
}
}
else if(ALG == L_WALL) { //left wall hug
while (1){
// move forward 1 cell
track();
// get direction and turn
turn(l_wall());
}
}
else if(ALG == TEST) { //test algorithm
track();
track();
track();
track();
delay_T4(100);
// // solve
// init_fill(maze_p);
// while(1) {
// if (has_wall_dir(maze_p, mouse_x(mouse_p), mouse_y(mouse_p), E) == true) PORTBbits.RB2 = 1;
// else PORTBbits.RB2 = 0;
// if (mouse_c_dir(mouse_p) == N) PORTBbits.RB3 = 1;
// else PORTBbits.RB3 = 0;
// // go forward
// if( get_val_dir(maze_p,mouse_x(mouse_p),mouse_y(mouse_p),mouse_c_dir(mouse_p)) > 8 && get_val_dir(maze_p,mouse_x(mouse_p),mouse_y(mouse_p),mouse_c_dir(mouse_p)) <= 14) {
// track();
// go_f(mouse_p);
// }
// else {
// turn(R_TURN);
// go_turn(mouse_p, R_TURN);
// }
// // update maze
// set_walls_mouse(maze_p, mouse_p);
// }
}
else if(ALG == TREMAUX) {
while(1) {
// fill maze with zeros and a 1
tremaux_fill(maze_p);
// solve the maze!
while((mouse_x(mouse_p) > 8 || mouse_x(mouse_p) < 7) || (mouse_y(mouse_p) > 8 || mouse_y(mouse_p) < 7)) {
// go forward
track();
// update mouse
go_f(mouse_p);
// update maze
set_walls_mouse(maze_p, mouse_p);
// increment current cell value
tremaux_increment(maze_p, mouse_p);
// get next turn
// turn
// update mouse
if((mouse_x(mouse_p) > 8 || mouse_x(mouse_p) < 7) || (mouse_y(mouse_p) > 8 || mouse_y(mouse_p) < 7)) {
go_turn(mouse_p, turn(tremaux_decide(maze_p, mouse_p)));
}
else {
go_turn(mouse_p, turn(tremaux_return(maze_p, mouse_p)));
}
}
while(1) {
// go back!
PORTBbits.RB2 = 1;
// put down 10's
tremaux_increturn(maze_p, mouse_p);
while(mouse_x(mouse_p) != 0 || mouse_y(mouse_p) != 0) {
// go forward
track();
// update mouse
go_f(mouse_p);
// put down 10's
tremaux_increturn(maze_p, mouse_p);
// get next turn
// turn
// update mouse
go_turn(mouse_p, turn(tremaux_return(maze_p, mouse_p)));
}
// put down 10's
tremaux_increspeed(maze_p, mouse_p);
PORTBbits.RB2 = 0;
while((mouse_x(mouse_p) > 8 || mouse_x(mouse_p) < 7) || (mouse_y(mouse_p) > 8 || mouse_y(mouse_p) < 7)) {
// go forward
track();
// update mouse
go_f(mouse_p);
// put down 10's
tremaux_increspeed(maze_p, mouse_p);
// get next turn
// turn
// update mouse
if((mouse_x(mouse_p) > 8 || mouse_x(mouse_p) < 7) || (mouse_y(mouse_p) > 8 || mouse_y(mouse_p) < 7)) {
go_turn(mouse_p, turn(tremaux_speed(maze_p, mouse_p)));
}
else {
go_turn(mouse_p, turn(tremaux_return(maze_p, mouse_p)));
}
}
}
}
}
else if(ALG == FLOOD) { //floodfill
while(1) {
// solve
init_fill(maze_p);
while((mouse_x(mouse_p) > 8 || mouse_x(mouse_p) < 7) && (mouse_y(mouse_p) > 8 || mouse_y(mouse_p) < 7)) {
if (mouse_x(mouse_p) == 7 /*&& mouse_y(mouse_p) == 8*/) PORTBbits.RB2 = 1;
else PORTBbits.RB2 = 0;
if (/*mouse_x(mouse_p) == 9 &&*/ mouse_y(mouse_p) == 7) PORTBbits.RB3 = 1;
else PORTBbits.RB3 = 0;
// go forward
track();
// update mouse
go_f(mouse_p);
// update maze
set_walls_mouse(maze_p, mouse_p);
// flood maze
// get next turn
// turn
// update mouse
go_turn(mouse_p, turn(flood(maze_p, mouse_p, 7, 7)));
}
// return to start
back_fill(maze_p);
go_turn(mouse_p, turn(U_TURN));
while(mouse_x(mouse_p) != 0 || mouse_y(mouse_p) != 0) {
// go forward
track();
// update mouse
go_f(mouse_p);
// update maze
set_walls_mouse(maze_p, mouse_p);
// flood maze
// get next turn
// turn
// update mouse
go_turn(mouse_p, turn(flood(maze_p, mouse_p, 1, 1)));
}
}
}
// else {}
}