-
Notifications
You must be signed in to change notification settings - Fork 0
/
shoot.c
548 lines (456 loc) · 13 KB
/
shoot.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "shoot.h"
static int number_of_teams;
static team_node_ptr teams;
static shoot_subgraph_ptr subgraphs;
// This ugly concept is necessary to achieve my ideal architecture and prevent
// a large useless global data structure from hanging around
static int _check_coach_name(coach_id_map_llist * first_coach, char * coach_name)
{
if (first_coach->coach_name == NULL)
{
first_coach->coach_name = coach_name;
first_coach->coach_id = 0;
first_coach->next = NULL;
return 0;
}
coach_id_map_llist * current_coach = first_coach;
do
{
if(!strcmp(current_coach->coach_name, coach_name))
{
return current_coach->coach_id;
}
} while (current_coach->next && (current_coach = current_coach->next));
// Never got a correct comparison so add a new coach to the end of the list
coach_id_map_llist * new_coach = malloc(sizeof(coach_id_map_llist));
current_coach->next = new_coach;
new_coach->coach_name = coach_name;
new_coach->coach_id = current_coach->coach_id + 1;
new_coach->next = NULL;
return new_coach->coach_id;
}
static void _free_coach_linked_list(coach_id_map_llist * first_coach)
{
coach_id_map_llist * next_coach = first_coach->next;
while(next_coach)
{
free(first_coach);
first_coach = next_coach;
next_coach = first_coach->next;
}
free(first_coach);
}
static int _get_team_level(char * team_level_name)
{
if(!strcmp("Elite", team_level_name))
return 1;
else if(!strcmp("Premier", team_level_name))
return 2;
else if(!strcmp("Excel", team_level_name))
return 3;
else if(!strcmp("Gold", team_level_name))
return 4;
else if(!strcmp("Silver", team_level_name))
return 5;
if(!strcmp("Red", team_level_name))
return 1;
else if(!strcmp("White", team_level_name))
return 2;
else if(!strcmp("Blue", team_level_name))
return 3;
fprintf(stderr, "No identifiable level for: %s\n", team_level_name);
return -1;
}
int read_entries(FILE * input_file)
{
char temp_team_name[50];
char temp_team_level[25];
char transfer_coach[50];
char transfer_coach_last[25];
int counter = 0;
char trash[25];
fscanf(input_file, "%s", trash);
// First count the number of entries
while((fscanf(input_file, "%s%s", temp_team_name, temp_team_level ) != EOF))
{
if(fscanf(input_file, "%s%s", transfer_coach, transfer_coach_last) == EOF)
{
printf("Error...something weird happened!\n");
exit(1);
}
++counter;
//strcat(transfer_coach, " ");
//strcat(transfer_coach, transfer_coach_last);
//printf("%s\t%s\n", temp_team_name, transfer_coach);
}
if(counter <= 0)
{
fprintf(stderr, "Error: counter is 0 or lower, incompatable file!\n");
exit(1);
}
// Rewind to the beginning of the file
rewind(input_file);
fscanf(input_file, "%s", trash);
//Dynamically allocate space for the number of teams
number_of_teams = counter;
//teams = malloc(number_of_teams * sizeof(team_node));
//teams = malloc(sizeof(team_node));
counter = 0;
coach_id_map_llist * first_coach = malloc(sizeof(coach_id_map_llist));
first_coach->coach_name = NULL;
while((fscanf(input_file, "%s%s", temp_team_name, temp_team_level) != EOF))
{
if(fscanf(input_file, "%s%s", transfer_coach, transfer_coach_last) == EOF)
{
printf("Error...something weird happened!\n");
exit(1);
}
strcat(transfer_coach, " ");
strcat(transfer_coach, transfer_coach_last);
team_node_ptr curr_team = teams;
while(curr_team && curr_team->next)
curr_team = curr_team->next;
if(!curr_team)
{
curr_team = malloc(sizeof(team_node));
if(!curr_team)
{
fprintf(stderr, "Error: could not allocate team node!\n");
exit(1);
}
teams = curr_team;
}
else
{
curr_team->next = malloc(sizeof(team_node));
if(!curr_team->next)
{
fprintf(stderr, "Error: could not allocate team node!\n");
exit(1);
}
curr_team = curr_team->next;
}
curr_team->team_age_group = strdup(temp_team_name);
curr_team->team_level_name = strdup(temp_team_level);
curr_team->coach_name = strdup(transfer_coach);
curr_team->team_id = counter;
curr_team->number_of_edges = 0;
curr_team->edges = NULL;
curr_team->team_level = _get_team_level(temp_team_level);
int ret_coach_id = _check_coach_name(first_coach, curr_team->coach_name);
curr_team->coach_id = ret_coach_id;
//Set up the next node
curr_team->next = NULL;
++counter;
}
//Cleaning up here...
_free_coach_linked_list(first_coach);
return 1;
}
int make_new_field_space(char * name, int number_of_teams, enum time_slot what_time, unsigned int which_days)
{
if(!create_field(name, number_of_teams, what_time, which_days))
{
fprintf(stderr, "Could not create field %s\n", name);
return 0;
}
return 1;
}
void print_team_node_info()
{
team_node_ptr curr_team = teams;
while(curr_team)
{
printf("Team Name: %s %s\n", curr_team->team_age_group, curr_team->team_level_name);
printf("Coach's Name: %s\n", curr_team->coach_name);
printf("Team ID: %d\n", curr_team->team_id);
printf("Team Level: %d\n", curr_team->team_level);
printf("Coach ID: %d\n", curr_team->coach_id);
printf("*****************************\n");
printf("Number of edges: %d\n", curr_team->number_of_edges);
for(int j = 0; j < curr_team->number_of_edges; ++j)
{
//We have to follow the pointers to get the right edge
shoot_edge_ptr curr_edge = curr_team->edges;
for(int k = 0; k < j; ++k)
{
curr_edge = curr_edge->next;
}
printf("Edge to: %d\t", curr_edge->team_id);
printf("Confirmed: ");
team_node_ptr conf_node = teams;
for(int z = 0; z < curr_edge->team_id; ++z)
conf_node = conf_node->next;
if(conf_node == curr_edge->other_node)
{
printf("Yes\n");
}
else
printf("No\n");
}
printf("*******************************\n\n");
curr_team = curr_team->next;
}
}
void print_graph_representation()
{
FILE * output = NULL;
output = fopen("the_graph.dot", "w");
if(!output)
{
fprintf(stderr, "Error: could not open the_graph.dot!\n");
return;
}
fprintf(output, "digraph graph_representation {\n");
fprintf(output, " edge [dir=none]\n");
team_node_ptr curr_node = teams;
while(curr_node)
{
if(curr_node->number_of_edges == 0)
{
fprintf(output, " %d\n", curr_node->team_id);
}
else
{
for(int j = curr_node->number_of_edges; j > 0; --j)
{
shoot_edge_ptr curr_edge = curr_node->edges;
int k = 1;
while(k < j)
{
curr_edge = curr_edge->next;
++k;
}
if(curr_edge->team_id > curr_node->team_id)
{
fprintf(output, " %d->%d", curr_node->team_id, curr_edge->team_id);
if(curr_edge->hard_edge)
fprintf(output, " [color=red]");
fprintf(output, "\n");
}
}
}
curr_node = curr_node->next;
}
fprintf(output, "}");
fclose(output);
}
void print_subgraph_info()
{
shoot_subgraph_ptr curr_subgraph = subgraphs;
int counter = 0;
while(curr_subgraph)
{
printf("Printing info for subgraph: %d\n", ++counter);
printf("Number of nodes: %d\n", curr_subgraph->number_of_nodes);
printf("Teams involved: ");
team_node_ptr curr_team = curr_subgraph->teams;
while(curr_team)
{
printf("%d ", curr_team->team_id);
curr_team = curr_team->next;
}
printf("\n\n");
curr_subgraph = curr_subgraph->next;
}
}
void print_field_info()
{
print_internal_field_info();
}
// Return 1 on success, 0 on failure. We are placeing on the source node.
static int _set_edge_on_node(int dest_offset, int source_offset, unsigned int hard_edge)
{
shoot_edge_ptr new_edge = malloc(sizeof(shoot_edge));
if(!new_edge)
{
fprintf(stderr, "Error: ran out of memory while allocating edge!\n");
return 0;
}
//Get the correct nodes
team_node_ptr source_node = teams;
for(int i = 0; i < source_offset; ++i)
source_node = source_node->next;
team_node_ptr dest_node = teams;
for(int i = 0; i < dest_offset; ++i)
dest_node = dest_node->next;
// We are setting the new_edge to the correct place in the if statements here
if(source_node->number_of_edges == 0)
{
source_node->edges = new_edge;
}
else
{
shoot_edge_ptr temp_edge = source_node->edges;
for(int j = 0; j < (source_node->number_of_edges - 1); ++j)
{
temp_edge = temp_edge->next;
}
if(temp_edge->next)
{
fprintf(stderr, "Error: set edge on node algorithm failing to find last edge!\n");
return 0;
}
temp_edge->next = new_edge;
}
source_node->number_of_edges++;
new_edge->team_id = dest_offset;
new_edge->other_node = dest_node;
new_edge->hard_edge = hard_edge;
new_edge->next = NULL;
return 1;
}
static team_node_ptr _find_earliest_coach(int coach_id)
{
team_node_ptr curr_team = teams;
for(int i = 0; i < coach_id; ++i)
curr_team = curr_team->next;
if(curr_team->coach_id == coach_id)
return curr_team;
while(curr_team->coach_id != coach_id)
{
curr_team = curr_team->next;
}
return curr_team;
}
// 0 on failure, 1 on success
static int _make_edges_by_coach()
{
int next_coach_id = 0;
team_node_ptr curr_team = teams;
while(curr_team)
{
if(curr_team->coach_id < next_coach_id)
{
team_node_ptr earliest_coach = _find_earliest_coach(curr_team->coach_id);
if(earliest_coach->number_of_edges > 0)
{
shoot_edge_ptr current_edge = earliest_coach->edges;
while(current_edge)
{
int which_node = current_edge->team_id;
if(!_set_edge_on_node(curr_team->team_id, which_node, 1))
return 0;
if(!_set_edge_on_node(which_node, curr_team->team_id, 1))
return 0;
current_edge = current_edge->next;
}
}
if(!_set_edge_on_node(curr_team->team_id, earliest_coach->team_id, 1))
return 0;
if(!_set_edge_on_node(earliest_coach->team_id, curr_team->team_id, 1))
return 0;
}
else
next_coach_id++;
curr_team = curr_team->next;
}
return 1;
}
// Right now we're going to take advantage of the fact that
// we know what the list format is going to look like. In the
// future we will probably have to add some hash map to make the
// library more flexible
static int _make_edges_by_level()
{
team_node_ptr curr_team = teams;
while(curr_team)
{
team_node_ptr next_team = curr_team->next;
while(next_team && (!(strncmp(next_team->team_age_group, curr_team->team_age_group, 3))))
{
if(( next_team->team_level - curr_team->team_level) > 2)
{
//We need to make sure their isn't already an edge between the two teams
int should_we_add = 1;
for(int j = curr_team->number_of_edges; j > 0; j--)
{
shoot_edge_ptr curr_edge = curr_team->edges;
int k = 1;
while(k != j)
{
curr_edge = curr_edge->next;
++k;
}
if(!curr_edge)
{
fprintf(stderr, "Error: current edge is invalid when trying to check for duplicates in level edging!\n");
return 0;
}
if(curr_edge->team_id == next_team->team_id)
{
should_we_add = 0;
break;
}
}
// End check
if(should_we_add)
{
if(!_set_edge_on_node(curr_team->team_id, next_team->team_id, 0))
return 0;
if(!_set_edge_on_node(next_team->team_id, curr_team->team_id, 0))
return 0;
}
}
next_team = next_team->next;
}
curr_team = curr_team->next;
}
return 1;
}
int make_shoot_graph()
{
if(!_make_edges_by_coach())
{
fprintf(stderr, "Error: makeing edges for coaches failed!\n");
exit(1);
}
if(!_make_edges_by_level())
{
fprintf(stderr, "Error: makeing edges by level failed!\n");
exit(1);
}
subgraphs = make_subgraphs(teams);
return 1;
}
// 1 on success, 0 on failure
void cleanup_graph()
{
team_node_ptr curr_team = teams;
while(curr_team)
{
// Free the edges
for (int j = curr_team->number_of_edges; j > 0; j--)
{
shoot_edge_ptr curr_edge = curr_team->edges;
for(int k = 1; k < j; ++k)
{
curr_edge = curr_edge->next;
}
free(curr_edge);
}
//Free the names
free(curr_team->team_age_group);
free(curr_team->team_level_name);
free(curr_team->coach_name);
team_node_ptr temp_ptr = curr_team;
curr_team = curr_team->next;
free(temp_ptr);
}
shoot_subgraph_ptr curr_graph = subgraphs;
while(curr_graph)
{
//Free the inner nodes
team_node_ptr temp_ptr = curr_graph->teams;
while(temp_ptr)
{
team_node_ptr temp_ptr2 = temp_ptr->next;
free(temp_ptr);
temp_ptr = temp_ptr2;
}
//Free the outer subgraph
shoot_subgraph_ptr temp = curr_graph->next;
free(curr_graph);
curr_graph = temp;
}
}