-
Notifications
You must be signed in to change notification settings - Fork 2
/
ff_follower.cpp
200 lines (149 loc) · 2.88 KB
/
ff_follower.cpp
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
193
194
195
196
197
198
199
200
#include <stdio.h>
//int main(int init_pos, int goal,int ff)
#include <stdio.h>
void ff_funct(int ff[6][6])
{
int grid = 6;
int walls[grid][grid];
for(int i=0;i<grid;i++)
{
for (int j=0;j<grid;j++)
{
ff[i][j] = 0;
//walls[i][j] = 0;
}
}
int n = 1; //goal value
ff[2][2] = n; //setting location of goal
while (n < 12)
{
for(int i=0; i<grid;i++)
{
for (int j=0; j<grid;j++)
{
if ((ff[i][j]) == n)
{
if (i+1<grid) //CHECK SOUTH
{
if (ff[i+1][j] != 1 && ff[i+1][j] == 0 && (walls[i][j] & 2) == 0)
{
ff[i+1][j] = n+1;
}
}
if ((i-1)>-1) //CHECK NORTH
{
if (ff[i-1][j] != 1 && ff[i-1][j] == 0 && (walls[i][j] & 8) == 0)
{
ff[i-1][j] = n+1;
}
}
if ((j+1)<grid) //CHECK EAST
{
if (ff[i][j+1] != 1 && ff[i][j+1] == 0 && (walls[i][j] & 4)== 0)
{
ff[i][j+1] = n+1;
}
}
if ((j-1)>-1) //CHECK WEST
{
if (ff[i][j-1] != 1 && ff[i][j-1] == 0 && (walls[i][j] & 1) == 0)
{
ff[i][j-1] = n+1;
}
}
}
}
}
n++;
}
for(int i=0;i<grid;i++)
{
for (int j=0;j<grid;j++)
{
printf("%d\t",ff[i][j]);
}
printf("\n");
}
return;
}
int main(void)
{
int init_pos[2];
init_pos[0] = 1;
init_pos[1] = 1;
int goal[2];
goal[0] = 2;
goal[1] = 2;
int ff_arr[6][6];
ff_funct(ff_arr);
int x = init_pos[0];
int y = init_pos[1];
int ff_value[4];
int lowest = 12; //corresponds to "n"
int dir= 256; //out of the way value
int dir_arr[50];
for (int i=0;i<50;i++)
{
dir_arr[i] = 256; //set to high value
}
for (int i=0;i<4;i++)
{
ff_value[i] = 256; //ff_value initialized to high number
}
printf("starting %d %d\n",init_pos[0],init_pos[1]);
printf("goal %d %d\n",goal[0],goal[1]);
while (x != goal[0] || y != goal[1])
{
//populates ff_value
if (y+1 < 6) //CHECK SOUTH
{
ff_value[2] = ff_arr[x][y+1];
}
if (y-1 > -1) //CHECK NORTH
{
ff_value[0] = ff_arr[x][y-1];
}
if (x+1<6) //CHECK EAST
{
ff_value[1] = ff_arr[x+1][y];
}
if (x-1 > -1) //CHECK WEST
{
ff_value[3] = ff_arr[x-1][y];
}
//finds lowest value
for (int i=0;i<4;i++)
{
if (ff_value[i] < lowest)
{
lowest = ff_value[i];
dir= i; //this tells you where to move
}
}
lowest = 256;
//tells robot which ff_value to move
switch(dir)
{
case 0: //north
{y--;
break;
}
case 1: //east
{x++;
break;
}
case 2: //south
{y++;
break;
}
case 3: //west
{
x--;
break;
}
}
printf("direction %d\n",dir);
printf("pos %d %d\n",x,y);
}
printf("\n\nGOALREACHEDDDD\n\n");
}