War card game

This is a discussion on War card game within the C Programming forums, part of the General Programming Boards category; Hello everyone! I am having trouble with my code. There seems to be an error with comparing the two hands. ...

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

    War card game

    Hello everyone! I am having trouble with my code. There seems to be an error with comparing the two hands. I would like to only compare the faces because the suite does not matter in this game. here is my code and any help would be greatly appreciated. Thank you!!

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    
    
    
    #define TOTAL_CARD_DECK 52
    typedefint Deck[TOTAL_CARD_DECK];
    
    
    void setup_deck(Deck);
    void shuffle(Deck);
    void printcard(int);
    void print_deck(Deck);
    void player_one_cards(Deck);
    void shuffle_deck(Deck);
    void deal_deck(Deck);
    void player_two_cards(Deck);
    
    
    
    
    int main() 
    {
        
        char answer[10];
        
    printf("\nWould you like to play The Greatest Game of All Time? (y/n). press 'i' for info\n");
        
        scanf("%c",answer);
        
        while ( strcmp(answer, "y") && strcmp(answer, "n") && strcmp(answer, "i") && strcmp(answer, "Y") )
        {
    printf("Invalid Entry. 'y', 'n' or 'i' only : ");
            scanf("%c",answer);
            
        }
        if (! strcmp(answer, "y") || ! strcmp(answer, "Y"))
        {
            
            Deck d;
            
            srand(time(NULL));
            setup_deck(d);
            print_deck(d);
            
            shuffle_deck(d);
            deal_deck(d);
            
            
            
            
        }
        else if ( ! strcmp(answer, "n" ))
        {
    printf("Have a Nice day!");
        }
        else if (! strcmp(answer, "i"))
        {
            
    printf("Each suite is refered to by the first letter of their name. i.e. Hearts is H.\n");
    printf("X is for 10, J is for Jack, Q is for Queen, and K is for King");
            
        }
        
        
        return 0;
    }
    
    
    
    
    void setup_deck(Deck deck) 
    { 
    	int i;
    	for (i=0; i<TOTAL_CARD_DECK; i++) 
        { 
            deck[i] = i; 
        }
    }
    
    
    void printcard(int card) 
    {
    	static char suit[4] = {'S','D','H','C'};
    staticchar face[13] = {'2','3','4','5','6','7','8','9','X','J','Q','K','A'};
        printf ("%c%c", face[card%13], suit[card/13]);
        
    }
    
    
    
    
    void print_deck(Deck deck) 
    {
    	int i;
    printf ("\n\nDeck: \n **************** \n");
        
    	for (i=0; i<TOTAL_CARD_DECK; i++)
        {
    		printcard(deck[i]);
    		printf (" ");
    	}
    printf ("\n");
    }
    
    
    void shuffle_deck(Deck deck) 
    {
    	
    	int i;
    printf ("\n\nRandom Deck: \n **************** \n");
        
        
    	for (i = 0; i < TOTAL_CARD_DECK; i++) 
        {
            
            
            int x = rand() % 52;
            
            
            
            int temp = deck[x];
            deck[x] = deck[i];
            deck[i] = temp;
            
            
    	}
        for (i=0; i<TOTAL_CARD_DECK; i++) 
        {
            printcard(deck[i]);
            printf (" ");
        }
    }
    
    
    void deal_deck(Deck deck)
    {
        
        
        
    printf ("\n\nYour Cards: \n **************** \n");
        int card_number_dealt;
        int hand_player_one[26];
        int hand_player_two[26];
        char card[2];
        for(card_number_dealt = 0; card_number_dealt < 26; card_number_dealt++)
        {
            
            
            hand_player_one[card_number_dealt] = deck[card_number_dealt];
            
            printcard(hand_player_one[card_number_dealt]);
            printf (" ");
        }
    printf ("\n\nComputer Cards: \n **************** \n");
        int x;
        for(card_number_dealt = 26, x =0; card_number_dealt < 52, x < 26; card_number_dealt++, x++)
        {
            hand_player_two[x] = deck[card_number_dealt];
            printcard(hand_player_two[x]);
            printf (" ");
        }
        
        int card_number;
        int player_one_total = 0;
        int player_two_total = 0;
        
        printf("\n");
        for(card_number=0; card_number<26; card_number++)
        {
            if(hand_player_one[card_number] > hand_player_two[card_number])
            {
                
                printf("You Win!\n");
                player_one_total = player_one_total + 1;
            }
            
            else if(hand_player_one[card_number] < hand_player_two[card_number])
            {
                printf("You lose!\n");
                player_two_total = player_two_total + 1;
            }
            else if (hand_player_one[card_number] == hand_player_one[card_number])
            {
                
                if(hand_player_one[card_number] > hand_player_two[card_number])
                {
                    printf("You Win!\n");
                    player_one_total = player_one_total+1;
                    break;
                }
                
                else if(hand_player_one[card_number] < hand_player_one[card_number])
                {
                    printf("You lose!");
                    player_two_total = player_two_total + 1;
                    break;
                }
            }
            
            
        }
        
        if(player_one_total == 26)
    printf("YOU WON THIS ROUND!!");
        
        else if (player_two_total == 26)
    printf("YOU LOST THIS ROUND");
        
        
        
        
        printf ("\n");
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The face of a card is card % 13
    compare like
    Code:
    if((hand_player_one[card_number] % 13 ) > ( hand_player_two[card_number] % 13))
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
        char answer[10];
        scanf("%c",answer);
    You need to pass a pointer to a single character. Not the value of an entire array.

    Code:
        srand(time(NULL));
    This only should be done once, not every time in the loop.

    Code:
        while ( strcmp(answer, "y") && strcmp(answer, "n") && strcmp(answer, "i") && strcmp(answer, "Y") )
    This, along with all the other strcmp()'ing, can be removed. If you corrected your scanf, comparing the character value against a literal (instead of the overhead of string.h functions) is faster and easier to read.

    Also, you might want to consider using a do-while if you're planning on implementing a menu.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching a Tie in War Card Game
    By surefire in forum C Programming
    Replies: 10
    Last Post: 02-15-2010, 07:00 PM
  2. Card game war
    By Dr Saucie in forum C Programming
    Replies: 3
    Last Post: 02-11-2010, 11:25 PM
  3. Card game help
    By aaroroge in forum Game Programming
    Replies: 9
    Last Post: 07-16-2005, 07:37 PM
  4. card game
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2004, 04:50 PM
  5. Bridge Card Game
    By IneedHelp in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2003, 06:32 PM

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