-
Notifications
You must be signed in to change notification settings - Fork 0
/
solitaire_s.c
357 lines (307 loc) · 7.88 KB
/
solitaire_s.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
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <strings.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "solitaire.h"
#include "cardstack.h"
#include "carddeck.h"
#define MAXLINE 80
#define MAXCONNECTIONS 1024
typedef struct _client_t {
int id;
int connections;
srtruct sockaddr_in address;
arena_t arena;
deck_t deck;
} client_t;
arena_t *newArena() {
arena_t *arena = (arena_t *)malloc(sizeof(arena_t));
for (int s = 0; s < 4; s++) {
arena->highest[s] = 0;
}
return arena;
}
deal_t *newDeal(deck_t *deck) {
deal_t *deal = (deal_t *)malloc(sizeof(deal_t));
// Make all the stacks.
deal->draw = new_cardstack(DRAW);
deal->discard = new_cardstack_fed_by(DISCARD,deal->draw);
for (int p = 0; p < 7; p++) {
deal->hidden[p] = new_cardstack(HIDDEN);
deal->lain[p] = new_cardstack_fed_by(LAIN,deal->hidden[p]);
}
// Build the draw pile.
for (int i = 0; i < 52; i++) {
push(&deck->cards[deck->order[i]],deal->draw);
}
// Fill each of the hidden piles.
for (int i = 0; i < 7; i++) {
// Place first cards, then second cards, then third...
for (int j = i; j < 7; j++) {
push(pop(deal->draw),deal->hidden[j]);
}
}
// Flip the top card of each pile.
for (int i = 0; i < 7; i++) {
feed(deal->lain[i]);
}
// Flip up the first card.
feed(deal->discard);
return deal;
}
void putArena(arena_t *arena) {
for (int s=0; s<4; s++) {
printf("[");
putCardOfCode(arena->highest[s],s);
printf("] ");
}
printf("\n");
}
void putDeal(deal_t *deal) {
for (int p = 6; p >= 0; p--) {
printf("[");
put_cardstack(deal->hidden[p],FALSE);
printf("]");
put_cardstack(deal->lain[p],TRUE);
printf("\n");
}
put_cardstack(deal->discard,TRUE);
printf("\n");
printf("[");
put_cardstack(deal->draw,FALSE);
printf("]\n");
}
int arenaTake(card_t *card, arena_t *arena) {
if (arena->highest[card->suit] == card->face-1) {
arena->highest[card->suit]++;
return SUCCESS;
} else {
return FAILURE;
}
}
int makeArenaPlay(card_t *card, arena_t *arena) {
cardstack_t *stack = card->stack;
if (isOnTop(card) && (arenaTake(card,arena) == SUCCESS)) {
pop(stack);
if (stack->type == LAIN && is_empty(stack) && !is_empty(stack->feed)) {
feed(stack);
}
return SUCCESS;
} else {
return FAILURE;
}
}
cardstack_t *findEmptyLainStack(deal_t *deal) {
for (int p = 0; p < 7; p++) {
if (is_empty(deal->lain[p])) {
return deal->lain[p];
}
}
return NULL;
}
int moveKingOntoFree(card_t *king, deal_t *deal) {
cardstack_t *dest = findEmptyLainStack(deal);
if (isUp(king) && dest != NULL) {
move_onto(king,dest);
return SUCCESS;
} else {
return FAILURE;
}
}
int moveCardOntoAnother(card_t *card, card_t *onto) {
cardstack_t *dest = onto->stack;
if ((dest->type == LAIN) && isUp(card) && isOnTop(onto) && isOkOn(card,onto)) {
move_onto(card,dest);
return SUCCESS;
} else {
return FAILURE;
}
}
int pullFromDrawPile(deal_t *deal) {
if (is_empty(deal->draw)) {
return FAILURE;
} else {
feed(deal->discard);
return SUCCESS;
}
}
void getNextPlay(int stream, play_t *play, deck_t *deck) {
static char buffer[MAXLINE];
static char cmd[MAXLINE];
static char from[MAXLINE];
static char onto[MAXLINE];
read(stream,buffer,MAXLINE);
sscanf(buffer,"%s",cmd);
if (cmd[0] == 'm') {
// it's a move onto a lain stack
sscanf(buffer,"%s %s",cmd,from);
play->from = cardOf(from,deck);
if (isKing(play->from)) {
play->type = KING_PLAY;
play->onto = NULL;
} else {
play->type = MOVE_PLAY;
sscanf(buffer,"%s %s %s",cmd,from,onto);
play->onto = cardOf(onto,deck);
}
} else if (cmd[0] == 'p') {
// it's a play onto the arena
play->type = ARENA_PLAY;
sscanf(buffer,"%s %s",cmd,from);
play->from = cardOf(from,deck);
play->onto = NULL;
} else if (cmd[0] == 'd') {
// it's a draw
play->type = DRAW_PLAY;
play->from = NULL;
play->onto = NULL;
}
}
void sendAck(int client, int ack) {
if (ack == SUCCESS) {
write(client,"SUCCESS",8);
} else {
write(client,"FAILURE",8);
}
}
void playSolitaire(void *client) {
// initialize
deal_t *deal = newDeal(deck);
play_t play;
int status = SUCCESS;
while (1) {
putArena(arena);
printf("\n");
putDeal(deal);
printf("\nEnter your play: ");
getNextPlay(client,&play,deck);
switch (play.type) {
case ARENA_PLAY:
// Play a card into the arena.
status = makeArenaPlay(play.from,arena);
break;
case KING_PLAY:
// Move a king card onto an empty lain stack.
status = moveKingOntoFree(play.from,deal);
break;
case MOVE_PLAY:
// Move a card (possibly with cards above it) onto some lain stack.
status = moveCardOntoAnother(play.from,play.onto);
break;
case DRAW_PLAY:
// Draws the next card and puts it on top of the discard pile.
status = pullFromDrawPile(deal);
break;
}
sendAck(client,status);
}
}
int acceptClientOn(int socket) {
socklen_t acceptlen = sizeof(struct sockaddr_in);
struct sockaddr_in clientaddr;
int connection;
if ((connection = accept(socket, (struct sockaddr *)&clientaddr, &acceptlen)) < 0) {
fprintf(stderr,"ACCEPT failed.\n");
exit(-1);
}
//
// Report the client that connected.
//
struct hostent *hostp;
if ((hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(struct in_addr), AF_INET)) == NULL) {
fprintf(stderr, "GETHOSTBYADDR failed.");
}
printf("Accepted connection from client %s (%s)\n",
hostp->h_name, inet_ntoa(clientaddr.sin_addr));
return connection;
}
int initConnection(int port) {
//
// Open a socket to listen for client connections.
//
int listenfd;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr,"SOCKET creation failed.\n");
exit(-1);
}
//
// Build the service's info into a (struct sockaddr_in).
//
struct sockaddr_in serveraddr;
bzero((char *) &serveraddr, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)port);
//
// Bind that socket to a port.
//
if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr)) < 0) {
fprintf(stderr,"BIND failed.\n");
exit(-1);
}
//
// Listen for client connections on that socket.
//
if (listen(listenfd, MAXCONNECTIONS) < 0) {
fprintf(stderr,"LISTEN failed.\n");
exit(-1);
}
fprintf(stderr,"Solitaire server listening on port %d...\n",port);
return listenfd;
}
void srand48(long);
double drand48(void);
int main(int argc, char **argv) {
//
// Make sure we've been given a port to listen on.
//
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
arena_t *arena = newArena();
int listener = initConnection(atoi(argv[1]));
int clients = 0
while (1) {
clients++;
//
// Build a client's profile to send to a handler thread
//
client_t *client = (client_t *)malloc(sizeof(client_t));
//
// Accept a connection from a client, get a file descriptor for communicating
// with the client
//
client->id = clients;
int client = acceptClientOn(listener);
}
//
// create a thread to handle the client
//
pthread_t tid;
pthread_create(&tid,NULL,playSolitaire, (void *)client);
unsigned long seed = 0;
if (argc == 1) {
struct timeval tp;
gettimeofday(&tp,NULL);
seed = tp.tv_sec;
} else {
seed = atol(argv[1]);
}
printf("Shuffling with seed %ld.\n",seed);
srand48(seed);
deck_t *deck = newDeck();
shuffle(deck);
playSolitaire(client);
}
}