Thread: Help with Game of UNO and Linked lists

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    1

    Help with Game of UNO and Linked lists

    I need help programming a game of UNO using linked lists. It is for a project. I got the initial deck setup correctly, shuffle everything, and deal the two hands to the two players.

    I don't know how to create a discard pile and a draw function. So I don't know how to take the start of one linked list and move that information to another. Like how would i take the top of the draw pile and move it to the discard pile to start that list. Then move any one of the players cards into the discard pile check to see if it matches correctly. Or draw a card if the user cannot play any card.


    I currently have it where I create the initial deck by reading a file containing the cards. The contains this text:
    Red 0 normal
    Red 1 normal
    Red 2 normal
    Red 3 normal
    Red 4 normal
    Red 5 normal
    Red 6 normal
    Red 7 normal
    Red 8 normal
    Red 9 normal
    Red 1 normal
    Red 2 normal
    Red 3 normal
    Red 4 normal
    Red 5 normal
    Red 6 normal
    Red 7 normal
    Red 8 normal
    Red 9 normal
    Yellow 0 normal
    Yellow 1 normal
    Yellow 2 normal
    Yellow 3 normal
    Yellow 4 normal
    Yellow 5 normal
    Yellow 6 normal
    Yellow 7 normal
    Yellow 8 normal
    Yellow 9 normal
    Yellow 1 normal
    Yellow 2 normal
    Yellow 3 normal
    Yellow 4 normal
    Yellow 5 normal
    Yellow 6 normal
    Yellow 7 normal
    Yellow 8 normal
    Yellow 9 normal
    Green 0 normal
    Green 1 normal
    Green 2 normal
    Green 3 normal
    Green 4 normal
    Green 5 normal
    Green 6 normal
    Green 7 normal
    Green 8 normal
    Green 9 normal
    Green 1 normal
    Green 2 normal
    Green 3 normal
    Green 4 normal
    Green 5 normal
    Green 6 normal
    Green 7 normal
    Green 8 normal
    Green 9 normal
    Blue 0 normal
    Blue 1 normal
    Blue 2 normal
    Blue 3 normal
    Blue 4 normal
    Blue 5 normal
    Blue 6 normal
    Blue 7 normal
    Blue 8 normal
    Blue 9 normal
    Blue 1 normal
    Blue 2 normal
    Blue 3 normal
    Blue 4 normal
    Blue 5 normal
    Blue 6 normal
    Blue 7 normal
    Blue 8 normal
    Blue 9 normal
    Red 20 skip
    Red 20 skip
    Yellow 20 skip
    Yellow 20 skip
    Green 20 skip
    Green 20 skip
    Blue 20 skip
    Blue 20 skip
    Red 20 reverse
    Red 20 reverse
    Yellow 20 reverse
    Yellow 20 reverse
    Green 20 reverse
    Green 20 reverse
    Blue 20 reverse
    Blue 20 reverse
    Red 20 draw-2
    Red 20 draw-2
    Yellow 20 draw-2
    Yellow 20 draw-2
    Green 20 draw-2
    Green 20 draw-2
    Blue 20 draw-2
    Blue 20 draw-2
    Red 50 wild
    Red 50 wild-draw-4
    Yellow 50 wild
    Yellow 50 wild-draw-4
    Green 50 wild
    Green 50 wild-draw-4
    Blue 50 wild
    Blue 50 wild-draw-4




    This is my code so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    
    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 \n", headp->suit, headp->value, headp->action);
            headp = headp->pt;
        }
        printf("\n");
    }
    
    
    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<n; j++){
                k=rand()%n;
                crd=arr[j];
                arr[j]=arr[k];
                arr[k]=crd;
            }
        }
        
        for (i=0; i<n-1;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)
    }
    
    
    void drawCard (struct card_s **pl, struct card_s** deck, struct card_s** discard){
        
    }
    
    
    void HandToDiscard (struct card_s** hand, struct card_s** discard){
    
    
    }
    int length (struct card_s *headp){  //this is to count the number of cards in the hand
        struct card_s *current;
        int count = 0;
        
        if (headp == NULL){
            return 0;
        }
        for (current = headp; current != NULL; current = current->pt) {
            count++;
        }
        return count;
    }
    
    
    
    
    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!\n");
        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.\n");
            Print(deck); //prints the deck from file
        }
        printf("\n--------------------------\nPlaying UNO with 2 players\n--------------------------\n\n");
        Deal_Hands(&deck, &hand1, &hand2, 7);//two hands are dealt from the deck with 7 cards
        
        printf("Player 1's hand: \n");
        Print(hand1);
        
        printf("Player 2's hand: \n");
        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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should create an API for your linked list.
    CreateNode
    DeleteNode
    AddNode
    RemoveNode

    So creating a list involves calling create and add.

    Moving a node from one list to another is remove and add.

    Cleanup at the end is remove and delete.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. War Card Game with linked lists and dynamic memory
    By jburn09 in forum C Programming
    Replies: 4
    Last Post: 04-24-2013, 01:06 AM
  2. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  3. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  4. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM

Tags for this Thread