-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsegment.cpp
More file actions
141 lines (129 loc) · 2.68 KB
/
segment.cpp
File metadata and controls
141 lines (129 loc) · 2.68 KB
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
/**
* Implementation of the Segment class.
*/
#include "segment.h"
#include <stdio.h>
/**
* Initialises the segment with the passed order, assuming alone in a list.
* Caller must also make calls to SetPrev/SetNext to integrate the segment
* in a snake (list)
*/
Segment::Segment(int o) {
prev = NULL;
next = NULL;
dir = RIGHT;
p_dir = RIGHT;
head = false;
x = 0;
y = 0;
p_x = -1;
p_y = 0;
order = o;
}
/**
* Initialises the segment with the passed order, as head, assuming alone in
* a list.
* Caller must also make calls to SetPrev/SetNext to integrate the segment
* in a snake (list)
*/
Segment::Segment(int o, bool h) {
prev = NULL;
next = NULL;
dir = RIGHT;
p_dir = RIGHT;
head = h;
x = 0;
y = 0;
p_x = -1;
p_y = 0;
order = o;
}
void Segment::SetPosition(int n_x, int n_y, Direction d) {
x = n_x;
y = n_y;
dir = d;
}
void Segment::SetPrev(Segment* p) {
prev = p;
}
void Segment::SetNext(Segment* n) {
next = n;
}
void Segment::SetHead(bool h) {
head = h;
}
void Segment::SetDirection(Direction d) {
if(head) {
dir = d;
}
}
void Segment::SetX(int n_x) {
x = n_x;
}
void Segment:: SetY(int n_y) {
y = n_y;
}
/**
* Moves the segment in the approproate direction.
* The only segment moved according to set direction is the head.
* All other segments are positioned in place of segments ahead of them.
*/
void Segment::Move() {
p_x = x;
p_y = y;
p_dir = dir;
if(head) {
// The head moves independently
switch(dir) {
case UP:
y -= 1;
break;
case RIGHT:
x += 1;
break;
case DOWN:
y += 1;
break;
case LEFT:
x -= 1;
break;
}
} else {
// Other segments move in place segments ahead of them
if(prev != NULL) {
x = prev->GetPX();
y = prev->GetPY();
dir = prev->GetPDirection();
} else {
return ;
}
}
}
void Segment::Delete() {}
int Segment::GetX() {
return x;
}
int Segment::GetY() {
return y;
}
int Segment::GetPX() {
return p_x;
}
int Segment::GetPY() {
return p_y;
}
unsigned int Segment::GetOrder() {
return order;
}
Direction Segment::GetDirection() {
return dir;
}
Direction Segment::GetPDirection() {
return p_dir;
}
Segment* Segment::GetNext() {
return next;
}
Segment* Segment::GetPrev() {
return prev;
}