-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
executable file
·123 lines (82 loc) · 2.36 KB
/
player.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
#include "player.h"
/*Initialises the player for placement*/
void initialisePlayer(Player * player, Position position, Direction direction)
{
player->position = position;
player->direction = direction;
player->moves = 0;
}
/*Checks player direction and turns it depending on the input*/
void turnDirection(Player * player, TurnDirection turnDirection)
{
if(turnDirection == TURN_LEFT){
if(player->direction == NORTH){
player->direction = WEST;
}
else if(player->direction == WEST){
player->direction = SOUTH;
}
else if(player->direction == SOUTH){
player->direction = EAST;
}
else if(player->direction == EAST){
player->direction = NORTH;
}
}
if(turnDirection == TURN_RIGHT){
if(player->direction == NORTH){
player->direction = EAST;
}
else if(player->direction == EAST){
player->direction = SOUTH;
}
else if(player->direction == SOUTH){
player->direction = WEST;
}
else if(player->direction == WEST){
player->direction = NORTH;
}
}
}
/*Determines the next cell depending on the player's direction*/
Position getNextForwardPosition(const Player * player)
{
Position position;
if(player->direction == NORTH){
position = player->position;
position.x --;
}
else if(player->direction == SOUTH){
position = player->position;
position.x ++;
}
else if(player->direction == WEST){
position = player->position;
position.y --;
}
else if(player->direction == EAST){
position = player->position;
position.y ++;
}
return position;
}
/*Updates player's position to the next position*/
void updatePosition(Player * player, Position position)
{
player->position = position;
}
/*Determines player's direction for the initialisation*/
void displayDirection(Direction direction) {
if (direction == NORTH) {
printf("%s|", DIRECTION_ARROW_OUTPUT_NORTH);
}
if (direction == WEST) {
printf("%s|", DIRECTION_ARROW_OUTPUT_WEST);
}
if (direction == SOUTH) {
printf("%s|", DIRECTION_ARROW_OUTPUT_SOUTH);
}
if (direction == EAST) {
printf("%s|", DIRECTION_ARROW_OUTPUT_EAST);
}
}