-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.c
122 lines (105 loc) · 2.6 KB
/
collection.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "card.h"
#include "collection.h"
void collection_free(collection * c){
int i;
for(i = 0; i < c->size; i++){
card_free(c->vector[i]);
}
free(c);
}
collection * collection_create(char name[50], int id){
collection * c = (collection *) malloc(sizeof(collection));
c->id = id;
strcat(c->name, name);
c->size = 0;
c->vector = NULL;
return c;
}
void collection_fprint(collection * c, FILE * f){
int i;
fprintf(f, "Size: %d; Cards: {", c->size);
for(i = 0; i < c->size; i++){
card_fprint(c->vector[i], f);
}
fprintf(f, "}");
}
collection * collection_fread(int id, char name[50], FILE * f){
collection * c = (collection *) malloc(sizeof(collection));
int i;
c->id = id;
strcpy(c->name, name);
fscanf(f, "Size: %d; Cards: {", &(c->size));
if(c->size > 0){
c->vector = (card **) calloc(c->size, sizeof(card));
for(i = 0; i < c->size; i++){
c->vector[i] = card_fread(f);
}
} else{
c->vector = NULL;
}
fscanf(f, "%*c");
return c;
}
collection * collection_overdue(collection * c){
collection * d = (collection *) malloc(sizeof(collection));
int i, index = 0;
d->id = c->id;
strcpy(d->name, c->name);
d->vector = (card **) malloc(c->size * sizeof(card *));
for(i = 0; i < c->size; i++){
if(!verifyCooldown(c->vector[i])){
d->vector[index] = c->vector[i];
index++;
}
}
d->size = index;
return d;
}
void collection_print(collection * c){
int i;
for(i = 0; i < c->size; i++){
printf("%d\t\t%s\t\t%s\n", i + 1, c->vector[i]->front, c->vector[i]->back);
}
}
void collection_append(collection * c, card * d){
c->size += 1;
c->vector = (card **) realloc(c->vector, c->size * sizeof(card));
c->vector[c->size - 1] = d;
}
void collection_del_card(collection * c, int index){
if(index < 0 || index > c->size) return;
int i;
for(i = index; i < c->size; i++){
c->vector[index] = c->vector[index + 1];
}
c->size -= 1;
c->vector = (card **) realloc(c->vector, c->size * sizeof(card));
}
void collection_edit_card(collection * c, card * d, int index){
if(index < 0 || index > c->size) return;
card_edit(c->vector[index], d->front, d->back);
}
int generate_id(collection * c){
if(c->size == 0) return 0;
return c->vector[c->size - 1]->id + 1;
}
void collection_play(collection * c){
int i;
printf("Jogando a colecao %s\n", c->name);
for(i = 0; i < c->size; i++){
play_card(c->vector[i]);
}
printf("\nFim da colecao. Parabens!!\n");
}
int collection_count_overdue(collection * c){
int counter = 0, i;
for(i = 0; i < c->size; i++){
if(!verifyCooldown(c->vector[i])){
counter++;
}
}
return counter;
}