Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C code: 2 A Game of UNC You are to develop an interactive game of UNO between

ID: 3713598 • Letter: I

Question

In C code:

2 A Game of UNC You are to develop an interactive game of UNO between two players. The gameplay for UNO is described at https://www.unorules.com/. Your program should operate as follows. 2.1 Setup 1. UNO cards are represented as variables of the following type: typedef struct card_s f char suit [7]; int value; char action [15]; struct card s *pt; card; You are allowed to add attributes to this definition, but not to remove any. You can represent colors by using card suits. Red: hearts; Yellow: diamonds; Green: clubs; Blue: spades. The action field is used to denote the function of action cards. 2. The game is played using the following deck of cards1 12 34 567 89 OG4 234 567 89O4 12 34 567 89OG 12 34 5 67 8 90 Figure 1: The deck of UNO cards. The following action cards are included: Reverse If going swi beginning, the first player loses his/her turn forfeit his/her turn. g clockwise, switch to counterclockwise or vice versa. . Skip - When a player places this card, the next player has to skip their turn. If turned up at the Draw Two - When a person places this card, the next player will have to pick up two cards and by Dmitry Fomin https://commons.wikimedia.org/w/index.php?curid-29517498

Explanation / Answer

main.c

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

typedef struct card_s {
   char suit[7];
   int value;
   char action[15];
   struct card_s *pt;
}card;

void card_Create(card* thisNode, int value, char* suit, char* action, card* nextLoc) {
   thisNode->value = value;
   strcpy(thisNode->suit, suit);
   strcpy(thisNode->action, action);
   thisNode->pt = nextLoc;
   return;
}

void card_InsertAfter(card* thisNode, card* newNode) {
   card* tmpNext = NULL;

   tmpNext = thisNode->pt;
   thisNode->pt = newNode;
   newNode->pt = tmpNext;
   return;
}

card* card_GetNext(card* thisNode) {
   return thisNode->pt;
}

int cardsinhand(card* headnode) {
   card* temp = headnode;
   int count = 0;
   while (temp != NULL) {
       count = count + 1;
       temp = temp->pt;
   }
   return count;
}

void card_PrintNodeData(card* thisNode) {
   if (thisNode->value != 10) {
       printf("%d", thisNode->value);
   }
   if (thisNode->suit != "*") {
       printf("%s", thisNode->suit);
   }
   if (thisNode->action != "") {
       printf("%s ", thisNode->action);
   }
   return;
}

int rand_gen(int count) {
   double frac;
   frac = (double)rand() / ((double)RAND_MAX + 1);
   return floor(count* frac);
}

void swap(card* ptr, int i, int j) {
   card *ni = ptr, *nj = ptr;
   int tempvalue;
   char tempsuit[7];
   char tempaction[15];

   for (int o = 0; o < i; ++o) {
       ni = card_GetNext(ni);
   }
   for (int o = 0; o < j; ++o) {
       nj = card_GetNext(nj);
   }

   tempvalue = ni->value;
   ni->value = nj->value;
   nj->value = tempvalue;

   strcpy(tempsuit, ni->suit);
   strcpy(ni->suit, nj->suit);
   strcpy(nj->suit, tempsuit);

   strcpy(tempaction, ni->action);
   strcpy(ni->action, nj->action);
   strcpy(nj->action, tempaction);
}

void CreateDeckFromFile(card **head, card **tail, FILE* inFile) {
   card* temp;
   temp = *head;
   int value;
   char suit[7];
   char action[15];

   for (int i = 0; i < 108; ++i) {
       value = 10;
       strcpy(suit, "");
       strcpy(action, "");

       fscanf(inFile, "%d", &value);
       if (value != 10) {
           fscanf(inFile, "%s ", suit);
       }
       else {
           fscanf(inFile, "%s ", suit);
           fscanf(inFile, "%s ", action);
       }
       card_Create(temp, value, suit, action, NULL);
       card_InsertAfter(*tail, temp);
       *tail = temp;
       temp->pt = (card*)malloc(sizeof(card));
       temp = temp->pt;
   }
  
   (*tail)->pt = NULL;
}

void shuffle(card* head) {
   srand((int)time(NULL));
   for (int i = 0; i < 1000; ++i) {
       swap(head, rand_gen(108), rand_gen(108));
   }
}

void Deckprint(card* head) {
   card* temp = head;
   while (temp != NULL) {
       card_PrintNodeData(temp);
       temp = card_GetNext(temp);
   }
}

void Pileprint(card* head) {
   card* temp = head;
   int num = cardsinhand(head);
   if (num <= 5) {
       while (temp != NULL) {
           card_PrintNodeData(temp);
           temp = temp->pt;
       }
   }
   else {
       for (int i = 0; i < num - 5; ++i) {
           temp = temp->pt;
       }
       while (temp != NULL) {
           card_PrintNodeData(temp);
           temp = temp->pt;
       }
   }
}

void deal7cards(card** deckhead, card** playerhead) {
   card* temp = *deckhead;
   card* decktemp = *deckhead;
   card* playertemp = *playerhead;
   for (int i = 0; i<6; ++i) {
       *deckhead = (*deckhead)->pt;
   }
   decktemp = *deckhead;
   *deckhead = (*deckhead)->pt;
   decktemp->pt = NULL;
   if (*playerhead == NULL){
       *playerhead = temp;
   }
   else {
       while (playertemp->pt != NULL) {
           playertemp = card_GetNext(playertemp);
       }
       playertemp->pt = temp;
   }
}

void draw(card** deckhead, card** playerhead) {
   card* temp = *deckhead;
   card* decktemp = *deckhead;
   card* playertemp = *playerhead;
  
   *deckhead = (*deckhead)->pt;
   decktemp = *deckhead;
   temp->pt = NULL;
   if (*playerhead == NULL) {
       *playerhead = temp;
   }
   else {
       while (playertemp->pt != NULL) {
           playertemp = card_GetNext(playertemp);
       }
       playertemp->pt = temp;
   }
}

int main(void) {
   card* head = NULL;
   card* temp = NULL;
   card* tail = NULL;
   FILE* inFile = fopen("text1.txt", "r");

   head = (card*)malloc(sizeof(card));
   tail = (card*)malloc(sizeof(card));
   ///////////////////////////////////////////////////

   int shuffleornot;

   printf("Let's Play a Game of UNO ");
   printf("Press 1 to shuffle the UNO deck or 2 to load a deck from a file: ");
   scanf("%d", &shuffleornot);
  
   if (shuffleornot == 1) {
       CreateDeckFromFile(&head, &tail, inFile);
       shuffle(head);
       Deckprint(head);
   }
   else {
       CreateDeckFromFile(&head, &tail, inFile);
       Deckprint(head);
   }
/////////////////////////


   int playernum;

   printf(" Please enter the number of players (2-10): ");
   scanf("%d", &playernum);
   card **player = (card**)calloc(playernum, sizeof(card*));

   for (int i = 0; i < playernum; ++i) {
       deal7cards(&head, &player[i]);
       Deckprint(player[i]); // test
       printf(" ");
   }
/////////////////////////////////////////////////


   card *pile = (card*)calloc(0, sizeof(card)); // pile is the junk card list
   draw(&head, &pile);
   draw(&head, &pile);    // test
   Deckprint(pile);

   //int p = 0;
   //int thecard;

   //while (1 == 1) {
   //   printf("player %d hand: ", p + 1);
   //   Deckprint(player[p]);
   //   printf("press (1-%d) to play any card from your hand, or press zero to draw a card: ", cardsinhand(player[p]));
   //   scanf("%d", &thecard);
   //   if (p < playernum) {
   //       p = p + 1;
   //   }
   //   else if (p = playernum) {
   //       p = 0;
   //   }
   //   for (int i = 0; i < playernum; ++i) {
   //       if (player[i] == NULL) {
   //           printf("Player %d wins", i + 1);
   //           break;
   //       }
   //   }
   //  
   //}

   return 0;
}

text1.txt

0Red 1Red 2Red 3Red 4Red 5Red 6Red 7Red 8Red 9Red 10Red Skip 10Red Reverse 10Red Draw2 10* Wild
0Yellow 1Yellow 2Yellow 3Yellow 4Yellow 5Yellow 6Yellow 7Yellow 8Yellow 9Yellow 10Yellow Skip 10Yellow Reverse 10Yellow Draw2 10* Wild
0Green 1Green 2Green 3Green 4Green 5Green 6Green 7Green 8Green 9Green 10Green Skip 10Green Reverse 10Green Draw2 10* Wild
0Blue 1Blue 2Blue 3Blue 4Blue 5Blue 6Blue 7Blue 8Blue 9Blue 10Blue Skip 10Blue Reverse 10Blue Draw2 10* Wild
1Red 2Red 3Red 4Red 5Red 6Red 7Red 8Red 9Red 10Red Skip 10Red Reverse 10Red Draw2 10* WildDraw4
1Yellow 2Yellow 3Yellow 4Yellow 5Yellow 6Yellow 7Yellow 8Yellow 9Yellow 10Yellow Skip 10Yellow Reverse 10Yellow Draw2 10* WildDraw4
1Green 2Green 3Green 4Green 5Green 6Green 7Green 8Green 9Green 10Green Skip 10Green Reverse 10Green Draw2 10* WildDraw4
1Blue 2Blue 3Blue 4Blue 5Blue 6Blue 7Blue 8Blue 9Blue 10Blue Skip 10Blue Reverse 10Blue Draw2 10* WildDraw4