-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
176 lines (155 loc) · 4.4 KB
/
common.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
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include "common.h"
int fd_unset_nonblocking(int fd, int *flags_ptr) {
int newflags = 0;
if (flags_ptr == NULL) {
flags_ptr = &newflags;
}
if ((*flags_ptr & O_NONBLOCK) == 0) {
if (fcntl(fd, F_SETFL, (*flags_ptr & ~O_NONBLOCK)) == -1) {
perror("fcntl unset nonblock");
return -1;
}
}
return 0;
}
int fd_set_nonblocking(int fd, int *flags_ptr) {
int newflags = 0;
if (flags_ptr == NULL) {
flags_ptr = &newflags;
}
*flags_ptr = fcntl(fd, F_GETFL);
if (*flags_ptr == -1) {
perror("fcntl get flags");
return -1;
}
if ((*flags_ptr & O_NONBLOCK) == 0) {
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl set nonblock");
return -1;
}
}
return 0;
}
int flush_instream(FILE *stream) {
assert(stream != NULL);
int fd = fileno(stream);
int res = 0;
int flags = 0;
int bytes_read = 0;
if (fd_set_nonblocking(fd, &flags) == -1) {
return -1;
}
while (bytes_read != -1)
bytes_read = fgetc(stream);
if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
perror("read() failed");
res = -1;
}
if (fd_unset_nonblocking(fd, &flags) == -1) {
return -1;
}
return res;
}
char *suit_to_string(enum Suits s) {
static char string[64] = "null";
switch (s) {
case DENARA:
strcpy(string, "Denara");
break;
case COPPE:
strcpy(string, "Coppe");
break;
case BASTONI:
strcpy(string, "Bastoni");
break;
case SPADE:
strcpy(string, "Spade");
break;
};
return string;
}
void initialize_deck(struct Card *cs) {
memset(cs, 0, 40 * sizeof(struct Card));
for (int i = 0; i < 40; i++) {
cs[i] = (struct Card){ .value = i % 10, .suit = i / 10 };
}
}
int calculate_pass_value(struct Card **thrown, int n_thrown) {
int pass_value = 0;
for (int i = 0; i < n_thrown; i++) {
switch (thrown[i]->value) {
case 1: case 2: case 7: case 8: case 9:
pass_value += 1;
break;
case 0:
pass_value += 3;
break;
}
}
return pass_value;
}
struct Card_node *find_valid_card(struct Card_node *head, int n_cards, int card_id, enum Suits pass_suit) {
if (card_id < 0) {
fprintf(stderr, "ERRO: Invalid card id recived.\n");
return NULL;
}
bool flying[4] = { true, true, true, true };
struct Card_node *cn = head;
for (int i = 0; i < n_cards; i++) {
assert(cn != NULL);
flying[cn->c->suit] = false;
cn = (struct Card_node *)cn->node.next;
}
cn = (struct Card_node *)head;
for (int i = 0; i < n_cards; i++) {
assert(cn != NULL);
if (i != card_id) {
cn = (struct Card_node *)cn->node.next;
continue;
}
if ((pass_suit == -1) || (cn->c->suit == pass_suit) || (flying[pass_suit] == true)) {
return cn;
}
printf("Can't play %d of %s:%d on ", cn->c->value + 1, suit_to_string(cn->c->suit), cn->c->suit);
printf("%s:%d\n", suit_to_string(pass_suit), pass_suit);
// TODO: take this out of the shared function
// char answ = 'a';
// printf("Would you like to play it anyways? [y/n]");
// while (true) {
// fflush(stdout);
// answ = fgetc(stdin);
// switch (answ) {
// case 'Y': case 'y':
// printf("Don't get caught!"); return cn;
// case 'N': case 'n':
// return NULL;
// default: printf("Invalid answer, retry: ");
// break;
// }
// }
// fprintf(stderr, "ERRO: Unplayable card id recived.\n");
return NULL;
}
fprintf(stderr, "ERRO: Could not find card with given id.\n");
return NULL;
}
// struct Card *find_card_in_deck(struct Card (*deck)[40], int v, enum Suits s) {
// }
/*
TODO:
-- general
- implement the bluffing feature
- make first player the admin
- have the server acknowledge the clients on connection
- implement team games
-- clientside
- redo printed interface (just a bit)
-- serverside
*/