Crazy 8s Card Game

This is a discussion on Crazy 8s Card Game within the C Programming forums, part of the General Programming Boards category; Hello. I am trying to program crazy eights card game. At the moment It will correctly allow me to play ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Crazy 8s Card Game

    Hello. I am trying to program crazy eights card game. At the moment It will correctly allow me to play or draw a card but I can not get the computer to take its turn correctly. It should not display what cards he has. only the number. I thought the process would be similar to my own turn but I can't make it work. Any help is greatly appreciated.

    Code:
    /*Dan McGrail
     *Project 2
     *This program plays a game of Crazy 8's pitting a user against a computer. */
    
    
    #include <stdio.h>
    
    
    int shuffle(int [], int size);
    void PrintCard(int card);
    void PrintValue(int card);
    void PrintSuit(int card);
    int IsValidPlay(int cardPlayed, int inPlayCard);
    int SameValue(int card1, int card2);
    int SameSuit(int card1, int card2);
    void GetNewCard(int card);
    
    
    
    
    int main(){
        char choice, decide, end, middle;
        int player_hand[52], i, play, in_play;
        int comp_hand[52];
        int comp_cards = 0;
        int deck[52];
        int const deckSize = 52;
        int comp_hand_size = 7;
        int player_hand_size = 7;
        int draw_card = 0;
        int player_wins = 0;
        int comp_wins = 0;
        int playerturn = 0;
        FILE *inFile;
        FILE *inFile2;
    
    
        inFile = fopen("project2.txt","r");
        fscanf(inFile,"%d %d",&player_wins,&comp_wins);
        fclose(inFile);
        //while(choice != 'e' && choice != 'E'){
            printf("You and the computer will be dealt 7 cards each.\n");
            printf("The next card in the deck becomes the in-play card.\n");
            printf("You can play either a card of the same number or suit as the in-play card.\n");
            printf("You may however play an 8 at any time and choose the suit.\n");
            printf("The first player to use all the cards in his hand wins the round.\n");
            printf("\n");
            while(choice != 'P' && choice!='p' && choice !='e' && choice != 'E' &&choice!= 'L' && choice!='l'){
                printf("Enter P to play, E to exit, or L to load saved game: ");
                scanf(" %c",&choice);
            }
            if(choice == 'E' || choice == 'e'){
                printf("Thanks for playing!\n");
                //break;
            }
            else if(choice == 'p' || choice == 'P'){
                printf("Begin Game.\n\n");
            }
            else if(choice == 'l' || choice == 'L'){
                inFile2 = fopen("middle.txt","r");
                fscanf(inFile2,"%d %d %d %d",&player_hand_size,&comp_hand_size,&in_play,&deck);
                fclose(inFile2);
            }
            int card;
            shuffle(deck, deckSize);
            printf("The in-play card is: ");
            PrintCard(deck[14]);
            in_play = 14;
            printf("Your hand:\n");
            PrintCard(deck[0]);
            PrintCard(deck[2]);
            PrintCard(deck[4]);
            PrintCard(deck[6]);
            PrintCard(deck[8]);
            PrintCard(deck[10]);
            PrintCard(deck[12]);
            player_hand[0] = deck[0];
            player_hand[1] = deck[2];
            player_hand[2] = deck[4];
            player_hand[3] = deck[6];
            player_hand[4] = deck[8];
            player_hand[5] = deck[10];
            player_hand[6] = deck[12];
    
    
            comp_hand[0] = deck[1];
            comp_hand[1] = deck[3];
            comp_hand[2] = deck[5];
            comp_hand[3] = deck[7];
            comp_hand[5] = deck[9];
            comp_hand[7] = deck[11];
            comp_hand[9] = deck[13];
            printf("\n");
            for(i=0;i<comp_hand_size;++i){
                comp_cards++;
            }
            draw_card = deck[15];
            printf("The computer has %d cards.\n\n",comp_cards);
            while(middle!='S'&&middle!='s'&&middle!='P'&&middle!='p'){
                printf("Would you like to save and exit (S) or play (P)?: ");
                scanf(" %c",&middle);
            }
            if(middle == 'S' || middle == 's'){
                inFile2 = fopen("middle.txt","w");
                fprintf(inFile2,"%d %d %d %d",player_hand_size,comp_hand_size,in_play,deck);
                fclose(inFile2);
            }
            else if(middle == 'p' || middle == 'P'){
            }
            while(player_hand_size!= 0 || comp_hand_size!= 0){
            if(playerturn == 0){
            printf("Which card would you like to play? (Enter 0 to draw card): ");
            scanf("%d",&play);
            int a=15;
            int b=7;
            if(play==0){
                do{
                    printf("You drew: ");
                    PrintCard(deck[a]);
                    player_hand[b] = deck[a];
                    ++a;
                    ++b;
                    player_hand_size++;
                    printf("Which card would you like to play?: ");
                    scanf("%d",&play);
                }
                    while(IsValidPlay(player_hand[play-1],deck[in_play]) != 1);
            }
            PrintCard(player_hand[play-1]);
            if(IsValidPlay(player_hand[play-1],deck[in_play]) != 1){
                while(IsValidPlay(player_hand[play-1],deck[in_play]) != 1){
                    printf("You can't play that card.\n");
                    printf("Which card would you like to play?: ");
                    scanf("%d",&play);
                }
            }
            if(IsValidPlay(player_hand[play-1],deck[in_play]) == 1){
                in_play = play-1;
                printf("The in-play card is now ");
                PrintCard(player_hand[play-1]);
            }
            printf("Your hand now contains:\n");
            for(i=0;i<player_hand_size;++i){
                if(i != (play-1)){
                    PrintCard(player_hand[i]);
                }
            }
            printf("\n");
            playerturn = 1;
            }
            else{
            printf("--Computer turn--\n");
    
    
            //computer turn goes here.
            int b = comp_hand_size;
            int a = in_play;
            for(i=0;i<comp_hand_size;++i){
            if(IsValidPlay(comp_hand[i],deck[in_play]) != 1){
                do{
                    comp_hand[b] = deck[a];
                    ++a;
                    ++b;
                    //draw card for computer somehow
                    comp_hand_size++;
                }
                while(IsValidPlay(comp_hand[play-1],deck[in_play]) != 1);
            }
            }
            if(IsValidPlay(comp_hand[i],deck[in_play]) == 1){
                for(i=0;i<comp_hand_size;++i){
                    if(i == in_play){
                        PrintCard(comp_hand[in_play]);
                        comp_hand_size--;
                    }
                }
            }
            printf("The computer now has %d cards",comp_hand_size);
            playerturn = 1;
            }
            }
            if(player_hand_size==0){
                printf("You won!");
                player_wins++;
            }
            else if(comp_hand==0){
                printf("The computer won.");
                comp_wins++;
            }
            printf("\n");
            printf("You have %d wins.\n",player_wins);
            printf("The computer has %d wins.\n",comp_wins);
            //saving career wins
            inFile2 = fopen("project2.txt","w");
            fprintf(inFile2,"%d %d",player_wins,comp_wins);
            fclose(inFile2);
            /*while(choice!='c' && choice!='C' && choice!='E' && choice!='e'){
                printf("Enter C to continue playing or E to exit: \n");
                scanf(" %c",&choice);
                if(choice == 'e' || choice=='E'){
                    printf("Thanks for playing.\n");
                    break;
                }
            }*/
    
    
        //}
        return 0;
    }
    
    
    int shuffle(int x[], int size){ //This function shuffles the deck
        int i;
        int index1;
        int index2;
        srand(time(NULL));
        for(i=0;i<size;++i){ //This loop initaializes the deck.
            x[i] = i+1;
        }
        for(i=0;i<size;++i){
            index1 = rand()%size;
            index2 = rand()%size;
            //The following 3 lines swap pairs of cards.
            int temporary = x[index1];
            x[index1] = x[index2];
            x[index2] = temporary;
            }
        return i;
    }
    
    
    void PrintCard(int card){
        PrintValue(card);
        printf(" of ");
        PrintSuit(card);
    }
    
    
    void PrintValue(int card){
        char const *values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10",
        "Jack", "Queen", "King", "Ace"};
        int const cardsPerSuit = 13;
        printf("%s",values[(card-1)%cardsPerSuit]);
    }
    
    
    void PrintSuit(int card){
        char const *suits[] = {"Diamonds\n", "Hearts\n", "Clubs\n", "Spades\n"};
        int const cardsPerSuit = 13;
        printf("%s",suits[(card-1)/cardsPerSuit]);
    }
    
    
    int IsValidPlay(int cardPlayed, int inPlayCard){
        if(SameValue(cardPlayed, inPlayCard) == 1 || SameSuit(cardPlayed, inPlayCard) == 1){
            return 1;
        return 0;
        }
    }
    
    
    int SameValue(int card1, int card2){
        int const cardsPerSuit = 13;
        if((card1-1)%cardsPerSuit == (card2-1)%cardsPerSuit)
            return 1;
        return 0;
    }
    
    
    int SameSuit(int card1, int card2){
        int const cardsPerSuit = 13;
        if((card1-1)/cardsPerSuit == (card2-1)/cardsPerSuit)
            return 1;
        return 0;
    }
    
    
    void GetNewCard(int card){
        PrintCard(++card);
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    The computer turn begins at line 155

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. money card game
    By filipson1 in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2010, 11:07 AM
  2. Card game war
    By Dr Saucie in forum C Programming
    Replies: 3
    Last Post: 02-11-2010, 10:25 PM
  3. Card game help
    By aaroroge in forum Game Programming
    Replies: 9
    Last Post: 07-16-2005, 06:37 PM
  4. card game
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2004, 03:50 PM
  5. IDEA: A card game
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 09-02-2002, 04:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21