-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjPosArrayList.cpp
More file actions
140 lines (113 loc) · 2.4 KB
/
Copy pathobjPosArrayList.cpp
File metadata and controls
140 lines (113 loc) · 2.4 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
#include "objPosArrayList.h"
objPosArrayList::objPosArrayList()
{
listSize = 0;
arrayCapacity = ARRAY_MAX_CAP;
aList = new objPos[arrayCapacity];
}
objPosArrayList::~objPosArrayList()
{
delete[] aList;
//aList = nullptr;
}
int objPosArrayList::getSize() const
{
return listSize;
}
// Copy Assignment Operator
objPosArrayList& objPosArrayList::operator=(const objPosArrayList& copy)
{
int i;
// Self-assignment check
if (this == ©)
{
return *this;
}
// Clean up any existing resources
delete[] aList;
// Allocate new memory for the array
listSize = copy.listSize;
arrayCapacity = copy.arrayCapacity;
aList = new objPos[arrayCapacity];
// Copy the elements from the other object
for (i = 0; i < listSize; ++i)
{
aList[i] = copy.aList[i];
}
return *this;
}
//Adds a new head segment to the snake's body whenever the snake moves
void objPosArrayList::insertHead(objPos thisPos)
{
int i;
if (listSize < arrayCapacity)
{
listSize++;
for (i = listSize - 1; i > 0; i--)
{
aList[i] = aList[i - 1];
}
aList[0] = thisPos;
}
}
void objPosArrayList::insertTail(objPos thisPos)
{
if (listSize < arrayCapacity)
{
aList[listSize] = thisPos;
listSize++;
}
}
void objPosArrayList::insertTails(int num)
{
if (listSize < arrayCapacity)
{
listSize = listSize + num;
}
}
void objPosArrayList::removeHead()
{
int i;
if (listSize >= 1)
{
for (i = 0; i < listSize - 1; i++)
{
aList[i] = aList[i + 1];
}
listSize--;
}
}
void objPosArrayList::removeTail()
{
// Check this would not result in a negative list size
if (listSize >= 1)
{
aList[listSize - 1] = objPos();
listSize--;
}
}
objPos objPosArrayList::getHeadElement() const
{
return aList[0];
}
objPos objPosArrayList::getTailElement() const
{
return aList[listSize - 1];
}
objPos objPosArrayList::getElement(int index) const
{
return aList[index];
}
// Like removeHead but used if we need to specify an index
void objPosArrayList::removeElement(int index)
{
int i;
if (listSize >= 1 && index >= 0 && index < listSize)
{
for (i = index; i < listSize - 1; i++)
{
aList[i] = aList[i + 1];
}
listSize--;
}
}