-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.h
More file actions
183 lines (152 loc) · 5.1 KB
/
ArrayList.h
File metadata and controls
183 lines (152 loc) · 5.1 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
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
#include <iostream>
using namespace std;
// Initial capacity of the array list
#define INITIAL_CAPACITY 10
// Structure to store basic player information in the array list
struct PlayerInfo {
int playerID; // Unique identifier for the player
char username[50]; // Player's username
int totalScore; // Player's total score
bool isLoggedIn; // Whether the player is currently logged in
// Default constructor
PlayerInfo() {
playerID = -1;
username[0] = '\0';
totalScore = 0;
isLoggedIn = false;
}
// Parameterized constructor
PlayerInfo(int id, const char* name, int score = 0, bool logged = false) {
playerID = id;
strncpy_s(username, name, 49);
username[49] = '\0'; // Ensure null termination
totalScore = score;
isLoggedIn = logged;
}
};
class ArrayList {
private:
PlayerInfo* array; // Dynamic array to store player info
int capacity; // Current capacity of the array
int size; // Current number of elements in the array
// Helper function to resize the array when needed
void resize(int newCapacity) {
// Create a new array with the new capacity
PlayerInfo* newArray = new PlayerInfo[newCapacity];
// Copy elements from the old array to the new array
for (int i = 0; i < size; i++) {
newArray[i] = array[i];
}
// Delete the old array
delete[] array;
// Update array pointer and capacity
array = newArray;
capacity = newCapacity;
}
public:
// Constructor
ArrayList() {
capacity = INITIAL_CAPACITY;
size = 0;
array = new PlayerInfo[capacity];
}
// Destructor
~ArrayList() {
delete[] array;
}
// Returns the current size of the array list
int getSize() {
return size;
}
// Returns true if the array list is empty
bool isEmpty() {
return size == 0;
}
// Add a player to the end of the array list
void add(const PlayerInfo& player) {
// If the array is full, resize it
if (size == capacity) {
resize(capacity * 2);
}
// Add the new player at the end of the array
array[size] = player;
size++;
}
// Add a player at a specific index in the array list
void add(int index, const PlayerInfo& player) {
// Check if the index is valid
if (index < 0 || index > size) {
cout << "Invalid index for insertion." << endl;
return;
}
// If the array is full, resize it
if (size == capacity) {
resize(capacity * 2);
}
// Shift elements to make space for the new player
for (int i = size; i > index; i--) {
array[i] = array[i - 1];
}
// Add the new player at the specified index
array[index] = player;
size++;
}
// Get a player at a specific index in the array list
PlayerInfo get(int index) {
// Check if the index is valid
if (index < 0 || index >= size) {
cout << "Invalid index for retrieval." << endl;
return PlayerInfo();
}
return array[index];
}
// Update a player at a specific index in the array list
void set(int index, const PlayerInfo& player) {
// Check if the index is valid
if (index < 0 || index >= size) {
cout << "Invalid index for update." << endl;
return;
}
array[index] = player;
}
// Remove a player at a specific index from the array list
void remove(int index) {
// Check if the index is valid
if (index < 0 || index >= size) {
cout << "Invalid index for removal." << endl;
return;
}
// Shift elements to fill the gap
for (int i = index; i < size - 1; i++) {
array[i] = array[i + 1];
}
size--;
// If the array is too empty, resize it to save space
if (size > 0 && size == capacity / 4) {
resize(capacity / 2);
}
}
// Find a player by username
int findByUsername(const char* username) {
for (int i = 0; i < size; i++) {
// Compare usernames using string comparison
if (strcmp(array[i].username, username) == 0) {
return i; // Return the index if found
}
}
return -1; // Return -1 if not found
}
// Display all players (for debugging)
void display() {
cout << "Player List:" << endl;
for (int i = 0; i < size; i++) {
cout << "ID: " << array[i].playerID
<< ", Username: " << array[i].username
<< ", Score: " << array[i].totalScore
<< ", Logged In: " << (array[i].isLoggedIn ? "Yes" : "No") << endl;
}
}
};
#endif // ARRAYLIST_H