Question
Uno Card Game in C?? So I have to create the game of UNO in C. I currently have created the deck (inputing the 108
Uno Card Game in C??
So I have to create the game of UNO in C. I currently have created the deck (inputing the 108 card from a file), been able to shuffle the deck if wanted, and deal the top 7 cards to two players.
The next step that I am currently struggling on is how to create the discard pile (using a linked list) and being able to add cards to the top of the discard pile while deleting cards from anywhere in the players hand. Also need to know how to pick up the top card from the draw pile, and move it into the players hand if they cannot play a card.
Can someone please code the functions to create a discard pile and add and delete cards from certain linked lists
This is the code I have so far:
#include
#include
#include
#include
typedef struct card_s {
char suit [7];
int value;
char action[15];
struct card_s *pt;
} card;
struct card_s* CreateDeck(){
card *headp=NULL, *temp, *tail=NULL;
FILE *fp;
fp = fopen("Uno_Deck.txt", "r"); //open file wiht 108 uno cards
//This creates the linked list from the file
temp = (card *)malloc(sizeof (card));
while (fscanf(fp, "%s %d %s", temp->suit, &temp->value, temp->action) != EOF){
if (headp == NULL){
headp = temp;
}
else {
tail->pt = temp;
}
tail = temp;
tail->pt = NULL;
temp = (card *) malloc(sizeof (card));
}
fclose(fp); //Closes file
return headp;
}
void Print(card *headp){ //prints list
while (headp != NULL){
printf("%s %d %s ", headp->suit, headp->value, headp->action);
headp = headp->pt;
}
printf(" ");
}
struct card_s* ShuffleCard(struct card_s* deck){
struct card_s* crd;
int n,i,j,k;
struct card_s** arr; //We will store as array for shuffling
for (n=0, crd=deck;crd!=NULL;n++,crd = crd->pt){
arr = (struct card_s**)malloc (n * sizeof(struct card_s*)); //allocates memory
}
for (i=0, crd = deck; crd != NULL; i++, crd=crd->pt){
arr[i]=crd;
}
for (i=0; i<1000; i++){
for (j=0; j k=rand()%n; crd=arr[j]; arr[j]=arr[k]; arr[k]=crd; } } for (i=0; i arr[i]->pt = arr[i+1]; } arr[i]->pt=NULL; crd=arr[0]; return crd; } void Deal_Hands(struct card_s** deck, struct card_s** hand1, struct card_s** hand2, int n) { struct card_s *cur, *cur1, *cur2; //Stores current card in 3 lists int i; cur1 = NULL; cur2 = NULL; cur = *deck; for(i = 0; i < 2 * n && cur != NULL; i++, cur = cur->pt) { if(i % 2 == 0) { //Player 1's turn (even) if(cur1 == NULL) { //Refers to first element(card) in list *hand1 = cur; } else { cur1->pt = cur; } cur1 = cur; } else { //Player 2's turn (odd) if(cur2 == NULL) { //Refers to first element(card) in list *hand2 = cur; } else { cur2->pt = cur; } cur2 = cur; } } cur1->pt = cur2->pt = NULL; //Ends the lists with NULL *deck = cur; //Updates the deck witht the remaining elements(cards) } int main(void) { int choice; struct card_s *deck, *hand1, *hand2, *discard; srand(time(NULL)); //Random Number deck = CreateDeck(); 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", &choice); if (choice == 1){ deck = ShuffleCard(deck); //shuffles deck from file Print(deck); //prints shuffled deck } if (choice == 2){ //printf("The deck has been loaded in. "); Print(deck); //prints the deck from file } printf(" -------------------------- Playing UNO with 2 players -------------------------- "); Deal_Hands(&deck, &hand1, &hand2, 7);//two hands are dealt from the deck with 7 cards printf("Player 1's hand: "); Print(hand1); printf("Player 2's hand: "); Print(hand2); /* Next we have to create a discard pile from the first node of the rest of the hand after it is dealt. Then we print the discard pile. Then we ask player one to play a card. Check to see if color OR number match, and if it does, add it to the front of the discard pile linked list. If neither match, then say that card is unplayable and to pick a different card or press 0 to draw from deck. If either one of the players hands are empty then round is over. Take opponents total score of each card and award them to the winner. If score > 500 then player wins. */ free(deck); free(hand1); free(hand2); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started