-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuno_machine_abstraite.h
163 lines (144 loc) · 3.25 KB
/
uno_machine_abstraite.h
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
#include <stdio.h>
#include <stdlib.h>
typedef struct card *pointer;
typedef struct Player *ptrPlayer;
struct card
{
char* color;
char* value;
pointer next;
};
struct Player
{
pointer hand;
ptrPlayer next;
ptrPlayer previous;
};
void allocate_card(pointer *head)
{
*head = (struct card*) malloc(sizeof(struct card));
}
void free_card(pointer *head){
free(*head);
}
pointer next_card(pointer head){
return(head->next);
}
char* data_card(pointer head){
return(head->color,head->value);
}
void aff_data(pointer p,char* c,char* v){
p->color=c;
p->value=v;
}
void aff_adr (pointer p,pointer q){
p->next=q;
}
int length(pointer head){
int length=0;
pointer iter=head;
if ( head==NULL){
return(0);
}
else{
while (iter != NULL)
{
length ++;
iter = next_card(iter);
}
}
return(length) ;
}
void allocate_player(ptrPlayer *head)
{
*head=(struct Player*) malloc(sizeof(struct Player));
}
ptrPlayer next_player(ptrPlayer head)
{
return(head->next);
}
void aff_hand(ptrPlayer p,pointer hand)
{
p->hand=hand;
}
void aff_next_player(ptrPlayer p,ptrPlayer q)
{
p->next=q;
}
void aff_prev_player(ptrPlayer p,ptrPlayer q)
{
p->previous=q;
}
void freePlayer(ptrPlayer *head)
{
free(*head);
}
int lengthP(ptrPlayer head){
int length=0;
ptrPlayer iter=head;
if ( head==NULL){
return(0);
}
else{
while (iter != NULL)
{
length ++;
iter = next_player(iter);
}
}
return(length) ;
}
void deleteCard(pointer* head, char* color, char* value) {
// If the head node itself holds the key to be deleted
if (*head != NULL && strcmp((*head)->color, color) == 0 && strcmp((*head)->value, value) == 0) {
pointer temp = *head;
*head = (*head)->next;
free(temp);
return;
}
// Search for the key to be deleted, keep track of the previous node
pointer temp = *head, prev;
while (temp != NULL && (strcmp(temp->color, color) != 0 || strcmp(temp->value, value) != 0)) {
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL) return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
pointer getLastCard(pointer discardPile) {
// If the discard pile is empty, there's no last card
if (discardPile == NULL) return NULL;
// Traverse the discard pile to the last card
pointer iter = discardPile;
while (iter->next != NULL) {
iter = iter->next;
}
// Return a pointer to the last card
return iter;
}
void freeCardList(pointer head) {
pointer temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
void freePlayerList(ptrPlayer head) {
ptrPlayer temp;
while (head != NULL) {
freeCardList(head->hand); // Free the player's hand of cards
temp = head;
head = head->next;
free(temp);
}
}
void print_cards(pointer head) {
while (head != NULL) {
printf(" %s %s ", head->color,head->value);
head = head->next;
}
}