-
Notifications
You must be signed in to change notification settings - Fork 0
/
FINAL.c
executable file
·616 lines (536 loc) · 14.5 KB
/
FINAL.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<time.h>
typedef struct card
{
int number;
char* colour;
int used;
struct card* next;
}CARD;
typedef struct player
{
CARD* handCard;
}PLAYER;
typedef struct stackOfCards
{
CARD* top;
}STACK;
void init_screen(STACK* discard, CARD* deck);
void printScreen(int playerNumber);
void clear(void);
void printCard(CARD card);
void putACard(PLAYER* player,STACK* discard,int number, char* colour,int comp);
void drawACard(PLAYER* player ,int cardNum, CARD* deck);
int drawCardNum(int number);
int specialFunctions(int number);
void makeUsed1(CARD*deck, int n);
void makeUsed0(CARD*deck);
CARD pop(CARD* deck);
void computerPlay(PLAYER* player, STACK* discard, CARD* deck,CARD stackTop);
void play(PLAYER* player, STACK* discard, CARD* deck,CARD stackTop);
void insertNode(PLAYER* player, int number, char* colour);
void deleteNode(PLAYER* player, int number, char* colour, int computer);
void create7(PLAYER* player, CARD* deck);
void printLinkedList(PLAYER* player);
void push(STACK* stack,int number, char* colour);
void swap (CARD *a, CARD *b);
void randomize ( CARD arr[], int n );
int main()
{
CARD deck[108] = {{ 0, "RED" },
{ 1, "RED" },
{ 1, "RED" },
{ 2, "RED" },
{ 2, "RED" },
{ 3, "RED" },
{ 3, "RED" },
{ 4, "RED" },
{ 4, "RED" },
{ 5, "RED" },
{ 5, "RED" },
{ 6, "RED" },
{ 6, "RED" },
{ 7, "RED" },
{ 7, "RED" },
{ 8, "RED" },
{ 8, "RED" },
{ 9, "RED" },
{ 9, "RED" },
{ 10, "RED" },
{ 10, "RED" },
{ 20, "RED" },
{ 20, "RED" },
{ 30, "RED" },
{ 30, "RED" },
{ 0, "BLUE" },
{ 1, "BLUE" },
{ 1, "BLUE" },
{ 2, "BLUE" },
{ 2, "BLUE" },
{ 3, "BLUE" },
{ 3, "BLUE" },
{ 4, "BLUE" },
{ 4, "BLUE" },
{ 5, "BLUE" },
{ 5, "BLUE" },
{ 6, "BLUE" },
{ 6, "BLUE" },
{ 7, "BLUE" },
{ 7, "BLUE" },
{ 8, "BLUE" },
{ 8, "BLUE" },
{ 9, "BLUE" },
{ 9, "BLUE" },
{ 10, "BLUE" },
{ 10, "BLUE" },
{ 20, "BLUE" },
{ 20, "BLUE" },
{ 30, "BLUE" },
{ 30, "BLUE" },
{ 0, "GREEN" },
{ 1, "GREEN" },
{ 1, "GREEN" },
{ 2, "GREEN" },
{ 2, "GREEN" },
{ 3, "GREEN" },
{ 3, "GREEN" },
{ 4, "GREEN" },
{ 4, "GREEN" },
{ 5, "GREEN" },
{ 5, "GREEN" },
{ 6, "GREEN" },
{ 6, "GREEN" },
{ 7, "GREEN" },
{ 7, "GREEN" },
{ 8, "GREEN" },
{ 8, "GREEN" },
{ 9, "GREEN" },
{ 9, "GREEN" },
{ 10, "GREEN" },
{ 10, "GREEN" },
{ 20, "GREEN" },
{ 20, "GREEN" },
{ 30, "GREEN" },
{ 30, "GREEN" },
{ 0, "YELLOW" },
{ 1, "YELLOW" },
{ 1, "YELLOW" },
{ 2, "YELLOW" },
{ 2, "YELLOW" },
{ 3, "YELLOW" },
{ 3, "YELLOW" },
{ 4, "YELLOW" },
{ 4, "YELLOW" },
{ 5, "YELLOW" },
{ 5, "YELLOW" },
{ 6, "YELLOW" },
{ 6, "YELLOW" },
{ 7, "YELLOW" },
{ 7, "YELLOW" },
{ 8, "YELLOW" },
{ 8, "YELLOW" },
{ 9, "YELLOW" },
{ 9, "YELLOW" },
{ 10, "YELLOW" },
{ 10, "YELLOW" },
{ 20, "YELLOW" },
{ 20, "YELLOW" },
{ 30, "YELLOW" },
{ 30, "YELLOW" },
{ 40, "WHITE" },
{ 40, "WHITE" },
{ 40, "WHITE" },
{ 40, "WHITE" },
{ 50, "WHITE" },
{ 50, "WHITE" },
{ 50, "WHITE" },
{ 50, "WHITE" }
};
printf("no of cards: %lu\n",(sizeof(deck)/sizeof(CARD)));
STACK discard;
randomize(deck,108);
init_screen(&discard,deck);
makeUsed0(deck);
PLAYER player1;
PLAYER player2;
player1.handCard=NULL;
player2.handCard=NULL;
create7(&player1,deck);
printf("\nPlayer 1\n");
printLinkedList(&player1);
printf("\n");
printf("\nPlayer 2\n");
create7(&player2,deck);
printLinkedList(&player2);
printf("\n\n");
int i=0;
while(player1.handCard!=NULL && player2.handCard!=NULL)
{
clear();
printf("\ntop of pile:\n");
printCard(*(discard.top));
printf("\n\nPlayer\n");
printLinkedList(&player1);
printf("\n\nComp\n");
printLinkedList(&player2);
printf("\n\n PLAYER\n");
if(getchar()=='\n')
{
play(&player1,&discard,deck,*(discard.top));
if(specialFunctions((discard.top)->number))
{
play(&player1,&discard,deck,*(discard.top));
}
}
printf("\n\n COMPUTER\n");
if(getchar()=='\n')
{
computerPlay(&player2,&discard,deck,*(discard.top));
if(specialFunctions((discard.top)->number))
{
computerPlay(&player2,&discard,deck,*(discard.top));
}
}
}
if(player1.handCard==NULL)
{
printf("\n ===>Player 1 won\n");
}
else if (player2.handCard==NULL) {
printf("\n ===>Computer won\n");
}
printf("\n");
printLinkedList(&player1);
printf("\n");
}
/*=========================================================================*/
//clears the screen hand cards to the two players, puts a card to discard pile from the main pile
void init_screen(STACK* discard, CARD* deck)
{
printf("game starts\n\n");
push(discard,deck[14].number,deck[14].colour);
printCard(deck[14]);
}
void printScreen(int playerNumber)
{
printf("PLAYER %d's TURN\n\n",playerNumber);
}
void clear(void)
{
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}
/*=========================================================================*/
//CARD FUNCTIONS
void printCard(CARD card)
{
printf("================\n");
printf("(%d , %s)\n",card.number, card.colour);
printf("================\n");
}
void putACard(PLAYER* player,STACK* discard,int number, char* colour,int comp)
{
deleteNode(player,number,colour,comp);
push(discard,number,colour);
}
void drawACard(PLAYER* player ,int cardNum, CARD* deck)
{
int i=0;
printf("card num: %d\n",cardNum);
for(i=0;i<cardNum;i++)
{
CARD temp = pop(deck);
printCard(temp);
insertNode(player,temp.number,temp.colour);
}
}
int drawCardNum(int number)
{
switch (number) {
case 30:
return 2;
case 50:
return 4;
default:
return 1;
}
}
int specialFunctions(int number)
{
switch(number)
{
case 10:
//reverse
printf("The REVERSE card was put!\n");
return 1;
//break;
case 20:
//skip
printf("Your opponent's turn has been skipped, play again!\n ");
return 1;
//break;
default:
return 0;
}
}
/*=========================================================================*/
//DECK FUNCTIONS
//used=1
void makeUsed1(CARD*deck, int n)
{
deck[n].used =1;
}
//used =0
void makeUsed0(CARD*deck)
{
int i;
for(i=0;i<108;i++)
{
deck[i].used=0;
}
}
CARD pop(CARD* deck)
{
static int i = 15;
deck[i].used=1;
i++;
return deck[i-1];
}
/*=========================================================================*/
/*Both play functions handle
*50: Draw four: the next player draws four cards and puts any card of any colour on the deck.
*40: Wild: the next player puts any card of any colour on the deck.
*30: Draw two: The next player draws 2 cards.
*20: Skip: The player plays again.
*10: Reverse: The player plays again.
*/
//Computer play function.
void computerPlay(PLAYER* player, STACK* discard, CARD* deck, CARD stackTop)
{
CARD* nodePtr= player->handCard;
if(drawCardNum(stackTop.number)!=1)
{
printf("\nCOMPUTER DRAWS %d CARD(s)",drawCardNum(stackTop.number));
drawACard(player,drawCardNum(stackTop.number),deck);
}
if(strcmp("WHITE",stackTop.colour)==0)
{
putACard(player,discard,nodePtr->number,nodePtr->colour,1);
}
while((nodePtr != NULL) && strcmp(nodePtr->colour,stackTop.colour)!=0 && nodePtr->number!=stackTop.number)
{
nodePtr = nodePtr->next;
}
printf("result of play function: \n");
if(nodePtr==NULL)
{
CARD* head =player->handCard;
while(head!=NULL && strcmp(head->colour,"WHITE"))
{
head=head->next;
}
if(head==NULL)
{
printf("\nCOMPUTER DRAWS %d CARD(s)",drawCardNum(stackTop.number));
drawACard(player,drawCardNum(stackTop.number),deck);
CARD* head=player->handCard;
if(head->number == stackTop.number || strcmp(head->colour,stackTop.colour)==0)
{
putACard(player,discard,head->number,head->colour,1);
}
else
{
return;
}
}
else
{
putACard(player,discard,head->number,head->colour,1);
}
}
else
{
putACard(player,discard,nodePtr->number,nodePtr->colour,1);
}
}
//Player play function.
void play(PLAYER* player, STACK* discard, CARD* deck, CARD stackTop)
{
CARD* nodePtr= player->handCard;
if(drawCardNum(stackTop.number)!=1)
{
printf("\nPLAYER DRAWS %d CARD(s)",drawCardNum(stackTop.number));
drawACard(player,drawCardNum(stackTop.number),deck);
}
if(strcmp("WHITE",stackTop.colour)==0)
{
putACard(player,discard,nodePtr->number,nodePtr->colour,0);
return;
}
while((nodePtr != NULL) && strcmp(nodePtr->colour,stackTop.colour)!=0 && nodePtr->number!=stackTop.number)
{
nodePtr = nodePtr->next;
}
printf("result of play function: \n");
if(nodePtr==NULL)
{
CARD* head =player->handCard;
while(head!=NULL && strcmp(head->colour,"WHITE"))
{
head=head->next;
}
if(head==NULL)
{
printf("\nPLAYER DRAWS %d CARD(s)",drawCardNum(stackTop.number));
drawACard(player,1,deck);
CARD* head=player->handCard;
if(head->number == stackTop.number || strcmp(head->colour,stackTop.colour)==0)
{
putACard(player,discard,head->number,head->colour,0);
}
else
{
return;
}
}
else{
putACard(player,discard,head->number,head->colour,0);
}
}
else
{
int number;
char colour[10];
fflush(stdin);
printf("Enter num: ");
scanf("%d",&number);
printf("Enter colour: ");
scanf("%s",colour);
putACard(player,discard,number,colour,0);
}
}
/*=========================================================================*/
//LINKED LIST
//deletes a node using the info given
void deleteNode(PLAYER* player, int number, char* colour, int computer)
{
CARD *current = player->handCard, *previous;
CARD *head = player->handCard;
if (head == NULL) {
printf("Error : Invalid node pointer !!!\n");
return;
}
if ((current->number == number) && strcmp(current->colour,colour)==0 )
{
CARD* t1 =current;
player->handCard = current->next;
free(t1);
return;
}
if(computer==1)
{
while ((current != NULL )&& ((current->number != number) && (strcmp(current->colour,colour))!=0))
{
printf("comp\n");
previous = current;
current = current->next;
}
}
else if(computer==0)
{ while ((current != NULL )&& (current->number != number||strcmp(current->colour,colour)))
{
previous = current;
current = current->next;
}
}
if (current == NULL){
printf("not found in Linked List\n");
return;
}
CARD* t =current;
previous->next = current->next;
free(t);
}
//adds a node with the given info to the end
void insertNode(PLAYER* player, int number, char* colour)
{
CARD* head = player->handCard;
CARD* newNode = (CARD*) malloc(sizeof(CARD));
newNode->number = number;
newNode->colour=(char*)malloc(100*sizeof(char));
strcpy(newNode->colour,colour);
newNode->next = head;
player->handCard = newNode;
}
//Allots 7 cards to each player at the start of the game.
void create7(PLAYER* player, CARD* deck)
{
int i,count=0;
for(i=0;count<7;i++)
{
if(deck[i].used==0)
{
insertNode(player,deck[i].number,deck[i].colour);
makeUsed1(deck,i);
count++;
}
}
}
//Prints all the cards of a particular player.
void printLinkedList(PLAYER* player)
{
CARD* nodePtr= player->handCard;
while (nodePtr != NULL)
{
printf("%d, %s", nodePtr->number, nodePtr->colour);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
{ printf("\n");}
}
}
/*=========================================================================*/
//Adds to the stack checks for duplicates. if duplicates are present it is invalid
void push(STACK* stack,int number, char colour[])
{
CARD* temp = stack->top;
CARD* newCard = (CARD*)malloc(sizeof(CARD));
newCard->number=number;
newCard->colour = (char*)malloc(100*sizeof(char));
strcpy(newCard->colour,colour);
newCard->used=1;
if(temp==NULL)
{
stack->top=newCard;
newCard->next=NULL;
}
else
{
newCard->next=stack->top;
stack->top=newCard;
}
printCard(*newCard);
printf("\n");
}
/*=========================================================================*/
//SHUFFLE FUNCTIONS
// A utility function to swap to integers
void swap (CARD *a, CARD *b)
{
CARD temp = *a;
*a = *b;
*b = temp;
}
// A function to generate a random permutation of arr[]
void randomize ( CARD arr[], int n )
{
int i;
srand ( time(NULL) );
for ( i = n-1; i > 0; i--)
{
int j = rand() % (i+1);
swap(&arr[i], &arr[j]);
}
}
/*=========================================================================*/