-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonList.h
More file actions
318 lines (298 loc) · 10.5 KB
/
PokemonList.h
File metadata and controls
318 lines (298 loc) · 10.5 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// Created by sjwel on 12/3/2019.
//
#include<string>
#include "Pokemon.h"
#include "ArrayList.h"
#ifndef POKEMONCOMPBUILDER_POKEMONLIST_H
#define POKEMONCOMPBUILDER_POKEMONLIST_H
class PokemonList{
private:
//pointer to the start of the array
Pokemon **array;
//count of the number of valid items currently stored in the array
int currItemCount;
//size of the current array
int currCapacity;
/**
* replaces the old array with an array twice the size
* private method only called within ArrayList when necessary
* @post: array points to a new array of twice the size with values copied from the old one,
* the old array is deleted.
*/
void doubleCapacity(){
currCapacity *= 2;
Pokemon* *temp = array;
array = new Pokemon*[currCapacity];
for (int x = 0; x < currItemCount; x++) {
array[x] = temp[x];
}
delete[] temp;
}
void halfCapacity(){
currCapacity /= 2;
Pokemon* *temp = array;
array = new Pokemon*[currCapacity];
for (int x = 0; x < currItemCount; x++) {
array[x] = temp[x];
}
delete[] temp;
}
public:
/**
* Constructor
* @throws an std::invalid_argument exception if size < 1
*/
PokemonList(int initialCapacity){
if (initialCapacity < 1) {
throw std::invalid_argument("Initial Capacity has to be at least 1");
}
currItemCount = 0;
currCapacity = initialCapacity;
array = new Pokemon*[initialCapacity];
}
//Copy Constructor
PokemonList(const PokemonList& toCopy){
currCapacity = toCopy.currCapacity;
currItemCount = toCopy.currItemCount;
array = new Pokemon*[currCapacity];
for (int x = 0; x < currItemCount; x++) {
array[x] = toCopy.array[x];
}
}
//Overloaded Assignment Operator
PokemonList& operator=(const PokemonList& toCopy){
delete[] array;
currCapacity = toCopy.currCapacity;
currItemCount = toCopy.currItemCount;
array = new Pokemon*[currCapacity];
for (int x = 0; x < currItemCount; x++) {
array[x] = toCopy.array[x];
}
}
//Destructor
~PokemonList(){
delete[] array;
}
void addPokemon(Pokemon* itemToAdd){
if(currItemCount >= currCapacity){
doubleCapacity();
}
if(currItemCount <= 0){
array[0] = itemToAdd;
}
else{
bool sorted = false;
int iteration = 0;
while(!(sorted||iteration>=currItemCount)){
if(itemToAdd->getName() < array[iteration]->getName()){
for(int x = currItemCount; x > iteration; x--){
array[x] = array[x-1];
}
array[iteration] = itemToAdd;
sorted = true;
}
iteration++;
}
if(!sorted){
array[currItemCount] = itemToAdd;
}
}
currItemCount++;
}
/**
* gets a value from the list
* @param index the location from which to get the value
* @return a copy of the item at index
* @throws out_of_range exception if index is invalid
*/
Pokemon* getValueAt(int index){
if (index > currItemCount - 1 || index < 0) {
throw std::out_of_range("No Such Index");
}
return array[index];
}
/**
* checks if there are any valid items in the list
* @return true if there are no valid items in the list, false otherwise
*/
bool isEmpty(){
return currItemCount <= 0;
}
/**
* returns a count of valid items currently in the list
* @returns the number of valid items in the list
*/
int itemCount(){
return currItemCount;
}
/**
* makes the list empty of valid items
* @post the list is empty, such that isEmpty() == true
*/
void clearList(){
currItemCount = 0;
if (currCapacity > 50) {
delete[] array;
array = new Pokemon*[50];
}
}
/**
*
* @param type1 has to be a valid type
* @param type2 can be valid or null
* @return returns list of pokemon mathcing types. if type 2 is null the pokemon returned will have any second type
*/
ArrayList<Pokemon*>* subList(std::string type1, std::string type2){
std::string types[18] = {"normal", "fighting", "flying", "poison", "ground", "rock", "bug", "ghost", "steel", "fire", "water", "grass", "electric", "psychic", "ice", "dragon", "dark", "fairy"};
ArrayList<Pokemon*>* pokeList = new ArrayList<Pokemon*>(100);
bool validType = false;
for(int x = 0; x < 18; x++){
if(type1 == types[x]){
validType = true;
}
}
if(!validType){
throw std::invalid_argument("Invalid Type");
}
validType = false;
for(int x = 0; x < 18; x ++){
if(type2==types[x]||type2 == "null"){
validType=true;
}
}
if(!validType){
throw std::invalid_argument("Invalid Type");
}
if(validType&&type2!="null"){
for(int x = 0; x < currItemCount; x++){
if(array[x]->getType() == type1&&array[x]->getType2()==type2){
pokeList->insertAtEnd(array[x]);
}
}
}
else{
for(int x = 0; x < currItemCount; x++){
if(array[x]->getType() == type1){
pokeList->insertAtEnd(array[x]);
}
}
}
return pokeList;
}
/**
* Searches an int array for a certain value
* @return the index of the first occurrence of numToFind if it is present, otherwise returns -1
*/
int find(Pokemon* toFind){
for (int x = 0; x < currItemCount; x++) {
if (array[x] == toFind) {
return x;
}
}
return -1;
}
int find(std::string name){
return binFind(currItemCount, name, currItemCount/2);
}
int find(int pokedex){
for(int x = 0; x < currItemCount; x++){
if(array[x]->getPokedex() == pokedex){
return x;
}
}
return -1;
}
int binFind(int size, std::string name, int loc){
if(size == 1){
if(array[loc]->getName() == name){
return loc;
}
else{
return -1;
}
}
else if(size == 2){
if(array[loc]->getName() == name){
return loc;
}
else{
return binFind(size-1, name, loc-1);
}
}
else if(size > 2){
int size2 = size/2;
int size3 = (size-1)/2;
if(array[loc]->getName() == name){
return loc;
}
else if (array[loc]->getName() > name){
return binFind(size2, name, loc-((size2+1)/2));
}
else{
return binFind(size3, name, loc+((size3+2)/2));
}
}
else{
return -1;
}
}
/**
* removes the item at the end of the list, and returns a copy of that item
* @post the item at the end is removed from the list
* @return a copy of the item at the end
* @throws out_of_range exception if there is no item to remove
*/
Pokemon* removeValueAtEnd(){
if (currItemCount <= 0) {
throw std::out_of_range("No Item To Remove");
}
if (currItemCount < currCapacity / 4) {
this->halfCapacity();
}
currItemCount--;
return array[currItemCount];
}
/**
* removes the item at the front of the list, and returns a copy of that item
* @post the item at the front is removed from the list, everything else is shifted down one
* @return a copy of the item at index
* @throws out_of_range exception if there is no item to remove
*/
Pokemon* removeValueAtFront(){
if (currItemCount <= 0) {
throw std::out_of_range("No Item To Remove");
}
if (currItemCount < currCapacity / 4) {
this->halfCapacity();
}
Pokemon* returnVal = array[0];
for (int x = 0; x < currItemCount - 1; x++) {
array[x] = array[x + 1];
}
currItemCount--;
return returnVal;
}
/**
* removes the item at index from the list, and returns a copy of that item
* @param index the location from which to get the value
* @post the item at index is removed from the list, everything else is shifted down one
* @return a copy of the item at index
* @throws out_of_range exception if index is invalid
*/
Pokemon* removeValueAt(int index){
if (index > currItemCount - 1 || index < 0) {
throw std::out_of_range("No Such Index");
}
if (currItemCount < currCapacity / 4) {
this->halfCapacity();
}
Pokemon* returnVal = array[index];
for (int x = index; x < currItemCount - 1; x++) {
array[x] = array[x + 1];
}
currItemCount--;
return returnVal;
}
};
#endif //POKEMONCOMPBUILDER_POKEMONLIST_H