-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.c
429 lines (342 loc) · 9.4 KB
/
Game.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
/* Name: Go Fish Game Engine
* Creators: Adam Douglas, James Wardman
* Date: 17-06-2016
*/
#include "Game.h"
static void removePair (Game g, link headCard);
link findPrev (list l, link target);
void bubbleSort (list l);
int listLength (list l);
void swap (link elt);
void append (list l, link val);
typedef struct _player {
list playerHand;
int pairs;
} player;
typedef struct _game {
list deck;
player playerArray[NUM_PLAYERS];
int whoseTurn;
int roundNumber;
} game;
static void moveToFront (list l, int nodeNum);
Game newGame (void) {
// Allocate an area in the heap to the game struct and assert that
// it does not return null
Game g = malloc (sizeof (struct _game));
assert (g != NULL);
// Set all of the lists to NULL
g->deck = malloc (sizeof (struct _list));
g->deck->head = NULL;
for (int i = PLAYER_1 - 1; i < PLAYER_4; i++) {
g->playerArray[i].playerHand = malloc (sizeof (struct _node));
g->playerArray[i].playerHand->head = NULL;
g->playerArray[i].pairs = 0;
}
// Set the variables to zero to start the game
g->whoseTurn = 1;
g->roundNumber = 0;
return g;
}
void disposeGame (Game g) {
free (g);
g = NULL;
assert (g == NULL);
}
void initialiseDeck (Game g) {
// Create a new node and point the deck list to it
assert (g->deck->head == NULL);
link curr = malloc (sizeof (struct _node));
g->deck->head = curr;
link prev;
// For loop to populate the deck list with all of the cards
for (int i = ACE; i <= KING; i++) {
for (int j = HEARTS; j <= SPADES; j++) {
curr->value = i;
curr->suit = j;
curr->next = malloc (sizeof (struct _node));
curr = curr->next;
}
}
// Find the previous node
prev = findPrev (g->deck, curr);
prev->next = NULL;
// Free the last node, which has no data in it
free (curr);
curr = NULL;
}
void shuffleDeck (Game g) {
int random;
// Move a card from a random position in the deck to the front
// This happens 1000 times
for (int i = 0; i < 1000; i++) {
random = rand() % 50 + 1;
moveToFront (g->deck, random);
}
}
static void moveToFront (list l, int nodeNum) {
link curr = l->head->next;
link prev = l->head;
// Move through the list up to the node specified
for (int i = 0; i < nodeNum-1; i++) {
prev = prev->next;
curr = curr->next;
}
// Move the node to the start of the list
prev->next = curr->next;
curr->next = l->head;
l->head = curr;
}
void dealDeck (Game g) {
g->whoseTurn = 1;
// Loop through dealing 5 cards to each player
while (g->whoseTurn <= 4) {
for (int i = 0; i < 5; i++) {
popOffDeck (g);
}
g->whoseTurn++;
}
g->whoseTurn = 1;
}
int isDeckEmpty (Game g) {
int result = FALSE;
// Check if the list is empty, if so setting result to true
if (g->deck->head == NULL) {
result = TRUE;
}
return result;
}
void popOffDeck (Game g) {
list l = g->deck;
link oldElement = l->head;
l->head = oldElement->next;
append (g->playerArray[g->whoseTurn-1].playerHand, oldElement);
}
void nextTurn (Game g) {
g->whoseTurn++;
if (g->whoseTurn > PLAYER_4) {
g->whoseTurn = PLAYER_1;
g->roundNumber++;
}
}
void printHand (Game g) {
list l = g->playerArray[g->whoseTurn - 1].playerHand;
link curr = l->head;
printf ("[%d]", curr->value);
curr = curr->next;
while (curr != NULL) {
printf (" [%d]", curr->value);
curr = curr->next;
}
printf ("\n\n");
}
int getWhoseTurn (Game g) {
return g->whoseTurn;
}
int getRoundNumber (Game g) {
return g->roundNumber;
}
int getPairs (Game g) {
return g->playerArray[g->whoseTurn - 1].pairs;
}
int checkOpponent (Game g, action a) {
link curr = g->playerArray[a.player-1].playerHand->head;
int result = FALSE;
while (curr != NULL) {
if (curr->value == a.card) {
result = TRUE;
}
curr = curr->next;
}
return result;
}
int checkPlayer (Game g, action a) {
link curr = g->playerArray[g->whoseTurn - 1].playerHand->head;
int result = FALSE;
while (curr != NULL) {
if (curr->value == a.card) {
result = TRUE;
}
curr = curr->next;
}
return result;
}
void findPairs (Game g) {
list hand = g->playerArray[g->whoseTurn-1].playerHand;
link card = hand->head;
//Holds the first matching card of a set
link headCard = hand->head;
//Used in the inner loop so as to not interfere with the flow
//of the outer loop
link pairCard = NULL;
int cardVal;
int same = TRUE;
int count = 0;
int looped;
bubbleSort (hand);
if (listLength (hand) >= 4) {
//Outer loop to find each set of 4
while (card->next != NULL) {
looped = FALSE;
headCard = card;
cardVal = card->value;
//Inner loop to check the next three values
pairCard = card;
while (same == TRUE && count < PAIR_SIZE &&
pairCard->next != NULL) {
looped = TRUE;
pairCard = pairCard->next;
if (cardVal == card->value) {
same = TRUE;
count++;
} else {
same = FALSE;
}
}
if (count == PAIR_SIZE) {
removePair (g, headCard);
}
if(looped) {
card = pairCard;
} else {
card = pairCard->next;
}
}
}
}
static void removePair (Game g, link headCard) {
link prev = findPrev (g->playerArray[g->whoseTurn-1].playerHand,
headCard);
link curr = g->playerArray[g->whoseTurn-1].playerHand->head;
link deadNode;
int count = 0;
int removed = FALSE;
if (prev == NULL) {
while (count <= PAIR_SIZE) {
deadNode = curr;
curr = curr->next;
free (deadNode);
count++;
}
g->playerArray[g->whoseTurn-1].playerHand->head = curr;
} else {
do {
if (curr == headCard) {
while (count <= PAIR_SIZE) {
deadNode = curr;
curr = curr->next;
free (deadNode);
count++;
}
removed = TRUE;
}
} while (!removed);
prev->next = curr;
}
}
link findPrev (list l, link target) {
link curr = l->head;
link prev = NULL;
int found = FALSE;
if (curr == NULL) {
} else {
while (curr != NULL && !found) {
if (curr == target) {
found = TRUE;
} else {
prev = curr;
curr = curr->next;
}
}
}
return prev;
}
void bubbleSort (list l) {
link current = l->head;
int listLen = listLength (l);
int count = 0;
while (count < listLen) {
current = l->head;
while (current->next != NULL) {
if (current->value > current->next->value) {
swap (current);
}
current = current->next;
}
count++;
}
}
int listLength (list l) {
link current = l->head;
int count = 0;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
void swap (link elt) {
int temp;
temp = elt->value;
elt->value = elt->next->value;
elt->next->value = temp;
}
void append (list l, link val) {
link curr = l->head;
if (curr != NULL) {
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = val;
} else {
l->head = val;
}
val->next = NULL;
}
void takeFromPlayer (Game g, action a) {
list player = g->playerArray[g->whoseTurn-1].playerHand;
list opponent = g->playerArray[a.player-1].playerHand;
link prev = NULL;
link curr = opponent->head;
link backup;
while (curr != NULL) {
if (curr->value == a.card) {
backup = curr->next;
append (player, curr);
//Store node for removal
curr = backup;
//Remove card and patch (relink) opponent's hand
if (prev == NULL) {
opponent->head = curr;
} else {
prev->next = curr;
}
} else {
prev = curr;
curr = curr->next;
}
}
}
void calculateWinner (Game g) {
int player = PLAYER_1;
int max = getPairs (g);
int winner = g->whoseTurn;
int originalWhoseTurn = g->whoseTurn;
g->whoseTurn = player;
while (player <= PLAYER_4) {
printf ("Player %d has %d pairs.\n", player, getPairs (g));
if (getPairs (g) > max) {
winner = g->whoseTurn;
max = getPairs (g);
}
player++;
g->whoseTurn = player;
}
g->whoseTurn = originalWhoseTurn;
printf ("\n\nGame Over \n\n\n");
printf ("Statistics:\n");
printf ("Rounds - %d\n", getRoundNumber (g));
printf ("Last Turn - %d\n", getWhoseTurn (g));
printf ("Winner - Player %d!\n", winner);
printf ("\nThanks for playing.\n");
}