-
Notifications
You must be signed in to change notification settings - Fork 2
/
mouse.c
61 lines (56 loc) · 1.02 KB
/
mouse.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
// mouse.c
// functions for mouse
#include "direction.h"
#include "decide.h"
#include "mouse.h"
#include <stdlib.h>
// initialize the mouse
void init_mouse(Mouse * mouse) {
mouse->x_pos = 0;
mouse->y_pos = 0;
mouse->c_dir = N;
}
// go forward
void go_f(Mouse * mouse) {
switch(mouse->c_dir) {
case N:
mouse->y_pos++;
break;
case W:
mouse->x_pos--;
break;
case S:
mouse->y_pos--;
break;
case E:
mouse->x_pos++;
break;
default: break;
}
}
// turn left
void go_turn(Mouse * mouse, int turn) {
switch(turn) {
case L_TURN:
mouse->c_dir = ((mouse->c_dir) + 1) % 4; // left
break;
case R_TURN:
mouse->c_dir = ((mouse->c_dir) + 3) % 4; // right
break;
case U_TURN:
mouse->c_dir = ((mouse->c_dir) + 2) % 4; // uturn
break;
default:
break;
}
}
// get x,y,c_dir
int mouse_x(Mouse * mouse){
return mouse->x_pos;
}
int mouse_y(Mouse * mouse){
return mouse->y_pos;
}
int mouse_c_dir(Mouse * mouse){
return mouse->c_dir;
}