-
Notifications
You must be signed in to change notification settings - Fork 0
/
hollow.c
221 lines (165 loc) · 6.71 KB
/
hollow.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
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
#include "escape.h"
#include <stdarg.h>
/*
* Function: initHollow
* ---------------------
* Initializes 2D grid with participant avatars in current positions.
*
* escape: Pointer to the EscapeType structure.
* hollow: 2D array representing the hollow grid.
*/
void initHollow(EscapeType* escape, char hollow[MAX_ROW][MAX_COL]) {
for (int i = 0; i < MAX_ROW; ++i) {
for (int j = 0; j < MAX_COL; ++j) {
hollow[i][j] = ' ';
}
}
for (int i = 0; i < escape->heroes.size; ++i) {
PositionType pos = escape->heroes.elements[i]->partInfo.pos;
if (pos.row >= 0 && pos.row < MAX_ROW && pos.col >= 0 && pos.col < MAX_COL){
hollow[pos.row][pos.col] = escape->heroes.elements[i]->partInfo.avatar;
}
}
for (int i = 0; i < escape->flyers.size; ++i) {
PositionType pos = escape->flyers.elements[i]->partInfo.pos;
if (pos.row >= 0 && pos.row < MAX_ROW && pos.col >= 0 && pos.col < MAX_COL) {
hollow[pos.row][pos.col] = escape->flyers.elements[i]->partInfo.avatar;
}
}
for (int i = 0; i < MAX_ROW; ++i) {
hollow[i][0] = '|';
hollow[i][MAX_COL - 1] = '|';
}
for (int j = 0; j < MAX_COL; ++j) {
hollow[0][j] = '-';
hollow[MAX_ROW - 1][j] = '-';
}
hollow[0][0] = '+';
hollow[0][MAX_COL - 1] = '+';
hollow[MAX_ROW - 1][0] = '+';
hollow[MAX_ROW - 1][MAX_COL - 1] = '+';
hollow[MAX_ROW-2][0] = '=';
hollow[MAX_ROW-2][MAX_COL-1] = '=';
}
/*
* Function: serializeHollow
* --------------------------
* Constructs a 1D array with formatted Hollow output, containing all avatars
* in their positions, with borders, spacing, and newlines.
*
* escape: Pointer to the EscapeType structure.
* serializedHollow: Pointer to the character array where serialized hollow will be stored.
*/
void serializeHollow(EscapeType* escape, char* serializedHollow) {
char hollow[MAX_ROW][MAX_COL];
initHollow(escape, hollow);
int index = 0;
for (int i = 0; i < MAX_ROW; ++i) {
for (int j = 0; j < MAX_COL; ++j) {
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = hollow[i][j];
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
}
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = '\n';
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
}
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = '\n';
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
if (escape->heroes.size > 0) {
HeroType* firstHero = escape->heroes.elements[0];
int requiredSize = snprintf(NULL, 0, "%s:%d", firstHero->name, firstHero->health) + 1;
if (index + requiredSize < MAX_BUFF) {
char* nameAndHealth = (char*)malloc(requiredSize);
if (nameAndHealth == NULL) {
fprintf(stderr, "Error: Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
snprintf(nameAndHealth, requiredSize, "%s:%d", firstHero->name, firstHero->health);
for (int k = 0; k < requiredSize - 1; ++k) {
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = nameAndHealth[k];
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
}
free(nameAndHealth);
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = '\n';
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
}
if (escape->heroes.size > 1) {
HeroType* secondHero = escape->heroes.elements[1];
int requiredSize = snprintf(NULL, 0, "%s:%d", secondHero->name, secondHero->health) + 1;
if (index + requiredSize < MAX_BUFF) {
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = ' ';
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
char* nameAndHealth = (char*)malloc(requiredSize);
if (nameAndHealth == NULL) {
fprintf(stderr, "Error: Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
snprintf(nameAndHealth, requiredSize, "%s:%d", secondHero->name, secondHero->health);
for (int k = 0; k < requiredSize - 1; ++k) {
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = nameAndHealth[k];
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
}
free(nameAndHealth);
if (index < MAX_BUFF - 1) {
serializedHollow[index++] = '\n';
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for serialization.\n");
exit(EXIT_FAILURE);
}
}
if (index < MAX_BUFF) {
serializedHollow[index] = '\0';
} else {
fprintf(stderr, "Error: Insufficient space in the buffer for null-termination.\n");
exit(EXIT_FAILURE);
}
}
/*
* Function: setPos
* ----------------
* Sets a participant's position to given row/col, with values reset
* to the closest edge if outside of range.
*
* pos: Pointer to the PositionType structure.
* row: New row position.
* col: New column position.
*/
void setPos(PositionType* position, int row, int col) {
position->row = (row < 0) ? 0 : (row >= MAX_ROW) ? MAX_ROW - 1 : row;
position->col = (col < 0) ? 0 : (col >= MAX_COL) ? MAX_COL - 1 : col;
}