-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMazeSolving.c
172 lines (120 loc) · 3.62 KB
/
MazeSolving.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
//Not implemented; contains functions to instruct robot to move to a particular cell after maze has been mapped, using a flooding algorithm
#include "MazeSolving.h"
#include "Config.h"
#include "MazeManipulation.h"
#include "Error.h"
#include "Motor2.h"
unsigned int FloodMaze [MAZEY][MAZEX];
static unsigned int GetFloodHere ( void );
static unsigned int GetFlood ( unsigned char, unsigned char );
static void SetFloodHere ( unsigned int );
static void SetFlood( unsigned char, unsigned char, unsigned int );
static unsigned int GetFloodHere ( void )
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
return FloodMaze[MouseY][MouseX];
}
static unsigned int GetFlood ( unsigned char MazeX, unsigned char MazeY )
{
return FloodMaze[MazeY][MazeX];
}
static void SetFloodHere ( unsigned int FloodValue )
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
if(FloodValue > 0xFFF) ThrowError(1);
FloodMaze[MouseY][MouseX] = FloodValue;
}
static void SetFlood (unsigned char MazeX, unsigned char MazeY, unsigned int FloodValue )
{
if(FloodValue > 0xFFF) ThrowError(2);
FloodMaze[MazeY][MazeX] = FloodValue;
}
static void InitFlood ( void )
{//clear out old flooding and set all to zero
int x = 0;
int y = 0;
while (y < MAZEY)
{
while (x < MAZEX)
{
SetFlood(x,y,0);
x++;
}
y++;
x=0;
}
}
void DoMazeFlooding ( void )
{
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
int x = 0;
int y = 0;
unsigned int CurrentFlood = 1;
unsigned char Exits = 0;
InitFlood();
SetFlood(TARGETX,TARGETY,1); // set target cell flood value =1
while ( ! GetFlood( MouseX, MouseY ) ) // while mouse's cell is not flooded
{
x=0;
y=0;
while (y < MAZEY)//loop through all sells
{
while (x < MAZEX)
{
if ( GetFlood( x, y ) == CurrentFlood)
{
//SetUnflooded exits to CFV+1
Exits = GetExits( x, y );
if ( (Exits & NORTH) && ( ! GetFlood(x,(y-1)) ) ) SetFlood( x, (y-1), (CurrentFlood +1) );
if ( (Exits & EAST) && ( ! GetFlood((x+1),y) ) ) SetFlood( (x+1), y, (CurrentFlood +1) );
if ( (Exits & SOUTH) && ( ! GetFlood(x,(y+1)) ) ) SetFlood( x, (y+1), (CurrentFlood +1) );
if ( (Exits & WEST) && ( ! GetFlood((x-1),y) ) ) SetFlood( (x-1), y, (CurrentFlood +1) );
}
x++;
}
y++;
x=0;
}
CurrentFlood++;
if (CurrentFlood > 0xFFF) ThrowError(3);
}
}
unsigned char MoveFollowFloodedMaze ( void )
{
unsigned char MouseY = GetMouseY();
unsigned char MouseX = GetMouseX();
unsigned int n=0xFFFF;
unsigned int s=0xFFFF;
unsigned int e=0xFFFF;
unsigned int w=0xFFFF;
unsigned char Walls=GetWalls(MouseX,MouseY);
unsigned char Exits=( (~Walls)&(0xF) );
unsigned int ne=0; //smallest between n&e
unsigned int sw=0;//smallest between s&w
unsigned int nesw=0;//smallest between ne&sw
unsigned char move = 0; //dir to move
if (Exits & NORTH) n = ( (GetFlood(MouseX,MouseY-1)<<4) | NORTH);
if (Exits & EAST) e = ( (GetFlood(MouseX+1,MouseY)<<4) | EAST);
if (Exits & SOUTH) s = ( (GetFlood(MouseX,MouseY+1)<<4) | SOUTH);
if (Exits & WEST) w = ( (GetFlood(MouseX-1,MouseY)<<4) | WEST);
if ( (n==0xFFFF) && (e==0xFFFF) && (s==0xFFFF) && (w==0xFFFF) ) ThrowError(4);
ne = (n <= e ) ? n : e ;
sw = (s <= w ) ? s : w ;
nesw = (ne <= sw) ? ne : sw ;
move = (unsigned char)(nesw & 0xF);
return move;
}
void MoveFloodHalfCellCorrecting ( void )
{
DriveForwardsFull(HALFCELL,1); //Move half cell with corrections
}
unsigned char MouseArrived ( void )
{
unsigned char MouseY = GetMouseY();
unsigned char MouseX = GetMouseX();
if ( (MouseX == TARGETX) && (MouseY ==TARGETY) ) return 1;
else return 0;
}