-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmoves.c
155 lines (143 loc) · 3.89 KB
/
moves.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
//
// Created by flasque on 19/10/2024.
//
#include "moves.h"
/* prototypes of local functions */
/* local functions are used only in this file, as helper functions */
/**
* @brief Function to rotate the robot according to a move and its actual orientation
* @param ori : the actual orientation of the robot
* @param move : the move to do
* @return the new orientation of the robot
*/
t_orientation rotate(t_orientation, t_move );
/**
* @brief function to translate the robot according to a move and its actual position
* @param loc : the actual localisation of the robot
* @param move : the move to do
* @return the new localisation of the robot
*/
t_localisation translate(t_localisation , t_move);
/* definition of local functions */
t_orientation rotate(t_orientation ori, t_move move)
{
int rst;
switch (move)
{
case T_LEFT:
rst=3;
break;
case T_RIGHT:
rst=1;
break;
case U_TURN:
rst=2;
break;
default:
break;
}
return (ori+rst)%4;
}
t_localisation translate(t_localisation loc, t_move move)
{
/** rules for coordinates:
* - x grows to the right with step of +1
* - y grows to the bottom with step of +1
* - the origin (x=0, y=0) is at the top left corner
*/
t_position res = loc.pos;
switch (move) {
case F_10:
switch (loc.ori) {
case NORTH:
res.y = loc.pos.y - 1;
break;
case EAST:
res.x = loc.pos.x + 1;
break;
case SOUTH:
res.y = loc.pos.y + 1;
break;
case WEST:
res.x = loc.pos.x - 1;
break;
default:
break;
}
break;
case F_20:
switch (loc.ori) {
case NORTH:
res.y = loc.pos.y - 2;
break;
case EAST:
res.x = loc.pos.x + 2;
break;
case SOUTH:
res.y = loc.pos.y + 2;
break;
case WEST:
res.x = loc.pos.x - 2;
break;
default:
break;
}
break;
case F_30:
switch (loc.ori) {
case NORTH:
res.y = loc.pos.y - 3;
break;
case EAST:
res.x = loc.pos.x + 3;
break;
case SOUTH:
res.y = loc.pos.y + 3;
break;
case WEST:
res.x = loc.pos.x - 3;
break;
default:
break;
}
break;
case B_10:
switch (loc.ori) {
case NORTH:
res.y = loc.pos.y + 1;
break;
case EAST:
res.x = loc.pos.x - 1;
break;
case SOUTH:
res.y = loc.pos.y - 1;
break;
case WEST:
res.x = loc.pos.x + 1;
break;
default:
break;
}
break;
default:
break;
}
return loc_init(res.x, res.y, loc.ori);
}
/* definitions of exported functions */
char *getMoveAsString(t_move move)
{
return _moves[move];
}
t_localisation move(t_localisation loc, t_move move)
{
t_localisation new_loc;
new_loc.ori = rotate(loc.ori, move);
new_loc = translate(loc, move);
return new_loc;
}
void updateLocalisation(t_localisation *p_loc, t_move m)
{
*p_loc = move(*p_loc, m);
return;
}